Before you can begin using the Image Manipulator class, you must include the file in which it resides by using the require or include expression.
require 'ImageManipulator.php';
To select an image to work with, initiate a new object in this manner.
$resource = new ImageManipulator('path/to/my/photo.jpg');
We will be working on this image (which would presumably be located in path/to/my/photo.jpg) throughout the documentation.
To resize the image for the sake of making a thumbnail or simply resizing it, simply use the following method.
$resource->resize(100, 100);
The resize function accepts two arguments - the width and the height of the resized image. By making one of them equal to zero, it will automatically be set to a value according to the other one, remaining the correct aspect ratio.
$resource->resize(150, 0);
$resource->resize(0, 150);
To convert the image into a black and white one, use the grayscale function.
$resource->grayscale();
You can add a watermark to any position on the image by passing the source of the watermarking image and the requested position to the watermark function.
$resource->watermark('watermark.png', 10, 50);
The above method will add the watermark to the position at 10 pixels from the left and at 50 pixels from the top on the image. By passing either of the positions as negative will allow you to start counting from the right and bottom edges of the image.
$resource->watermark('watermark.png', -30, -15);
It is often useful to place the watermark in the center. To do so, simply pass the position as the string "center" instead of a number.
$resource->watermark('watermark.png', 'center', 'center');
All sorts of combinations will work.
$resource->watermark('watermark.png', 'center', 10);
$resource->watermark('watermark.png', -50, 'center');
You can crop a part of an image using the crop method by specifying the location and the dimensions of the desired cut. The method accepts 4 arguments, in order: the x-position, the y-position, the width, and the height of the crop.
$resource->crop(130, 50, 200, 100);
You can, of course, manipulate the image using more than one method. The order in which you do them does not matter.
$resource->resize(200, 100);
$resource->grayscale();
$resource->watermark('watermark.png', 'center', -10);
Please view the onthefly example for a demo on how to use this functionality.
$resource->output();
The output method accepts one argument - a boolean indicating whether to cache the image or not. Caching is enabled by default, pass false as the first argument to disable it. Don't forget to set the cache folder path in the configuration part of the main ImageManipulator.php file if you wish to use this functionality to speed up the loading of your images.
Please view the avatar example for a demo on how to use this functionality.
$resource->save('new_image.png');