PROGRAMMING SOLUTIONS

Tuesday 30 June 2009

A php function that calculates the size of an image to a preview thumb

Note that $equal2Width is by default set to 60 pixels, you can set this parameter to your own needs.


function SetPreviewImageDimensions($img, &$newImgWidth, &$newImgHeight)
{
$equal2Width = 60;

$arrayImg = getimagesize($img);
$ImgWidth = $arrayImg[0];
$ImgHeight = $arrayImg[1];
if ($ImgWidth > $equal2Width) {
$newImgWidth = $equal2Width;
$newImgHeight = ($ImgHeight/$ImgWidth) * $newImgWidth;
}
else {
$newImgWidth = $ImgWidth;
$newImgHeight = $ImgHeight;
}

}

Variables $newImgWidth and $newImgHeight are passed by refference, so the returned values of width and height will be passed in these variables for your later use.

No comments:

Post a Comment