Finally (after a long break) I managed to force myself to update the PHP documentation and this time it was distortImage code example. Things have been hectic lately but that does not quite explain the 6 months(?) break between this and the previous post. As a matter of a fact there is no excuse for such a long silence so I will try to update this blog a bit more often from now on.
Back in the day I used to blog the examples and update the documentation if I remembered but I am trying to fix this bad habit. Most of the latest examples have been updated in to the manual. In the case of the two last examples I updated the documentation first and then blogged on the subject.
I took some time to actually understand the perspective transformations properly using the excellent ImageMagick examples (mainly created by Anthony Thyssen) as a reference. The basic idea of perspective distortion seems simple: to distort the control points to new locations. Grabbing the syntax for Imagick was easy, an array of control point pairs in the form of:
The following example uses the built-in checkerboard pattern to demonstrate perspective distortion:
-
<?php
-
/* Create new object */
-
$im = new Imagick();
-
-
/* Create new checkerboard pattern */
-
$im->newPseudoImage(100, 100, "pattern:checkerboard");
-
-
/* Set the image format to png */
-
$im->setImageFormat('png');
-
-
/* Fill background area with transparent */
-
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
-
-
/* Activate matte */
-
$im->setImageMatte(true);
-
-
/* Control points for the distortion */
-
10, 5,
-
-
10, $im->getImageHeight() - 20,
-
10, $im->getImageHeight() - 5,
-
-
$im->getImageWidth() - 10, 10,
-
$im->getImageWidth() - 10, 20,
-
-
$im->getImageWidth() - 10, $im->getImageHeight() - 10,
-
$im->getImageWidth() - 10, $im->getImageHeight() - 30);
-
-
/* Perform the distortion */
-
$im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true);
-
-
/* Ouput the image */
-
echo $im;
-
?>
Here is the source image:

And the result:
