Create Thumbnail with PHP
Create a copy of an image as a thumbnail. You get a great function that use the GD library to create the new thumbnail file.
Often you need to change the size/dimensions of images in your web site.
One example could be if you let your visitors upload an image of themself when they register. For different reasons it could be important to make sure the images have a specific size, so they don’t mess upp hte design of your layout. Another example could be if you have a photo gallery for your self, then it’s nice to have the same size for images.
Together with standard PHP installations you get something that is called GD. This package is made for creating and modifying images directly from PHP. From PHP 4.3 you get GD bundled and only have to make sure it’s activated/compile, but this is the case most of the times.
Read about PHP’s GD functions: here.
GD is great, but it can be a little bit hard to understand at the beginning. So therefor I will give you a Great function for creating thumbnails to start with:
What you do is to call the function with four parameters:
- imageDirectory – Where is your original images stored?
- imageName – What’s the filename of the file to make a thumbnail of?
- thumbDirectory – Where to store the newly created thumbnail?
- thumbWidth – What width do you want for the thumbnail?
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);
$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight * $ratio;
$thumbImg = imagecreate($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbImg), imagesy($thumbImg));
imagejpeg($thumbImg, "$thumbDirectory/$imageName");
}
createThumbnail("img", "theFileName.jpg", "img/thumbs", 100);
The function is not that hard to read. Simply copy the whole script and try it. The way I call the function you have to store your images in a directory called “img” in the same directory where this script will be. You need a directory called “thumbs” inside the img-directory. You also need to make sure the webserver have write permissions to this directory, otherwise it can’t save the output file.
I choose to set the width to 100px wide. What happens with the height is a little bit special. The height is changed just as much as the width is. Let’s say the original image is 200px wide and 300px high. Then the width will be 100px ofcourse and the height is also changed to the half as the width and becomes 150px.
Hope you get some use of this script and have fun with it and use it as much as you like!!

May 7th, 2009 at 4:26 am
Thanks
June 19th, 2009 at 9:45 am
You have an error in the code.
It should be like this:
1. $thumbHeight = $origHeight / $ratio;
And it makes a thumb from the top left part of your picture not from the whole pic.
July 8th, 2009 at 2:39 am
Thanks for the correction!
We will soon start publishing new tutorials with higher quality. These are old and should e rewritten, but hopefully they are useful for some basic stuff!
August 12th, 2009 at 2:31 pm
I was looking for this
September 4th, 2009 at 11:30 pm
Here is the corrected code for anyone who needs it.
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth)
{
$srcImg = imagecreatefromjpeg(“$imageDirectory/$imageName”);
$origWidth = imagesx($srcImg);
$origHeight = imagesy($srcImg);
$ratio = $origWidth / $thumbWidth;
$thumbHeight = $origHeight / $ratio;
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $origWidth, $origHeight);
imagejpeg($thumbImg, “$thumbDirectory/$imageName”);
}
***notice the “imagecopyresized” now contains …$thumbWidth, $thumbHeight, $origWidth, $origHeight); which is the correct way to resize, not crop the image.
I also changed imagecreate to imagecreatetruecolor because the quality is better.
Hope this helps.
God Bless
Scott
September 5th, 2009 at 9:37 am
Thanks a lot Scott for taking the time to improve the code!
If you are interested we are always looking for guest bloggers at 999tutorials!?!
October 9th, 2009 at 5:05 pm
Thanks for this piece of code, I really appreciate it.
November 11th, 2009 at 10:59 am
Thanks for the corrected code,I always follow this site whenever its required and will continue to do that…
December 27th, 2009 at 8:09 am
Mine code, creates thumbnails from gif/jpg/png
function createthumb($File_In,$thumb_x,$Path_Out)
{
//select img type
$type=substr($File_In,strlen($File_In)-3,3);
switch ($type)
{
default:
{
$img_in = @imagecreatefromjpeg($File_In);
break;
}
case ‘gif’:
{
$img_in = @imagecreatefromgif($File_In);
break;
}
case ‘png’:
{
$img_in = @imagecreatefrompng($File_In);
break;
}
}
if($img_in==”){return;}
if(imagesx($img_in)>$thumb_x)
{
$thumb_y=imagesy($img_in)/(imagesx($img_in)/$thumb_x);
}else{
$thumb_y=imagesy($img_in);
$thumb_x=imagesx($img_in);
}
$img_out = @imagecreatetruecolor($thumb_x, $thumb_y);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, imagesx($img_out), imagesy($img_out), imagesx($img_in), imagesy($img_in));
imageinterlace($img_out,1) ;
imagejpeg($img_out, $Path_Out, 50);
//destroy the tmp images
imagedestroy($img_out);
imagedestroy($img_in);
}
Still only got a problem with the png’s
December 29th, 2009 at 8:59 am
Looks great. It will help a lot of people! Let me know if you are interested in writing guest posts at 999tutorials.com!
March 4th, 2010 at 5:25 pm
How about adding this should allow you to deal with all image types
$ext = substr(strrchr($imageName, ‘.’), 1);
if ($ext == “png”) {$srcext = imagecreatefrompng(“$imageDirectory/$imageName”);goto run;}
if ($ext == “jpg”) {$srcext = imagecreatefromjpeg(“$imageDirectory/$imageName”);goto run;}
if ($ext == “bmp”) {$srcext = imagecreatefrombmp(“$imageDirectory/$imageName”);goto run;}
if ($ext == “gif”) {$srcext = imagecreatefromgif(“$imageDirectory/$imageName”);goto run;}
run:
$srcImg = $srcext;