PHP Image Resize script – Resize Images on the fly

What does this Script do?

It changes the dimensions and hence the file size of  image files on the fly. All you need to do is specify a source file, set the new dimensions and this PHP class will generate the new re-sized image for you.

Who should use this?

Any one who needs to re-size their images automatically.

For example: If you need to generate thumbnails for the images uploaded to your image sharing site then this is the solution for you. Sometimes we need to display smaller images to save band width and improve page load time.

What does this need?

PHP 5 + GD Library

(and of course the image files to manipulate and apache server)
To check if you have got GD library on your PHP bundle Create a php file with the following code and run it.

<?php
phpinfo();
?>

Let’s get working then.

Here is the Class File. It contains two functions resize() and resize_const(). The first one only takes in a width value with the source file and generates a new image with height in constrained proportions. While resize_const() takes in width, height and source file to generate an image of exact dimensions.


<?php
class ImgResizer {
private $originalFile = '';
public function __construct($originalFile = '') {
$this -> originalFile = $originalFile;
}
public function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for
//output image quality with 100 being the most luxurious
}

//the function below will get you an image with the width and height values set by you.
// In case you need exact size images.
public function resize_const($newWidth,$newHeight, $targetFile) {
if (empty($newWidth)|| empty($newHeight) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
}
}
?>

Using the Class and the Functions.

Use the code below in as where ever you want it to work. (I am sorry but that does not include pasting the code on your photo frame)


<?php
$work = new ImgResizer('path/to/original/image.jpg');
$work -> resize(75, 'path/to/resized/image.jpg');
$work = new ImgResizer('path/to/original/image.jpg');
$work -> resize_const(200,150, 'path/to/resized/image.jpg');
?>

This is not my original code. I am building upon this base.

Similar topics I have written about recently:

One Response so far.

  1. MockY says:

    The resulting image becomes extremely pixelated. You can barely see what the image is of. This is even when the quality is set to 100.

Leave a Reply