You are here: Home » HTML/PHP/JS/CSS » Image Resizing With PHP –Really Simple Tips

Image Resizing With PHP –Really Simple Tips

A very simple way of resizing your images:

Even though PHP is a web programming language, there are lots of libraries developed that don’t strictly deal with web functionality – there are compression libraries, image processing libraries, as well as a plethora of others.  I’m saying that to remind you that there is no need to reinvent the wheel. In this day and age, given the Internet, one could very easily find a solution to their problem, or even several of them, and pick out the easiest and most convenient one after that. 

PHP is an object oriented language, which means it relies on an abstraction called an object, which is a separate piece of data in the program, with which data and procedures alike are associated. To get to our simple resizing script, we’ll need to use a library which will make an object to use with our image.

For this tutorial, I have chosen an image library called WideImage. It can easily be found and downloaded with a Google search. It’s free and easy to use, and only needs to be included with a single line before use.

So our resizing example consists of the following code lines:

<?php

include "path-to/WideImage.php";

$image = WideImage::load("path-to/image.jpg");

$resized = $image->resize(400, 300);

$resized->saveToFile("small.jpg");

?>

So let’s explain what our code does.

The first line is basically how we include the library that will do the actual residing work. It is located in the directory called path-to, and the name of the library file is WideImage.php. By using the include function, all of its classes and functions become usable.

In the second line, we declare a variable called $image, and we assign the class WideImage to it, by using a constructor called load(). To the constructor we pass the image file as a parameter. The image is called “image.jpg” and it is also located in the path-to directory.

Then we declare a variable called $resized , which we will use as a container for the resized image. From there , we call the resize function from the image class, with 400 pixels width and 300 pixels height. This all happens in the third line.

In the fourth line , we simply save our image to another file.

That’s it, four simple steps and we have a nicely saved image, done entirely in PHP. There are other arguments you can pass to the resize function, but they control whether aspect ratio is kept or not. If you want to know more about them, there is a lot of information in the library’s documentation. Hopefully this article has helped to solve your problem.

 

About admin

Ajinkya Thete is passionate Blogger,a self-taught web developer and a person who is always looking for innovation.

3 comments

  1. Thanks for the tip. I usually just do re-sizing in Paint.

  2. This is good and concise..
    i have seen alot of longer code writen for this same result..
    but love this..

Leave a Reply

Scroll To Top