Image Scale calculation
You can choose the new Width, or new Height but not both (if you’re going by Height, put a -1 for new Width
will return an array, element 0 is the new width, element 1 is the new height
Say I want to scale a 800×600 image to have a height of 120 and I want to figure out what the width would be I would use
$scale = imageScale(”path/to/image.jpg”, -1, 120);
$scale[0] = 160 //correct width
$scale[1] = 120
function imageScale($image, $newWidth, $newHeight)
{
if(!$size = @getimagesize($image))
die(”Unable to get info on image $image”);
$ratio = ($size[0] / $size[1]);
//scale by height
if($newWidth == -1)
{
$ret[1] = $newHeight;
$ret[0] = round(($newHeight * $ratio));
}
else if($newHeight == -1)
{
$ret[0] = $newWidth;
$ret[1] = round(($newWidth / $ratio));
}
else
die(”Scale Error”);
return $ret;
}
