Image Manipulator

version 2.0 by Sourcehat


Image Manipulator is a PHP class with the purpose to manipulate images in different ways. Using the GD Image Library, it can manipulate all of the major image formats and maintain transparency for png and gif images.


Original image


Original image

This is the image that we will be working on throughout the demonstration. This is a live preview of the script working to see the performance. The images are being cached after their initial generation, so to see how fast they generate, use the button below to clear the cache.

Clear the cache


Outputting on the fly

Outputting manipulated images on the fly is as easy as making the src attribute of an img tag equal to the main ImageManipulator.php file path and passing manipulation methods in the query strings.

When showing an image using this method, you must remember to set up the configuration in the main ImageManipulator.php file. The path of the image that you are manipulating must be relative to the path set in the configuration and is set as the onthefly attribute.

Setting the src attribute of an img tag

<img src="ImageManipulator.php?onthefly=original.jpg"/>

To define how to manipulate an image on the fly, add the methods in the query string and separate the arguments they take with commas (see on the fly examples in different features below to gain a better understanding).


Working with images

On the other hand, if you are manipulating an image in PHP for the sake of saving it as a new image, you must include the main ImageManipulator.php file and create a new ImageManipulator object. The file path must either be absolute or relative to the folder that you are executing the script in.

Initializing Image Manipulator

require 'ImageManipulator.php';

$resource = new ImageManipulator('original.jpg');

Smart resizing

The main function of the class is to resize images for the sake of making thumbnails or simply changing the dimensions. The script will not lose the aspect ratio and is designed to crop the most important area of the original image instead of just centering it when parts are omitted due to resizing.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&resize=200,200"/>

Resizing to a certain size (width, height)

$resource->resize(200200);

The resize method accepts two arguments, in order: the width and the height of the manipulated image. If you set one of the parameters equal to zero, it the script will assign it an automatic value, remaining the correct aspect ratio of the image.

Resizing to known width, automatic height

$resource->resize(2000);

Resizing to known height, automatic width

$resource->resize(0200);

Watermarking

The script also allows you to easily add a watermark on the image for copyright purposes with an intuitive way to position it. When outputting on the fly, the path to the watermarking image must be relative to the image folder set in the main configuration.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&watermark=watermark.png,center,center&resize=200,200"/>

When manipulating images in PHP, the path to the watermarking image must either be absolute or relative to the folder that the script is executing in.

To set either the x or y position to be centered, you can pass it as the string "center"

$resource->watermark('watermark.png''center''center');

If a position is a negative number, the offset of the watermark will be that many pixels from either the bottom (y-position) or right (x-position) edge

$resource->watermark('watermark.png''center', -10);

If a position is a positive number, the offset of the watermark will be that many pixels from either the top (y-position) or left (x-position) edge

$resource->watermark('watermark.png', -1010);

Grayscaling

A minor functionality of the script is to convert the image to grayscale (black and white) in one easy line of code. Since grayscaling doesn't take any arguments, you can simply include it in the query string as shown below.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&grayscale&resize=200,200"/>

Grayscaling in PHP

$resource->grayscale();

Cropping

As suggested in the comments section, you can now crop the image by specifying the x-location, the y-location, the width, and the height of the crop. The crop method accepts those 4 arguments in order.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&crop=300,100,200,200"/>

Cropping in PHP

$resource->crop(300100200200);

Changing the quality

If you wish to save bandwidth then you can modify the quality of the manipulated image using the quality method. Setting the quality is done percentually (0-100). The quality of the image on the right is 10%.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&quality=10&resize=200,200"/>

Setting the quality in PHP

$resource->quality(10);

Stacking methods

You can, of course, manipulate an image in more than one way by calling different methods on a single resource. The order in which you execute the methods doesn't matter.

Outputting on the fly

<img src="ImageManipulator.php?onthefly=original.jpg&resize=200,200&watermark=watermark.png,center,center&crop=300,100,400,200&grayscale&quality=40"/>

Multiple methods in PHP

$resource->resize(200200);
$resource->watermark('watermark.png''center''center');
$resource->crop(300100400200);
$resource->grayscale();
$resource->quality(40);

Saving the image

To save a manipulated image, simply use the save method on the Image Manipulator object. The image can be saved as jpg, png, gif or wbmp.

Saving in PHP

$resource->save('new_image.jpg');