How to resize and make thumbnail image in PHP?

One month ago I got a task to make thumbnail images and save them at different path.

There were lots of images, so it was very difficult to make thumbnail images and save them manually.

So for that I got a PHP script to make thumbnail images and save them at the given path.

Here is the script:

Step : 1 : Make a  “make_thumb” function to resize (make thumbnail) image and save it to the given path.

This function has four arguments:

  1. $source : Source image file path and name
  2. $target : Target file path and name
  3. $new_w : New width for the image
  4. $new_h : New height for the image
// This is the function that will create the thumbnail image from the given image
// The resize will be done considering the width and height defined, but without deforming the image
function make_thumb($source,$target,$new_w,$new_h)
// Call the function and get image extension.
$ext = getExtension($source);

// Creates the new image using the appropriate function from gd library
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))



// Gets the dimmensions of the image
$old_x = imagesx($src_img);
$old_y = imagesy($src_img);

// Next we will calculate the new dimensions for the thumbnail image
// The next steps will be taken:
// 1. Calculate the ratio by dividing the old dimensions with the new ones
// 2. If the ratio for the width is higher, the width will remain the one define in WIDTH variable
// And the height will be calculated so the image ratio will not change
// 3. Otherwise we will use the height ratio for the image
// As a result, only one of the dimensions will be from the fixed ones
$ratio1 = $old_x/$new_w;
$ratio2 = $old_y/$new_h;

if($ratio1>$ratio2) {
else {

if ($old_x > $thumb_w || $old_y > $thumb_h)
// We create a new image with the new dimensions

// Resize the big image to the new created one

// Output the created image to the file. Now we will have the thumbnail into the file named by $target
imagepng($dst_img,$target, 9);
imagejpeg($dst_img,$target, 100);

Step : 2 : Make a “getExtension” function to get the extension of given file.

And it has “filename” as an argument.

// This function reads the extension of the file. It is used to determine if the file is an image by checking the extension.
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1);
return $ext;

Step : 3 : Call “make_thumb” function and resize (make thumbnail) image.

Syntax :

make_thumb(“source image file path and name”, “target file path and name”, "new width for the image", "new height for the image");

For e.g.

make_thumb("C:/wamp/www/ThumbnailBlog/OrginalImages/abc.jpg", "C:/wamp/www/ThumbnailBlog/ThumbnailImages/xyz.jpg", 125, 125);

I hope this will help you….


  • To use the given code you must have gd library support.
  • With the use of given code you could not increase height and width of image.
  • This code will be useful only with .jpg, .jpeg, .gif and .png entension.

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: