SniptoolsSniptools | Design & Technology Observations

RSS

Generating JPG/GIF/PNG thumbnails in PHP using imagegif, imagejpeg, imagepng

Sep 6th 2003
21 Comments

Respond
Trackback

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:

  1. Root HTTP folder: /home/domain/public_html/
  2. Images folder: /home/domain/public_html/images/
    This becomes our $sourcePath because this is where the original image comes from.
  3. Thumbs folder: /home/domain/public_html/images/thumbs/ (this folder is CHMODed 744, and is our $thumbPath)



This post is tagged

21 Comments

  1. Cine

    This requires GD installed on the server, and not all hosts have that either.

  2. Shaik Yesdani

    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.

  3. ake

    imagegif is not working!

  4. Shashank

    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.

  5. Sean

    I do belive that gif support was pulled in later versions of, something to do with licencing restrictions on this format.

  6. Sean

    I do belive that gif support was pulled in later versions of GD, something to do with licencing restrictions on this format.

  7. Wes

    Imagegif is only working in GD2 or higher if im right. Check what version u got with ur host or ur server.

  8. gx

    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]

  9. Mihai

    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.

  10. Vince

    Relating to the above comment…

    Use imagecreatetruecolor() NOT imagecreate()
    AND
    Use imagecopyresampled() NOT imagecopyresized()

    This tutorial seems to be for GD

  11. Vince

    that last line of my above comments is supposed to say:
    The comments seems to be for GD version 2 or less

  12. Moondog

    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.

  13. Parag

    Is there any way to create thumbnails without using
    GD, IM, NETBPM.

  14. jaeSun

    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.

  15. dave

    thanks for the tip about using imagecopyresampled() instead of imagecopyresized(). It produces a much nicer, softer thumbnail :)

  16. shaik shajahan

    good site indeed.It helps to find out good tricks as well.

  17. soban [BG]

    (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’); ?>

  18. Bhupi8

    Hello,

    This is working on IE but not on Mozilla/Netscape.?

  19. David

    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?

  20. CK

    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.

  21. Dennis

    Great code!
    Thank you for sharing

Incoming Links