Don’t you just adore the simplicity of PHP? It is a cake walk to create thumbnails in PHP, without any plugins like imagemagick or NetPBM, and it is super fast too. Here’s the simple code.
Don’t you just adore the simplicity of PHP! It is a cake walk to create thumbnails in PHP, without any plugins like imagemagick or NetPBM, and it is super fast too. Here’s the simple code.
The following code takes a “source image” from a “source path” and makes a thumbnail out of it, placing the thumbnailed image into a “thumb path.” It is fairly straightforward so I won’t waste time explaining stuff. Post a comment below if you have any questions or problems.
- Elements that you’ll probably have to edit are highlighted in red
- Comments are in green
Make sure the path for thumbnails is writeable, the imagejpeg function takes care of writing by itself, you don’t need to fwrite etc.
$sourcePath = ‘images/‘; // Path of original image
$sourceUrl = ‘http://domain.com/images/‘;
$sourceName = ‘test.jpg‘; // Name of original image
$thumbPath = $sourcePath . ‘thumbs/‘; // Writeable thumb path
$thumbUrl = $sourceUrl . ‘thumbs/‘;
$thumbName = “test_thumb.jpg“; // Tip: Name dynamically
$thumbWidth = 60; // Intended dimension of thumb
// Beyond this point is simply code.
$sourceImage = imagecreatefromjpeg(“$sourcePath/$sourceName”);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$targetImage = imagecreate($thumbWidth,$thumbWidth);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,
$thumbWidth,imagesx($sourceImage),imagesy($sourceImage));
imagejpeg($targetImage, “$thumbPath/$thumbName”);
// By now, the thumbnail is copied into the $thumbpath
// as the file name specified in $thumbName, so display
echo “<img src=’$thumbUrl$thumbName’ alt=”>”;
?>
This above example is for JPEG format. Replace all occurences of “jpeg” in the above code with “gif” and you’re good to go for GIF files! Similarly, you can edit the “jpeg” to “png” and you’re good to go. For instance, replace “imagejpeg” with “imagegif” or “imagepng“.
Eventually, you can also cobble together some code to figure out the file type of a $sourceImage dynamically (from a directory on the server for instance) and apply the appropriate functions. This is just an example to give you an idea!
Just FYI, my setup on the server is as follows:
- Root HTTP folder:
/home/domain/public_html/ - Images folder:
/home/domain/public_html/images/
This becomes our$sourcePathbecause this is where the original image comes from. - Thumbs folder:
/home/domain/public_html/images/thumbs/(this folder is CHMODed 744, and is our$thumbPath)
25 Comments
This requires GD installed on the server, and not all hosts have that either.
Hai ,
This is a nice site .
Generating JPG/GIF/PNG thumbnails in PHP using imagegif, imagejpeg, imagepng the example code is very good one.
Hope this site will help lots of developer like me
thank u.
imagegif is not working!
Ake, please elaborate when you write messages on this site. What do you mean is not working — the code is resulting in errors, or does the PHP version you use not support imagegif? It does work, for millions of people.
I do belive that gif support was pulled in later versions of, something to do with licencing restrictions on this format.
I do belive that gif support was pulled in later versions of GD, something to do with licencing restrictions on this format.
Imagegif is only working in GD2 or higher if im right. Check what version u got with ur host or ur server.
http://my2.php.net/manual/en/function.imagegif.php
[quote]
In case you are wondering (I know I’ve been) GD support for GIFs will not re-appear until the patent GIF expires world-wide on July 7th, 2004. Though the patent has expired in the U.S. it hasn’t in Europe.[/quote]
I found that the thumbnail generated has VERY washed out colors (JPEG). I am dissapointed with the poor quality of the image. If someone has a solution for this please email me if anyone has some time. mihai323@hotmail.com
Thanks.
Relating to the above comment…
Use imagecreatetruecolor() NOT imagecreate()
AND
Use imagecopyresampled() NOT imagecopyresized()
This tutorial seems to be for GD
that last line of my above comments is supposed to say:
The comments seems to be for GD version 2 or less
The latest builds of GD2 now have gif support again. If your server is not allowing it ask them to upgrade to the latest version of GD2. Believe me it vastly improves over GD1.
Is there any way to create thumbnails without using
GD, IM, NETBPM.
as stated above, requires GD1. GIF as far as i know isnt supported due to licensing, even though the license expired last year. but even so, GD1 is very poor quality (gallery.menalto.com has an example of it). better to use ImageMagick or NetPBM, or even GD2.
thanks for the tip about using imagecopyresampled() instead of imagecopyresized(). It produces a much nicer, softer thumbnail
good site indeed.It helps to find out good tricks as well.
(Sorry for my English)
A little improved function -
<?php
function thumbgenerator ( $image, $saveorshow, $quality, $thumbsize , $savename){ // v.1 by Soban
$handle = @imagecreatefromjpeg( $image );
$x = imagesx ( $handle ); // Image Original Width
$y = imagesy ( $handle ); // Image Original Height
// Calculating whitch side is larger
if ( $x > $y ){
$max = $x;
$min = $y;
}
if ( $x <= $y ){
$max = $y;
$min = $x;
}
$rate = $max / $thumbsize; // Thumbnail Ratio
$final_x = $x / $rate;
$final_y = $y / $rate;
if( $final_x > $x ) {
$final_x = $x;
$final_y = $y;
}
$final_x = ceil ( $final_x ); // Thubnail Width
$final_y = ceil ( $final_y ); // Thubnail Height
$black_picture = imagecreatetruecolor ( $final_x, $final_y ); // Generating blank image for thumbnail
imagefill ( $black_picture, 0, 0, imagecolorallocate ( $black_picture , 255, 255, 255 ) );
imagecopyresampled ( $black_picture, $handle, 0, 0, 0, 0, $final_x, $final_y, $x, $y );
imagestring ($black_picture, 1, $final_x-4, $final_y-8, “.”, imagecolorallocate ( $black_picture, 0, 0, 0 ) );
if ( $saveorshow == ‘show’ ) { header ( ‘Content-type: image/jpeg’ ); imagejpeg ( $black_picture, ”, $quality); }
if ( $saveorshow == ‘save’ ) imagejpeg ( $black_picture, $savename, $quality);
if ( $saveorshow == ‘save&show’ ) { header ( ‘Content-type: image/jpeg’ ); imagejpeg ( $black_picture, ”, $quality); imagejpeg ( $black_picture, $savename, $quality); }
imagedestroy ( $handle );
imagedestroy ( $black_picture );
}
thumbgenerator (‘images/030.jpg’, ‘save’, 10, 100, ‘thumbs/newimage1.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 20, 100, ‘thumbs/newimage2.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 30, 100, ‘thumbs/newimage3.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 40, 100, ‘thumbs/newimage4.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 50, 100, ‘thumbs/newimage5.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 60, 100, ‘thumbs/newimage6.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 70, 100, ‘thumbs/newimage7.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 80, 100, ‘thumbs/newimage8.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save’, 90, 100, ‘thumbs/newimage9.jpg’);
thumbgenerator (‘images/030.jpg’, ‘save&show’, 100, 160, ‘thumbs/newimage.jpg’);
?>
Hello,
This is working on IE but not on Mozilla/Netscape.?
Thanks so much for the code, this is a life saver… only one problem, all the thumbnails I create have a little black dot in the bottom right-hand corner. I’ve run into this problem on two different servers, not sure which GD versions either one is running… any ideas?
Yeah, i’ve the similar problem. The thumbnail created will have a black line at the bottom. But this only happen to some of the pictures i’ve uploaded. Please help…. thanks.
Great code!
Thank you for sharing
hi
is there any way that i can create gif animated image from jpeg ,
image as background and gif image on top of it …
check out this one
http://freelogic.pl/thumbnailer/
I also suffer from the black line blues…. I believe it only occurs with space outside of the range of the actual desired image dimensions when dealing with cropping, yet I can’t seem to figure out how to fix it yet.
[...] credit goes to http://sniptools.com/vault/generating-jpggifpng-thumbnails-in-php-using-imagegif-imagejpeg-imagepng for the snippet! Share this [...]