website promotion banner
eturnkeys
Your Ad Here
Web Programming  Home Web Programming PHP PHP Thumbnail Generation Script
rss

PHP Thumbnail Generation Script

Author: Stefashwell.com More by this author


ThumbnailThis tutorial focuses on a thumbnail generation script I wrote for the Accidental President site. Rather than generating a smaller representation of the image, it cuts out a section of the photograph to create an obscure snapshot of the photo itself. The thumbnail's size is variable also, so the script be used across different sites to create different sized thumbnails where needed.

First off, we get 3 variables passed when the script is called (see last section). $image is the full URL of the image we want the thumbnail to be of. $newWidth and $newHeight is the width and height you would like the thumbnail to be.

$image = $HTTP_GET_VARS['image'];
$newWidth = $HTTP_GET_VARS['width'];
$newHeight = $HTTP_GET_VARS['height'];

Next we find out the width and height of the full image we are using, and store it in the variables $width and $height

$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];

Next we take the current width of the image and take off the wanted width. This means when the image is cropped it will not get cropped too close to the edge.

$width = $width-$newWidth;

The same is then done for the height

$height = $height-$newHeight;

Now we generate x and y co-ordinates where the thumnail will start cropping from. There are a few ways this could be done to achieve different results, however I opted to simply half the width and height

$x = $width/2;
$y = $height/2;

The next part of the code makes a copy of the image to an image named $src.

$src = ImageCreateFromJpeg($image);

Next a blank image is created with the wanted width and height

$tmb = ImageCreateTrueColor($newWidth,$newHeight);

Now we can generate our actual thumbnail. This line of code copies the old image to the blank one, starting and the generated x and y co-ordinates. This will therefore crop the image at the specified size

ImageCopy($tmb, $src, 0, 0, $x, $y, $newWidth, $newHeight);

Now we can display the image

header('Content-type: image/jpeg');
ImageJpeg($tmb, null, 100);

Finally, destroy images from memory

ImageDestroy($src);
ImageDestroy($tmb);
ImageDestroy($thumb);

And thats it, as I said at the start, this script can be used in many situations and at different sizes.

Finally, to use the script in your code use the following line of html. Rather than pointing the src of the img tag to an image, it points to this php script and pass the location of the image, width and height along with it.

<img src="thumbnail.php?image=http://www.mysite.com/image.jpg&width=50&height=50" />

Downloads

Click here to download the source code for this tutorial.



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "PHP Thumbnail Generation Script"