PHP –– Image Processing –– part 5: ImageMagick
written by: admin
10/8/09 Last Updated:
is PHP's other family of image manipulation functions. GD and ImageMagick are different extensions of PHP. Neither is necessarily better than the other. Each can do things that the other can't. They may already be installed with your version of PHP. Try the last example listed below to see if it is installed.
Listed below are a few things that ImageMagick can do that GD doesn't.
flip an image horizontally
header( 'Content-Type: image/jpg' );
system("convert images/pops/gdsample1.jpg -flop jpg:-");
will give you some facts about an image file
system("exec identify images/pops/gdsample1.jpg");
images/pops/gdsample1.jpg JPEG 128x126 128x126+0+0 8-bit DirectClass 6.14KB 0.000u 0:00.000
To the best of my knowledge, the information is defined as:
relative file location:
total number of unique colors:
time to read and transform image:
This is useful in determining whether an image has the correct extension. If I were to rename a file from test.jpg to test.png the script will be able to detect if the file extension is incorrect.
Set the maximum number of colors used in an image
system("convert images/pops/gdsample1.jpg -colors 13 jpg:-");
save an image at filename
system("convert images/pops/gdsample1.jpg -write aaa.jpg jpg:-");
Convert one image format to another
system("convert im1.jpg -write im2.png jpg:-");
This will get the image im1.jpg
and create an image with filename im2.png
. The output image will be in jpg format even though the file saved is
in the png format.
$a1="-pointsize 96 -font 'include/fonts/tl.ttf'";
$a2=" -transparent white -background \"rgb(201,228,255)\" -stroke black -strokewidth 2 label:'Q'";
system("convert $a1 $a2 png:-");
In this example I am breaking apart a long line of instructions by assigning them to variables. This code is also not the current preferred method for bold text, but is the only one I could get to work with my older version of ImageMagick. double quotes can be escaped like they normally would with php. Relative file locations can be used when specifying custom fonts. ImageMagick has many built in fonts available.
will connect two images horizontally. -append
will connect two images vertically. ref
system("convert images/catdog.jpg images/azumangadaioh4.jpg +append montage_cat2.jpg");
Go to phpbb thread.