How to Make Thumbnail Images Using PHP
So you want to create a thumbnail from a given image… PHP makes this task easy. I first wrote the function below to deal with images uploaded by users at a site I help administer. Uploaded images to this site can be jpegs, pngs, or gifs, so I had to write a function to deal with all three. This code is originally based off of a function found at webcheatsheet.com
How to Do It:
function createThumbs( $pathToImages, $fname, $pathToThumbs, $thumbWidth )
{
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( (strtolower($info['extension']) == ‘jpg’) || (strtolower($info['extension']) == ‘jpeg’) )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );}
if ( strtolower($info['extension']) == ‘gif’ )
{
// load image and get image size
$img = imagecreatefromgif( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );// save thumbnail into a file
imagegif( $tmp_img, "{$pathToThumbs}{$fname}" );}
if ( strtolower($info['extension']) == ‘png’ )
{
// load image and get image size
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );// save thumbnail into a file
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );}
}
How This Works
The function createThumbs takes four arguments.
- $pathToImages -> The path to the folder the image file is in. It might look something like this: /home/content/username/html/list/uploads/
- $fname -> The filename: pic1.gif, johnny.jpg, etc…
- $pathToThumbs -> The path to the folder you want the thumbnail image to be in. You DO NOT want this to be the same path as $pathToImages, because the thumbnail image will have the same name as the original image.
- $thumbWidth -> The width that you want the thumbnail to be in pixels.
An important note that I should make here is that this function has nothing in place, other than the file extension check, to make sure that the image files contain nothing malicious. Since the files createThumbs() works with have already been uploaded it is assumed that the files have been properly scrutinized.
The first thing createThumbs() does is call pathinfo(). pathinfo() breaks the parts of a path into an array. This is useful to us, because we need the file extension to properly create the thumbnail. Next createThumbs() checks to see if we are dealing with a jpeg. If the original file is a jpeg createThumbs creates a new image based on the original image with imagecreatefromjpeg(). The next step is to get the width and height of this image (imagesx(), imagesy()). Using $thumbWidth and the ratio of the original width and height createThumbs() sets the new width and height of the thumbnail. Using these new ratios createThumbs() creates a "blank" image that will be the same width and height of the thumbnail with imagecreatetruecolor(). imagecopyresized() is used to resize the image and save it in the "blank" just created. imagecopyresized() might seem like it takes a lot of parameters, but if you take a look at the documentation it really isn’t that complicated. Now that the thumbnail exists as a true color image all createThumbs needs to do is convert the file into a jpeg. To do this imagejpeg() is called.
That’s how the function works for jpegs. There are minimal differences for .pngs and .gifs.








































