Image Scale calculation

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;
}

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.