(26 ratings)   
By: www.stefashwell.com
This 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....
Added: 24 April 2008    Views: 1391  
PathComputers    Programming    Php
Keywords: script   language   freelancer   coder   code   program   web   php   programming  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 
This 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"
About the Author :
PHP Thumbnail Generation Script
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments
  says :
4/10/08, 4:10 pm

awesome tutorial, really usefull :)