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 128x128+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 640x480 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.5x1.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: , ,

Leave a Reply

Your email address will not be published.