Click here to Skip to main content
13,446,191 members (35,707 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 18 Aug 2008

Thumbnail images in PHP

, 18 Aug 2008
Rate this:
Please Sign up or sign in to vote.
Creating thumbnail images using PHP.


At times, we need to change image sizes. We will see here how we can do that using PHP.

Using the code

This is the main function we will use:

function thumbnail_image($original_file_path, $new_width, $new_height, $save_path="")


  • $original_file_path: Path of the original image
  • $new_width: Width of the thumbnail image
  • $new_height: Height of the thumbnail image
  • $save_path: Path of the created thumbnail (if you skip this, the thumbnail image will be saved in the original image's folder with a thumbnail.* file name)


This function is void thus returns nothing.

The following function creates the thumbnail image in the sample:

thumbnail_image("source.jpg","320", "200", "");

The above sample creates thumbnail.jpg as a 320*200 image inside source.jpg.

How does it work

I get the image information using the getimagesize PHP function.

$imgInfo = getimagesize($original_file_path);
$imgExtension = ""; //extension of original image

switch ($imgInfo[2])
    case 1:
        $imgExtension = '.gif';

    case 2:
        $imgExtension = '.jpg';

    case 3:
        $imgExtension = '.png';

If $save_path equals "" then the thumbnail file will be "thumbnail".$imgExtension.

if ($save_path=="") $save_path = "thumbnail".$imgExtension ;

Here is how to get the width and height of the original image and creating an image object for the thumbnail image:

// Get new dimensions
list($width, $height) = getimagesize($original_file_path);

// Resample
$imageResample = imagecreatetruecolor($new_width, $new_height);

if ( $imgExtension == ".jpg" )
    $image = imagecreatefromjpeg($original_file_path);
else if ( $imgExtension == ".gif" )
    $image = imagecreatefromgif($original_file_path);
else if ( $imgExtension == ".png" )
    $image = imagecreatefrompng($original_file_path);

imagecopyresampled($imageResample, $image, 0, 0, 0, 0, $new_width,
                   $new_height, $width, $height);

Lastly, create the thumbnail image and save it:

if ( $imgExtension == ".jpg" )
    imagejpeg($imageResample, $save_path);
else if ( $imgExtension == ".gif" )
    imagegif($imageResample, $save_path);
else if ( $imgExtension == ".png" )
    imagepng($imageResample, $save_path);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohammad Dayyan
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralObservation of a Discrepancy Pin
JaredRClemence19-Aug-08 5:16
memberJaredRClemence19-Aug-08 5:16 
Dear Mr. Dayyan:

First, let me express my appreciation for your article. I had never known about some of the PHP functions that you used and now have something more to look into. Thank you.

Regarding the discrepancy to which I refer in the subject of this message: In your function definition, you state the parameters are $original_file_path, $new_width, $new_height, $save_path = "" (in that order exactly). However, in your implimentation, you place the $save_path before the $new_width. Could you revise either the implementation text or the function definition so that they are concistant?

Thank you again. Other than the above discrepancy, I think it is a most excellent article!

Kind regards,

Jared Clemence
LAMP Hobbiest
GeneralRe: Observation of a Discrepancy Pin
Mohammad Dayyan19-Aug-08 10:30
memberMohammad Dayyan19-Aug-08 10:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180314.2 | Last Updated 18 Aug 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid