How To Resizing Images With PHP

One of the most important thing for making your website load faster is resizing image. Sometimes you have images that you want to be showed on your website, but the size of the images is not suitable and you are lazy to use photoshop for resizing and re-uploading the images to your website. :)

You can resize the image by using html script with adding attribute width=”..px” in img tag, but this way is same with loading your original images and the size of the file is same, too and its doesn’t make your website load faster.

To resolve this problem, this PHP script will allow you to resize the image and the size of image file will be reduce and you can also choose the quality of your image to be shown in your website.


$filename = $_GET['src'];
$percent = $_GET['scale'];
$quality = ($_GET['q'])?$_GET['q']:100;
header('Content-Type: image/jpeg');
$img_type = exif_imagetype($filename);
list($width, $height) = getimagesize($filename);
$newwidth = $width * ($percent / 100);
$newheight = $height * ($percent / 100);
$image_p = imagecreatetruecolor($newwidth, $newheight);
if($img_type == 1){
$image = imagecreatefromgif($filename);
}elseif($img_type == 2){
$image = imagecreatefromjpeg($filename);
}elseif($img_type == 3){
$image = imagecreatefrompng($filename);
imagecopyresized($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, null, $quality);

Original image :

For using this script , you just need put in your tag img, just like this


<img src="resize.php?src=" />

Result :

The script above will reduce the size of images to 50% from original size.

And to change image quality, you can add &q=(1-100) in src attribute, like this

<img src="resize.php?src=">


If you have any problems in this script please  don’t hesitate to tell me in post comment.

I hope it can help you ! :)

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha : * Time limit is exhausted. Please reload the CAPTCHA.