Here is an example on how to compose two images into a single one. The Imagick class utilises the exception handling model introduced in PHP5 and thus we will do that as well. Let's presume, that we have a directory in our filesystem, which contains our program and the two images we want to operate on.
<?php
try
{
if (TRUE !== extension_loaded('imagick'))
{
throw new Exception('Imagick extension is not loaded.');
}
if (TRUE !== class_exists('Imagick'))
{
throw new Exception('Imagick class does not exist.');
}
$dir = dirname(__FILE__);
$glasses = new Imagick();
if (FALSE === $glasses->readImage($dir . '/glasses.png'))
{
throw new Exception();
}
$face = new Imagick();
if (FALSE === $face->readImage($dir . '/face.jpg'))
{
throw new Exception();
}
$face->compositeImage($glasses, Imagick::COMPOSITE_DEFAULT, 10, 20);
$face->flattenImages();
$face->setImageFileName($dir . '/face_and_glasses.jpg');
if (FALSE == $face->writeImage())
{
throw new Exception();
}
}
catch (Exception $e)
{
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
exit(0);
?>
Also a couple more words on the Imagick::COMPOSITE_DEFAULT argument. The images we are composing together are separate layers. Not only can we put them in specific order, but we can also choose the way we want them to interfere with each other. And here comes the second argument of the compositeImage method. It can be given either as a constant or as the integer value of that constant. You can use the reflection API of PHP5 to get the list of them.
<?php
Reflection::export(new ReflectionClass('Imagick'));
?>
Just look for COMPOSITE_* constants in the "Constants" section.