Creating Image Thumbnails with PHP
Today I have been working on my butterfly tattoos website. There is a lot more to come on that website, I have ideas for Flickr integrations and more. At the moment I am getting c. 500 unique users a day passing through the site and I have made 0 changes since 2004. This site needs to be brought up to date. One way I am looking to do this is through enabling users to simply upload their own photographs (this will help me too since I have about 50 tattoos sitting ready to be uploaded). A key part of this is to do all the image handling on the server with pre-existing PHP functions. While working on this idea I found the following code source "create square image thumbnails with PHP".
This code uses the simple but very powerful GD graphics utilities that come with PHP 4 and 5. I have used them a lot and I won't reproduce his code here however I will show my use of the code. I create a function which I store in a seperate include and can be called any time. The function will return "none" if there has been no error or will return an error like "I don't support images of the type image/bmp support png, jpg and gif try converting to that and uploading" if an image format I don't support is sent.
function createThumb($source,$dest,$mimetype)
{
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
if($width> $height)
{
$x = ceil(($width - $height) / 2 );
$width = $height;
}
elseif($height> $width)
{
$y = ceil(($height - $width) / 2);
$height = $width;
}
if ($mimetype == "image/jpeg" || $mimetype == "image/jpg")
{
// set up the image (note imagecreatetruecolor doesn't work for gif)
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
// what is the input format, create from that
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
// the 100 is the quality here, this is quite high
imagejpeg($new_im,$dest,100);$error = "none
return $error;
}
elseif ($mimetype == "image/png")
{
// set up the image (note imagecreatetruecolor doesn't work for gif)
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
// what is the input format, create from that
$im = imagecreatefrompng($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagepng($new_im,$dest);$error = "none
return $error;
}
elseif ($mimetype == "image/gif")
{
// set up the image (note imagecreatetruecolor doesn't work for gif)
$new_im = ImageCreate($thumb_size,$thumb_size);
// what is the input format, create from that
$im = imagecreatefromgif($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagegif($new_im,$dest);
$error = "nonereturn $error;
}
else
{
$error = "I don't support images of the type $mimetype but I do support png, jpg and gif try converting to that and uploading";
return $error;
};
}
The best bit of this code in my opinion is the ability to crop the image to a square while maintaining aspect ratio. This is managed by the imagecopyresampled() php function.
