PHP Thumbnail Image creation script

Creating thumbnail images is a very common requirement.
Many scripts use this to create thumbnails of uploaded images or any stored images. We will learn how to create thumbnail images while uploading images to the server. Please read the tutorials on file upload to know how to upload files to the server with necessary permissions in PHP 5.

The file ( or image ) will be first uploaded to a directory and then one thumbnail image will be created in a different directory. So for creating the thumbnail we will check the file extension ( it is gif or jpeg image ) but to keep the script simple we are not checking here the file size. So if the file size is to be restricted then file size validation is to be added. You can see how the file size checking is done in file upload tutorial.

This script is tested with PHP version 5.3.18 .

<FORM ENCTYPE="multipart/form-data" ACTION="addimgck.php" METHOD=POST>
Upload this file: <INPUT NAME="userfile" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>

We will now see the addimgck.php file and check the code to create the thumbnail image. We will upload the file to the upimg directory and check to see if file upload is successful or not.

// To display file name, temp name and file type , use them for testing your script only//////
echo "File Name: ".$_FILES['userfile']['name']."<br>";
echo "tmp name: ".$_FILES['userfile']['tmp_name']."<br>";
echo "File Type: ".$_FILES['userfile']['type']."<br>";
echo "<br><br>";
$add="upimg/".$_FILES['userfile']['name']; // the path with the file name where the file will be stored, upload is the directory name.
//echo $add;
if(move_uploaded_file ($_FILES['userfile']['tmp_name'],$add)){
echo "Successfully uploaded the mage";

}else{echo "Failed to upload file Contact Site admin to fix the problem";

Now the image is uploaded to the directory and from that image we will create thumbnail image of it. We will first set the height and width of the thumbnail  images to be  generated. Then we will check the type of the file and now we are checking for file type of gif and jpeg and if the image is not of this type then we are terminating the script giving an error message.

///////// Start the thumbnail generation//////////////
$n_width=100; // Fix the width of the thumb nail images
$n_height=100; // Fix the height of the thumb nail imaage

$tsrc="thimg/".$_FILES['userfile']['name']; // Path where thumb nail image will be stored
//echo $tsrc;
if (!($_FILES['userfile']['type'] =="image/jpeg" OR $_FILES['userfile']['type']=="image/gif")){
echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";

Now let us start with gif file thumb nail image creation. We will first read the height and width of the uploaded image and then resize it to our thumbnail image size. Note that in some GD library support GIF version is not included so to check that we have used one if condition and  accordingly used jpeg support.  We will be using imagecreatetruecolor to retain the actual color combination of the main picture. We have used function_exists() to check supported image functions

$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
}////////// end of gif file thumb nail creation//////////

////////////// starting of JPG thumb nail creation//////////
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$n_height=($n_width/$width) * $height; // Add this line to maintain aspect ratio

Same way we can add other graphics format like BMP, PNG etc to the system

Maintaining aspect ratio in thumbnail

After uploading the image we can maintain aspect ratio while creating the thumbnail image. We will keep the width of the thumbnail as fixed and change the height to maintain the aspect ratio. For this we will use the following formula

thumbnail_height = (thumbnail_width/original width) x original_height

Based on this our code is here

$n_height=($n_width/$width) * $height;

download Php Thumbnail creation script

Create profile picture by uploading photo by members

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


parteek kumar


please give an best and short idea for make thubnail after image upload in php.


I will try your code soon....


Great .
Thank you very much.


The above code works well, thanks! However it does not allow for aspect ratio preservation.


One line of code is to be added to proportionately manage the height of thumbnail image. That part is added now. Thanks.
navjit singh


thanx above code is very helpfull


How will the image upload and thumbnail php script look like if it was to be in an application form where applicant will need to upload image and immediately the browser uploads then the image previews or thumbnails in the form?


To display thumbnail the file has to be uploaded first. Displaying directly from user local computer is not possible.
Php Expert Programmer


Great tutorial. This tutorial was really helpful to create a thumbnail images. Thanks for this tutorial.

Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer