Instant image resize using PHP GD library

August 21st, 2015 PHP 2,807 views

While working on ecommerce website, I once faced a situation when my client requests me to change all the product thumbnail resolutions and I had to resize all the product images manually. Infact I made a PHP script to resize all the images in specific folder. But there is even better solution for this which I found in a later stage. I am going to share a sourcecode of it below.

Copy and paste below code at specific place in your project folder. E.g. myproject/functions/resize.php

Now in your html code, wherever you have written <img> tags, use it the way I have done in below example.

So basically we are passing resize.php file as image source and then giving 3 values as query string.

  1. path – it is the original image path which is obviously a bigger sized image
  2. width – width of the resized thumbnail
  3. height – height of the resized thumbnail

This way it will simply resize the image and render it on frontend. Plus you don’t need to store this resized image anywhere as it will be resized under server cache and then rendered.

Hope you guys find it helpful.

Leave your comments: