▼  Site Navigation Main Articles Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc News Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP –– Image Processing –– part 5: ImageMagick
written by: admin

Date Written: 10/8/09 Last Updated: 4/19/20

ImageMagick 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.

Listed below are a few things that ImageMagick can do that GD doesn't.

Display version of ImageMagick installed

header('Content-Type: text/plain');
system("convert -version");

system() Execute an external program (in this case ImageMagick) and display the output.

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
header('Content-Type: text/plain');
system("exec identify images/pops/gdsample1.jpg");

This generates:

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: images/pops/gdsample1.jpg
file type: JPEG
dimensions: 128x126
file size: 6.14KB
color type: 8-bit
image class: DirectClass
total number of unique colors: 0.000u
time to read and transform image: 0:00.000
not sure: 128x126+0+0

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.

Connect images: example 1

+append will connect two images horizontally.  -append will connect two images vertically. ref
system("convert images/catdog.jpg images/azumangadaioh4.jpg +append montage_cat2.jpg");

This generates:

connect images: example 2

$im = new Imagick();


$combined = $im->appendImages(false);

header("Content-Type: image/png");
echo $combined;

This generates:

This second example does the same thing as example 1, but uses object oriented language.


Overall, I prefer example 1, but it depends on your coding needs and what you are trying to do as well as what is available to you.

append images


TAGS: images, php

Go to phpbb thread.
copyright 2005–2021