View Full Version : Image manipulation practices
11-11-2007, 06:47 PM
Hi I have an image upload script that works but I need it to also resize images. Right now it takes the users image and puts it straight in a folder on the server. I would like to have it check the image to make sure the width and height are not over a specific pixel value, say 800. If it does exceed this value I need to resize the image to be less than 800 * 800 while keeping the proportions correct. That is the fullsize image. I also need it to create a medium (under 300 * 300) and a thumbnail and upload those to different folders.
What is the best way to do these manipulations? What is the best way to do these manipulations keeping in mind server load.
11-11-2007, 07:47 PM
Use getimagesize() (http://php.net/getimagesize) to get the image dimensions, and a function such as the one below to get the new sizes if the image is larger than your maximum dimensions. Then use imagecreatetruecolor() (http://php.net/imagecreatetruecolor) to make image pointers for the new images; imagecopyresampled() (http://php.net/imagecopyresampled) to resize & copy the new images to the newly created image pointers; and finally save them with imagejpeg() (http://php.net/imagejpeg) or imagepng() (http://php.net/imagepng).
* ratioSizes - Calculates Proportionately Accurate Image Resizes
* @param integer Image Width
* @param integer Image Height
* @param integer Maximum Width
* @param integer Maximum Height
* @return array
function ratioSizes(integer $width, integer $height, $maxWidth = 250, $maxHeight = 200)
if($width > $maxWidth || $height > $maxHeight)
$diff = 0;
$aspectRatio = $width / $height;
if($width > $height)
$newHeight = round($maxWidth / $aspectRatio);
if($newHeight > $maxHeight)
$diff = $newHeight - $maxHeight;
return array('width' => round($maxWidth - ($diff * $aspectRatio)), 'height' => $newHeight - $diff);
$newWidth = round($maxHeight * $aspectRatio);
if($newWidth > $maxWidth)
$diff = $newWidth - $maxWidth;
return array('width' => $newWidth - $diff, 'height' => round($maxHeight - ($diff * $aspectRatio)));
return array('width' => $width, 'height' => $height);
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.