Case Study : Create Thumbnail Version from Image with PHP GD Library
Requirements : Webserver Packages, already installed. And make sure your PHP support Image GD Library (version 1-8 above)
Follow this step..
Step 1 : Prepare the Work Folder
- Create folder named tutorphp in your document root
- Prepare sample JPG image in this folder. For this tutorial, i use this sample image (p.s. : you can use any jpeg image). If you want to use this image, just right click and save as

- Rename the image to myPic.jpg (p.s. : this “rename” things just to make easy for follow the tutorial
)
Step 2 : Create main script to resize image in to thumbnail
- Type the following script,
<?php $image = 'myPic.jpg'; //default image, change here if you want to use another image //get the image size and image info list($width_orig, $height_orig, $image_type) = getimagesize($image); if($image_type !== 2) { // 2 is for JPEG Image echo 'Image is not JPEG Image!'; } else { $thumbname = 'thumbnail_'.$image; //default name for thumbnail version of image // setting the height for the width 75px (75px is default width for the thumbnail). this is for maintain the ratio $height_tb = (int) ((75 / $width_orig) * $height_orig); //Create a new true color image $image_p = imagecreatetruecolor(75, $height_tb); //Create a new JPEG image from file or URL $image = imageCreateFromJpeg($image); // //Copy and resize part of an image with resampling imagecopyresampled($image_p, $image, 0, 0, 0, 0, 75, $height_tb, $width_orig, $height_orig); //upload image to folder if(!is_writeable(dirname($thumbname))) { echo 'Unable to Upload image to ' . dirname($thumbname); } else { //output image to browser or file, the parameter is imageJPEG(resource image, name of the image, quality) imageJpeg($image_p, $thumbname, 100); } } ?> <center><h1>Create Thumbnail Image</h1> <table border='1' cellpadding='5' cellspacing='5'> <tr align='center'> <td>Original</td> <td>Thumbnail</td> </tr> <tr> <td><img src="myPic.jpg" alt="image" /></td> <td><img src="<?php echo $thumbname;?>" alt="thumbnail" /></td></tr> </table> <center> - Save with the name imagetb.php, save in the tutorphp folder
- Explanation : see on the script (script with green) color, Okay
Step 3 : Testing Code
- Go to http://localhost/tutorphp/imagetb.php
- You can see your image (original) with its thumbnail
just like the picture below..

- Check tutor.php folder. You will see the thumbnail_myPic.jpg file, that is thumbnail version of your image


![]()
Others that might interesting too:
Copyright
All scripts and techniques demonstrated on itx.web.id can be used in whatever manner you wish without attribution. You cannot copy whole tutorials, either in English or translated to another language.
This post is also available in: Indonesian
English
Indonesia