Monday, May 10, 2021
Home Technology Programming PHP Image Resize script - Resize Images on the fly

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.

1 COMMENT

  1. 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.

Comments are closed.

Most Popular

Organic Food and Its Importance for Our Health and Our Planet

Food constitutes one of the pillars of human survival, besides being a source of our energy. Over the years of evolution, food...

List of Organic Vegetables Price in Delhi from Organic Food Suppliers

After facing the pandemic and realizing the need to build strong immunity in our bodies, we today witness a strong push towards...

Top Gift Ideas To Celebrate This Christmas Day With Your Family

Christmas is celebrated every year on the 25th of December. It brings joy and cheers to our life. People celebrate Christmas to...

KBC Question Categories 2020

Kaun Banega Crorepati is one of the most popular t.v game shows in India. The show takes its inspiration from the British...

MY DICTIONARY PROJECT

I have started a new project called the Indic Dictionary. This will cover popular household words in India and what they are called in various languages. Eventually, I will make this an easy to use app where where people can easily find something like “hing in English” or “Tea Tree Oil in Hindi” or “carrom seeds in Urdu”.