imagickLib
About imagickLib
First example
Configuration
Input and output images
Actions
Useful methods
About imagickLib
The php class imagickLib uses ImageMagick to manipulate images.
Requirements
- PHP4 or higher
- ImageMagick (tested with version 6.2.4)
- webserver need write-access
Written by Oliver Kührig, viersicht
First example
At the beginning you have to load and instantiate the class. After these steps load the image, make as many “actions” as you want and finally save it to a new file.
Here we read the file “Bambus.jpg”, resize it and save it into a new file.
// include the class file (required) include('class.imagickLib.php'); // make a instance of the class (required) $l_oImageMagick = new imagickLib(); // image input by file $l_oImageMagick->input("file", "./img/Bambus.jpg"); // resize the image to max. 400px width and max. 300px height $l_oImageMagick->actionResize(400, 300); // image output into file named "testpic0" $l_oImageMagick->output("file", "testpic0");
Configuration
Locate the binary files of ImageMagick. By default it is “/usr/bin/”.
var $imageMagickDir = '/usr/bin/'; // ImageMagick binary files
Enable or disable logging. Define the logfile-path and file. The file needs write permission by the webserver.
var $log = true; // write log file, if true var $logDir = './'; // logfile dir var $logFile = 'imagickLib.log'; // logfile name
imagickLib produces temporary file. Define the temporary directory to store them. In the end the files will be deleted by the “cleanUp”-method.
var $tempDir = '/var/tmp/'; // httpd must be able to write there
Defines the default directory to store the resulting images. Can be changed while using the class.
var $targetDir = './'; // target dir for the result picture
Defines the JPG-Quality
var $jpgQuality = '65'; // quality for JPG images
To produce small image-files, strip all profile informations for example exif data. You have to perform at least one “actionConvert()”
var $stripProfile = true; // if set to true on conversion image profile information will be stripped
Input and output images
Input
The input-method knows three possibilities to load images:
- “file” reads the images from the file-system.
- “binary” gets the image in binary-format for example by the function “file_get_contents()”
- “http” reads the image from the internet
$l_oImageMagick->input("file", "./img/Bambus.jpg"); $l_oImageMagick->input("binary", file_get_contents("./img/Bambus.jpg")); $l_oImageMagick->input("http", "http://www.example.com/Bambus.jpg");
Output
To get the produced image you can use two ways:
- “file” writes the image to the default image-directory. The extension will be appended automatically.
- “binary” returns the binary data.
$l_oImageMagick->output("file", "testpic0"); $l_oImageMagick->output("binary");
Actions
The class provides you different methods to manipulate images.
actionBlur
Blur the image with a gaussian operator. Define the radius as first and the sigma as second parameter.
$l_oImageMagick->actionBlur(5, 2);
actionBorder
Resize the image to given size. Make a transpanrent border around the image to fit the size. The direction parameter defines the position of the filling image.
$l_oImageMagick->actionBorder(400, 300, 'north');
actionConvert
Converts the picture in a new format. Possible values are ‘jpg’, ‘png’, ‘gif’, ‘bmp’, …
$l_oImageMagick->actionConvert("png");
actionCrop
Crops the image to given size. The example crops the image at center 100×100 pixel.
Third parameter:
- “center” crops at center (default)
- “left” crops only from the top left side
- “right” crops only from the top right side
$l_oImageMagick->actionCrop(100, 100, 'center');
actionFlip
Flips the image “horizontal” (default) or “vertical”
$l_oImageMagick->actionFlip('vertical');
actionFrame
Draws a frame around the image with a width (default=5) and a color (default=”666666″)
$l_oImageMagick->actionFrame(6, "FF0000");
actionFuzzyShadow
Create soft fuzzy shadows
$l_oImageMagick->actionFuzzyShadow();
actionGrayscale
Converts the image to grayscale
$l_oImageMagick->actionGrayscale();
actionLabel
Labels the image. Produce a alpha-transparent gray background with white text.
- “south” draws text at the bottom of the image (default)
- “north” draws text at the top of the image
- “center” draws text in the middle of the image
$l_oImageMagick->actionLabel("Testlabel Test2 Test3 Test4", "south");
actionManyPolaroids
Produce many polaroid images with random rotation.
$l_oImageMagick->actionManyPolaroids();
actionMonochrome
Converts the image to monochrome (2 color black-white)
$l_oImageMagick->actionMonochrome();
actionNegate
Converts the image to negative
$l_oImageMagick->actionNegate();
actionPolaroid
Polaroid-like image with white border, a shadow, rotation. The parameter defines the rotation in degrees.
$l_oImageMagick->actionPolaroid(-6);
actionResize
Resize the image to given size. Third parameter defines the method:
- “keep_aspect” changes width and/or height of image proportional (default)
- “fit” fit image to given size
$l_oImageMagick->actionResize(400, 300);
actionRotate
Rotates the image by given degree. Second optional parameter defines the color in background (default is white). The third parameter:
- “” empty is standard rotation (default)
- “morewidth” rotates the image only if only if its width exceeds the height
- “lesswidth” rotates the image only if its width is less than the height
$l_oImageMagick->actionRotate(30);
actionSquare
Square the image by to different methods:
- “crop” crop to largest square (default)
- “border” add colored border (second parameter) to square the image. No cropping.
$l_oImageMagick->actionSquare('border', 'FFA200');
Useful methods
cleanUp
After all is done, you should perform the cleanUp-method to delete all temporary files.
$l_oImageMagick->cleanUp();
goBackInAction
Go steps back and virtually delete actions. The parameter defines the amount of actions to revoke (default is 1). In this example we undo the crop-action.
include('class.imagickLib.php'); $l_oImageMagick = new imagickLib(); $l_oImageMagick->input("file", "./img/Bambus.jpg"); $l_oImageMagick->actionGrayscale(); $l_oImageMagick->actionCrop(100, 100, 'center'); $l_oImageMagick->output("file", "testpic0"); $l_oImageMagick->goBackInAction(1); $l_oImageMagick->actionFrame(1, "000000"); $l_oImageMagick->actionFuzzyShadow(); $l_oImageMagick->output("file", "testpic1"); $l_oImageMagick->cleanUp();
printError
For debug-purpose use the printError-method to print errors.
$l_oImageMagick->printError();
05.April 2008 at 20:43h
[...] einen konfortablen Zugriff auf ImageMagick zur Bildmanipulation. Details und Download auf Oliver Kührig’s Blog… Bookmark to: (No Ratings Yet) Loading [...]
09.January 2009 at 22:48h
Noticed a few bugs:
Geometry is returned by ImageMagick 6.3.5 as Geometry: 400×265+0+0 in example4.php
So the code of parseImageData()
should be in lines 390ff
$tmp1 = explode(‘ ‘, $this->arrImageRawData[$i]);
$tmp2 = explode(‘x’, $tmp1[1]);
$tmp3 = explode(‘+’, $tmp2[1]);
$this->arrImageData["width"] = $tmp2[0];
$this->arrImageData["height"] = $tmp3[0];
And on my Server the masked ‘\(‘ and ‘\)’ have not to be masked like ‘(‘ and ‘)’
Nice and easy class for predefined image filters
Regards
Jako
19.February 2009 at 23:25h
Hi Jako!
The method is now changed.
Thank you!
Have a nice day,
Oliver
28.December 2009 at 22:37h
[...] [...]