Pages

Friday, May 21, 2010

AS3: Function to resize an image


/**
* Function to resize an image
**/
function resizeImage (id, ReqWidth, ReqHeight)
{
  id.scaleX = 1;
  id.scaleY = 1;
  var OriginalW:Number = id.width;
  var OriginalH:Number = id.height;
  var new_width:Number = 0;
  var new_height:Number = 0;
  if (OriginalW<=ReqWidth && OriginalH<=ReqHeight)
  {
    new_width = OriginalW;
    new_height = OriginalH;
  }
  else
  {
    if (OriginalW>ReqWidth)
    {
      new_width = ReqWidth;
      new_height = Math.floor(OriginalH * (ReqWidth / OriginalW));// 149*(119.30/149) = 83.51
      if (new_height > ReqHeight)
      {
        new_width = Math.floor(new_width*(ReqHeight/new_height));
        new_height = ReqHeight;
      }
    }
    else if (OriginalH>ReqHeight)
    {
      new_width = Math.floor(OriginalW*(ReqHeight/OriginalH));
      new_height = ReqHeight;
      if (new_width>ReqWidth)
      {
        new_width = ReqWidth;
        new_height = Math.floor(new_height*(ReqWidth/new_width));
      }
    }
  }
  id.width = new_width;
  id.height = new_height;

}

No comments:

Post a Comment