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


Date Written: 10/8/09 Last Updated: 11/13/19

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


detect version
<?php
header('Content-Type: text/plain');
system("convert -version");
?>



Bold Text
<?php
header('Content-Type: image/png');
$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.


Connect images

+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:





Links


TAGS: php, images
Comments

Go to phpbb thread.
copyright 2005–2019