Php makes it easy to create thumbnails if you have the GD library
installed
. Lets see the following example.
<?
header("Content-type: image/jpeg");
$first=ImageCreateFromJPEG("myimage.jpg") or die("couldn't open image");
$second=ImageCreate(100,150) or die("couldn't create image");
ImageCopyResized($second,$first,0,0,0,0,100,150,imagesx($first),imagesy($first)) or die("coudln't resize image");
ImageJPEG($second,"thumb.jpg") or die("couldn't save image");
?>
In the example above we will create a thumbnail of 100x150 pixels
named thumb.jpg from the image myimage.jpg. Lets see the code step by step.
header("Content-type: image/jpeg");
tells the browser that the following content is an image of type jpeg.
If you don't provide a header the browser will work with the data as
if it where an html file.
$first=ImageCreateFromJPEG("myimage.jpg") or die("couldn't open image");
The imagecreatefromjpeg function is used to open an existing image,
myimage.jpg (if you want to work with png files you should use
imagecreatefrompng instead). This function returns an image identifier
that is stored in the $first variable.
$second=ImageCreate(100,150) or die("couldn't create image");
the function imagecrate is used to create a new image of size 100x150.
The variable $second stores the image identifier.
ImageCopyResized($second,$first,0,0,0,0,100,150,ImageSX($first),ImageSY($first)) or die("couldn't resize image");
Imagecopyresized is the function used to create a resized copy of
our image. To use it we must provide the identifier of the image we
want to create ($second); the identifier of the first image ($first);
the following zeros indicate that we want to copy all the image(they
are the x and y coordinates of upper left corner for the destination
image and the source image respectively); 100,150 indicates that we
want a new image of that size in pixels; and imagesx($first),
imagesy($first) indicate the width and height of the source image
(imagesx and imagesy are image functions that return the width and
height respectively).
ImageJPEG($second,"thumb.jpg") or die("couldn't save image");
Finally, imagejpeg saves the file which identifier is $second, and saves
it with the name thumb.jpg. Notice that if you want to use png images
you should use imagepng instead.