Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

The basics

ImageMagick is a command line program and does not have a GUI ( Graphics User Interface ). As such you have to learn the various commands to make it work and the order in which the commands must be used.

ImageMagick is an external program and is not built into php so it must be "called" from php usually with the exec function.

Care must be taken as with any other code to validate user input as it could be used maliciously.

php uses the exec function to send the command line code to ImageMagick.

Note: Although these examples are written with php in mind most of them can be used from the command line with a little modification. Mainly removing the exec(" and the ") from the example code.

Imagemagick is always being updated and some code may not work on all versions

I use the two formats below to write my code as it is quite simple. If it is a simple one line command I will use the first method.

For simple code

exec("convert input_image output_image");

For more complicated code I use

$cmd = "input_image operations";
exec("convert $cmd output_image");

The benefit of the second method is that you can echo out the command which is useful if it is a long command or contains variables.

I have found that I can just use convert in my code but depending on the server setup you may need to use the path to convert e.g. /user/local/bin/convert The path to convert can be found by using the code here.

There are different programs within ImageMagick, I mostly use convert as that does everything I want. There is also:

animate
animates an image sequence.

compare
mathematically and visually annotate the difference between an image and its reconstruction.

composite (examples)
overlaps one image over another.

conjure
interprets and executes scripts written in the Magick Scripting Language (MSL).

convert (examples)
convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.

display
displays an image or image sequence on any X server.

identify
describes the format and characteristics of one or more image files.

import
saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.

mogrify
resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. Mogrify overwrites the original image file, whereas, convert writes to a different image file.

montage (examples)
create a composite image by combining several separate images. The images are tiled on the composite image optionally adorned with a border, frame, image name, and more.

stream
a lightweight tool to stream one or more pixel components of the image or portion of the image to your choice of storage formats. It writes the pixel components as they are read from the input image a row at a time making stream desirable when working with large images or when you require raw pixel components.

Note: Certain options will only work on an X server

When creating images it is best not to create temporary images as they will have to be removed later and so if possible carry out all your modifications in the one command.

If you do need to create temporary images it is best to save them as a lossless format for example .png as if you save them as a .jpg they will suffer the loss of quality due to the .jpg compression.

You can delete the temporary images if they are created by using the php unlink( ) function