PHP code to rescale image and show an image thumb
Posted On at by milanDuring development mostly we need to show images from data base or so by rescaling them but rescaling them in a way that they don't loose the original length to width ratio.
e.g. you have an area of 500*500 pixels to show the image in following code while calling the function you will specify the target as 500 rest of things will be well taken care of by the function. It will rescale image to fit in that 500 pixel sqare area and fit your image in a way that it dont loose the original length to width ratio. Also this function will link image to its own so that you can click image to see it in full size in new browser tab/window. And this thing obviously can be changed into function code if you don't need the link on image.
function showimgthumb($imgpath,$target)
{
list($width, $height, $type, $attr) = getimagesize($imgpath);
if ($width > $height)
{
$percentage = ($target / $width);
}
else
{
$percentage = ($target / $height);
}
$width = round($width * $percentage);
$height = round($height * $percentage);
$hiwi="width=\"$width\" height1=\"$height\"";
echo " echo $hiwi;
echo " /> ";
}//end of function Show image thumbnails
?>
And you will call this function like following.
$target=45; // For 45*45 pixels target area
$imgpath="images/myphoto.jpg"; //This is path of image form page where the function is being called
showimgthumb($imgpath,$target);
?>