▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc 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

<?php
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
<?php
header( 'Content-Type: image/jpg' );
system("convert images/pops/gdsample1.jpg -flop jpg:-");
?>



will give you some facts about an image file
<?php
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.

-identify


Set the maximum number of colors used in an image
<?php
header('Content-Type:image/jpg');
system("convert images/pops/gdsample1.jpg -colors 13 jpg:-");
?>



save an image at filename
<?php
header('Content-Type:image/jpg');
system("convert images/pops/gdsample1.jpg -write aaa.jpg jpg:-");
?>



Convert one image format to another
<?php
header('Content-Type:image');
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
<?php
system("convert images/catdog.jpg images/azumangadaioh4.jpg +append montage_cat2.jpg");
?>


This generates:






connect images: example 2

<?php
$im = new Imagick();

$im->readImage('images/screenshots/whr7.JPG');
$im->readImage('images/screenshots/whr8.JPG');

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

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


This generates:



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

Notes:

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


Links


TAGS: images, php
copyright 2005–2024