ImageMagick: Resize, Covert and Merge Image

Use ImageMagick to convert, edit, or compose bitmap images in a variety of formats. In addition resize, rotate, shear, distort and transform images automagically.

ImageMagick is a command line program and does not have a GUI.

ImageMagick is an external program and is not built into php so it must be “called” from php usually with the exec function or from other language with os execution function.

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.

The current version of ImageMagick I am using is 6.7.1-10.

Installation of ImageMagick

1) Ubuntu
sudo apt-get install imagemagick

2) Linux
We can install either tar file or rpm but i prefer to install tar file.

Installation Tar file

Unpack the distribution with this command:
$magick> tar xvfz ImageMagick.tar.gz

Next configure and compile ImageMagick:
$magick> cd ImageMagick-6.7.1-10
$magick> ./configure
$magick> make

If ImageMagick configured and compiled without complaint, you are ready to install it on your system. Administrator privileges are required to install. To install, type
$magick> sudo make install

Finally, verify the ImageMagick install worked properly, type
$magick> /usr/local/bin/convert logo: logo.gif

For a more comprehensive test, run the ImageMagick validation suite. Ghostscript is a prerequisite, otherwise the EPS, PS, and PDF tests will fail.
$magick> make check

Installation RPM
ImageMagick RPM’s are self-installing. Simply type the following command and you’re ready to start using ImageMagick:
$magick> rpm -Uvh ImageMagick-6.7.2-1.i386.rpm

The method I use:

<?php
exec("convert input_image output_image"); 
?>

The method some others use:

<?php
$cmd = "input_image output_image";
exec("convert $cmd"); 
?>

Or we can use in command line as:
convert input_image output_image

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

Issue:
Some isssues when convert image to pdf or merging pdf files to pdf as :

unable to open image `gif2.gif’: @ error/blob.c/OpenBlob/2588.
Postscript delegate failed `test.pdf’: @ error/pdf.c/ReadPDFImage/664.

// list all the delegation lib and other information about imageMagick
convert -list configure

Display:
DELEGATES bzlib djvu fontconfig freetype gvc jpeg jng jp2 lcms lqr openexr png rsvg tiff x11 xml wmf zlib

To fix delegation error by installation below packages which are not available in your system and finally install imageMagick6.7.1-10:

freetype
jpeg
libpng
tiff
zlib
ghostscript
jng
fonts

Following are the steps to install ghostscript after then you can install similarly other packages which are not installed in your system.

To install GhostScript

Download Ghostscript source code ( usually it’s a .tar.gz ) to the server’s temporary directory ( for example /tmp ) or as your choice.
http://image_magick.veidrodis.com/image_magick/delegates/ghostscript-8.71.tar.gz
cd /tmp
enter /tmp folder
// Unpack the archive
tar -xvzf ghostscript-8.71.tar.gz
// Enter the Ghostscript source code directory
cd ghostscript-8.71
// install ghostscript
./configure
make
make install

Some More command:

// Convert one formate to another.
mogrify -format jpg *.gif

// Convert jpg file to pdf file.
convert test.jpg final.pdf

// Merge jpg and pdf file to pdf file.
convert test.jpg and test.pdf final.pdf

// Convert all images which formate is jpg to pdf file.
convert *.jpg final.pdf

// Resize all images to thumbnails
mkdir thumbnails
mogrify -path thumbnails -thumbnail 100×100 *

// Append gif images
convert sky.gif watch.gif +append aa.gif

// To optimize the file size of ouput pdf file
convert index.pdf print.pdf -page A4 -compress jpeg -quality 70 output.pdf

References:

http://www.rubblewebs.co.uk/imagemagick/imagick.php
http://www.imagemagick.org/
For extra library
http://image_magick.veidrodis.com:8003/image_magick/delegates/
For RPM package
ftp.imagemagick.org/pub/ImageMagick/linux/CentOS/i386/ImageMagick-6.7.1-9.i386.rpm