PHP ImageMagick MagickWand Examples

A while back I explained how to compile the ImageMagick extension for PHP and this past week I got around to creating some example code to make some of the command line examples I have in ImageMagick command line examples part 1 and ImageMagick command line examples part 2.

The first step of course is to make sure the MagickWand extension is installed. You will want to verify that it is listed in a phpinfo() call before trying any of these examples. After verifying that you have the extension installed you might want to read an introduction to using MagickWand before looking at these examples. And of course you will want to know where the MagickWand reference documentation is once you are ready to try more.

For more information on the options used in these examples it is best to look at their corresponding command line example.

Example 1: Simple Annotate

<?php

// convert flower.jpg -font courier -fill white -pointsize 20 -annotate +50+50 Flower flower_annotate1.jpg

$resource = NewMagickWand();
$dwand = NewDrawingWand();
$pwand = NewPixelWand();

PixelSetColor($pwand, "white");
DrawSetFont($dwand, "/usr/share/fonts/default/TrueType/cour.ttf");
DrawSetFontSize($dwand, 20);
DrawSetFillColor($dwand, $pwand);

MagickReadImage( $resource, 'small_flower.jpg' );

if( MagickAnnotateImage( $resource, $dwand, 0, 0, 0, "Flower" ) )
{
  header( 'Content-Type: image/gif' );
  MagickEchoImageBlob( $resource );
}
else
{
  echo MagickGetExceptionString($resource);
}

?>

One note on the above is that I needed to specify the exact location of the font to get it to show up. I believe this isn't always needed but if you try to leave it out and nothing shows up you should try specifying the full path to the font.

Example 2: Complex Annotate

<?php

// convert flower.jpg -fill white -box "#00770080" -gravity South -pointsize 20 -annotate +0+5 "   Flower  " flower_annotate2.jpg

$resource = NewMagickWand();
$dwand = NewDrawingWand();
$pwand = NewPixelWand();

PixelSetColor($pwand, "white");
DrawSetFont($dwand, "/usr/share/fonts/default/TrueType/cour.ttf");
DrawSetFontSize($dwand, 20);
DrawSetFillColor($dwand, $pwand);

DrawSetGravity($dwand, MW_SouthGravity);

MagickReadImage( $resource, 'small_flower.jpg' );

if( MagickAnnotateImage( $resource, $dwand, 0, 0, 0, "Flower" ) )
{
  header( 'Content-Type: image/gif' );
  MagickEchoImageBlob( $resource );
}
else
{
  echo MagickGetExceptionString($resource);
}

?>

Example 3: Crop an Area

<?php

// convert flower.jpg -crop 128×128+50+50 flower_crop.jpg

$resource = NewMagickWand();

MagickReadImage( $resource, 'small_flower.jpg' );

if( MagickCropImage( $resource, 128, 128, 50, 50 ) )
{
  header( 'Content-Type: image/gif' );
  MagickEchoImageBlob( $resource );
}
else
{
  echo MagickGetExceptionString($resource);
}

?>

Example 4: Rotate

<?php

// convert flower.jpg -rotate 45 flower_rotate45.jpg

$resource = NewMagickWand();
MagickReadImage( $resource, 'small_flower.jpg' );

MagickRotateImage( $resource, null, 45 );

header( 'Content-Type: image/gif' );
MagickEchoImageBlob( $resource );

?>

Example 5: Resize

<?php

// convert flower_original.jpg -resize 640×480 flower.jpg

$resource = NewMagickWand();
MagickReadImage( $resource, 'small_flower.jpg' );

MagickResizeImage( $resource, 100, 100, MW_QuadraticFilter, 1.0 );

header( 'Content-Type: image/gif' );
MagickEchoImageBlob( $resource );

?>

Example 6: Apply Resharp Filter

<?php

// convert flower.jpg -unsharp 1.5×1.0+1.5+0.02 flower_unsharp.jpg

$resource = NewMagickWand();
MagickReadImage( $resource, 'small_flower.jpg' );

MagickUnsharpMaskImage( $resource, 1.5, 1.0, 1.5, 0.02 );

header( 'Content-Type: image/gif' );
MagickEchoImageBlob( $resource );

?>

Example 7: Compress JPG

<?php

// convert flower.jpg -quality 80% flower_quality.jpg

$resource = NewMagickWand();
MagickReadImage( $resource, 'small_flower.jpg' );

MagickSetFormat($resource, 'JPG');
MagickSetImageCompression($resource, MW_JPEGCompression);
MagickSetImageCompressionQuality($resource, 80.0);

header( 'Content-Type: image/gif' );
MagickEchoImageBlob( $resource );

?>

Tags: , ,

2 thoughts on “PHP ImageMagick MagickWand Examples

  1. Kiran

    I want to add some php magickwand fucntion for sharpening an image. I tried with MagickUnsharpMaskImage but am not able to get proper combinations to be used for the 3 parameters of raduis,amount and threshold. Please anyone suggest me the solution.

  2. Mike Fulton

    I got all excited about the MagickWand for PHP library… then I discovered that it needed to be installed as a PHP extension. My sites are on a shared hosting setup, so I don't have any way to install additional PHP extensions.

    .cry

    I guess to do what I want, I'm gonna have to write a C program and use the original MagickWand library with that. Fortunately, I do have SSH access and can compile things on the server.

Leave a Reply

Your email address will not be published. Required fields are marked *