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.

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.
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.
<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).
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.
require 'ImageManipulator.php'; $resource = new ImageManipulator('original.jpg');
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.
<img src="ImageManipulator.php?onthefly=original.jpg&resize=200,200"/>
$resource->resize(200, 200);
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.
$resource->resize(200, 0);
$resource->resize(0, 200);
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.
<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.
$resource->watermark('watermark.png', 'center', 'center');
$resource->watermark('watermark.png', 'center', -10);
$resource->watermark('watermark.png', -10, 10);
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.
<img src="ImageManipulator.php?onthefly=original.jpg&grayscale&resize=200,200"/>
$resource->grayscale();
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.
<img src="ImageManipulator.php?onthefly=original.jpg&crop=300,100,200,200"/>
$resource->crop(300, 100, 200, 200);
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%.
<img src="ImageManipulator.php?onthefly=original.jpg&quality=10&resize=200,200"/>
$resource->quality(10);
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.
<img src="ImageManipulator.php?onthefly=original.jpg&resize=200,200&watermark=watermark.png,center,center&crop=300,100,400,200&grayscale&quality=40"/>
$resource->resize(200, 200);
$resource->watermark('watermark.png', 'center', 'center');
$resource->crop(300, 100, 400, 200);
$resource->grayscale();
$resource->quality(40);
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.
$resource->save('new_image.jpg');