Tuesday 22 April 2008

Resizing Images - Server Side

Resizing Images

On our soon to be new website we have an area for landlords to go into and enter their images. This then uploads to our server into an individual folder. On the index page of our website we have a random product viewer. Here we are showing randomly a range of properties. What I need to is check to see if there if a thumbnail of the image. If not create one. You will need to have GD2 uploaded on your server to run this. You can check that by running the function phpinfo().


Heres the code

######################################################

img1=$row["img1"]; // heres our image
$rid=$row['rid']; // this is a unique number for every property and I have used this to name each folder of images for each properties// here comes the image search - $pathFull = $abpath.$rid.'/'; // this gives us our root to image folder - $abpath is set in config.php$imgFull = $pathFull.$img1; // with image as well $thumbnailImg = $pathFull.'160thum'.$img1; // where are thumbnail will be if not already

if (!file_exists($thumbnailImg)) { // if we haven't got an thumbnail then make oneif (file_exists($imgFull)) { // if the image doesnt exist the following code would have produced an error

$origImage = $imgFull; // just renaming for more clarity

$imageSizes = GetImageSize($origImage); // get the sizes of the big image - we are going to make sure the image is kept in proportion

$imageheight = $imageSizes[1]; // get the sizes of the big image height

$thumbWidth = 160; // how big do you want the thumbnails - you can change this to any height

// here comes the calculation to work out thumbheight

if ($thumbHeight){ // making sure we have a value to work with so to not cause an error

if ($imagewidth > $thumbWidth){ // making sure the value can divide, again so to not cause an error

$divisionBy = $imagewidth / $thumbWidth;
$thumbHeight = $imageheight / $divisionBy;

}

else {

$thumbHeight = $imageheight; // if the image is smaller than 160 then leave it alone

}
}

resizeImage($origImage, $thumbnailImg,$thumbWidth, $thumbHeight); // runs the function see below
}
}
if (!file_exists($imgFull)){ // if there's no image we can display a message of 'no pic available'

$displayimg = 'nopic.gif';}
else
{
$displayimg = $row['rid'].'/160thum'.$row["img1"];

}


************************************************
then all we have do is wrap $displayimg in an img src tag -
ONE MORE THING -- you'll need this function to get this all to work. I've put mine in a folder /includes/functions/images.php but you can put it on the same page if you want.


function resizeImage

($src_file, $dst_file, $max_width, $max_height)
{ // Formats we will handle.
$formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png');

// Get info on the image.

if (!$sizes = getimagesize($src_file))
return false;
// A known and supported format?

if (!isset($formats[$sizes[2]]) !function_exists('imagecreatefrom' .$formats[$sizes[2]]))
return false;
// Create image from file.
$imagecreatefrom = 'imagecreatefrom' . $formats[$sizes[2]]; if (!$src_img = $imagecreatefrom($src_file))
return false;
// Calculate the new size.
$max = array('width' => $max_width, 'height' => $max_height);
$dst['width'] = $src['width'] = $sizes[0];
$dst['height'] = $src['height'] = $sizes[1];
foreach (array('width' => 'height', 'height' => 'width') as $k => $v)
if (!empty($max[$k]) && $dst[$k] > $max[$k]) {
$dst[$k] = $max[$k];
$dst[$v] = floor($src[$v] * $max[$k] / $src[$k]);
}

// Create true color image.
$dst_img = imagecreatetruecolor($dst['width'], $dst['height']);

// Do the resize.
if(!imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst['width'],$dst['height'], $src['width'], $src['height']))
return false;

// Save image to file.
imagejpeg($dst_img, $dst_file);

// Free the memory.
imagedestroy($src_img);
imagedestroy($dst_img);

return true;}



No comments: