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:
- array(source_x, source_y, dest_x, dest_y ... )
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 */
- $controlPoints = array( 10, 10,
- 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 */
- header("Content-Type: image/png");
- echo $im;
- ?>
Here is the source image:

And the result:

#1 by dynek on February 5, 2009 - 11:17 am
Quote
Good to see you are back! I was missing the Imagick tricks
#2 by Andriesss on February 5, 2009 - 11:59 am
Quote
Another cool trick! Keep up the good work!
#3 by Mats Lindh on February 5, 2009 - 12:25 pm
Quote
It’s good to have you back blogging again! All the projects where I’ve been involved during the last 8 months has changed to Imagick after the initial release of version 2.0 of the module.
#4 by Mikko Koppanen on February 5, 2009 - 5:39 pm
Quote
Mats,
you wouldn’t be interested in helping with the documentation? Sounds like you already have some experience with the extension.
#5 by Brent Wong on February 5, 2009 - 10:44 pm
Quote
Welcome back Mikko, nice to have you back and updating again.
#6 by Spooky on February 7, 2009 - 1:42 am
Quote
Could someone please tell me how to create a semi-transparent .png from a .jpg file?
#7 by riyuk on February 9, 2009 - 4:18 pm
Quote
welcome back.
#8 by Poldy on February 19, 2009 - 7:33 pm
Quote
I recently played a lot with the Imagick extension. – Your tutorials helped quite a bit.
Now I have a problem with SVGs: when loading a SVG with a transparent background the resulting image has a white background. The format doesn’t seem to matter. I used both renderers (default rsvg and msvg). Would be nice, If you could wrap some things about svgs up in a tutorial.
#9 by Dimitar Siljanovski on February 23, 2009 - 12:21 pm
Quote
Great.. Glad to read something new from you….
Welcome Back
#10 by Oliver on March 1, 2009 - 5:27 pm
Quote
Hey Mikko. All the best. Really love Imagick.
Have a quick question. I’m working on a cropper type script, and am having an issue with transparent png’s. I crop images to be 48×48, but if they are too small (eg. 40×40) I add a black canvas in the background and position the image on top. If they’re transparent though, I’d like to set the background color to the transparency colour for the image.
That leads me to two questions actually:
1) Is there a way to determine if the png has transparent properties? (with GD I can do so on gif’s with that imagecolortransparent function)
2) If so, is there a way to make a newImage call, and instead of passing in an ImagickPixel instance for the background color, somehow make it transparent?
If the answer to 2 is no, that’s fine, I can just use cropThumbnailImage, but in order to do so, I need to figure out if the png is infact transparent. That method takes care of the canvas colors and all.
All the best.
#11 by filamoon on March 5, 2009 - 8:23 pm
Quote
Hi Mikko, thank you for your great stuff!
I have a question about using annotateImage() to display Chinese ttf fonts. The displayed fonts are very very blurred, even with anti-aliasing. Without anti-aliasing, the display effect is even worse. I tried 12pt and 16pt. Almost the same.
Do you have any hints to solve this problem?
Thanks a lot!
#12 by Pavel on March 12, 2009 - 6:00 pm
Quote
Hi, Mikko
Can you help me to convert image to black-and-white? I couldn’t find any solution.
Thanks!
#13 by Mikko Koppanen on March 12, 2009 - 6:09 pm
Quote
@Pavel,
that should be nothing else but setting image colorspace to grayscale
#14 by Pavel on March 12, 2009 - 6:29 pm
Quote
Thanks, you saved hours of my time!
There is no imagick documentation in russian(
#15 by MrABC on March 26, 2009 - 3:35 pm
Quote
http://img240.imageshack.us/img240/8485/distort.png
Hello, i have problem by transparent background in this example.
i m useing the Windows – php_imagick_st-Q8.dll
thx for any help.
#16 by phord on March 26, 2009 - 7:17 pm
Quote
MrABC, I got the very same issue. looking forward what the cause is.
I use imagemagick 6.3.7.9.dfsg1-2ubuntu3 ubuntu amd64 package with php5-imagick 2.1.1RC1-1
#17 by phord on March 26, 2009 - 7:59 pm
Quote
just upgraded imagick to 2.2.2 from PECL. same error.
#18 by badabou on April 24, 2009 - 2:49 pm
Quote
Hello,
In fact, I just copied / pasted the example below on the mikko’s blog and I have this error :
Fatal error: Uncaught exception ‘ImagickException’ with message ‘invalid argument for option PerspectiveProjection : ‘Needs 8 coefficient values” in …
My configuration with which I have this problem is :
Windows XP
Imagick module version 2.2.1-dev (php_imagick_dyn-Q16.dll)
ImageMagick 6.5.1-6
When I use the same script on my linux server, it runs at half, there was no transparency (Resulting image as an attachment) : http://imagemagick.org/discourse-server/download/file.php?id=26
#19 by T. Nguyen on April 27, 2009 - 9:09 am
Quote
I am very glad to have found Mikko’s blog since I am looking into using Imagick to replace gd code that I am currently using in PHP. Great postings. I have a simple question and haven’t found an answer yet. I am using COM in PHP to evoke an object that returns a pointer to an RGB bitmap (24-bit, 8-bit per color). Is there a way to construct an Imagick from a bitmap in PHP, without having to write a php extension?
Thank you very much.
tom
#20 by Mikko Koppanen on May 2, 2009 - 2:05 am
Quote
T. Nguyen,
doesn’t $im = new Imagick(); $im->readImageBlob($image_contents); work?
#21 by Mikko Koppanen on May 2, 2009 - 2:06 am
Quote
badabou,
the windows binaries are old
Trackback: Web Development Blog
Pingback: My Chances.net - College admissions predictions.
#22 by Shanti on May 22, 2009 - 10:39 am
Quote
Mikko Koppanen;
This is great and can probably help me with a project I have,
do you know if this principle can be applied to convert an equirectangular image to a set of cube faces?
lets say this: http://upload.wikimedia.org/wikipedia/commons/e/ea/Equirectangular-projection.jpg
and extract 6 cube faces like this:
http://i.msdn.microsoft.com/Bb204881.cube_faces(en-us,VS.85).gif
but each cube face saved as a separate file.
I’ve been looking everywhere and I ended up on your great site, so hopefully you can help me
Thanks!
#23 by Jim on July 25, 2009 - 5:10 pm
Quote
What is better: Imagick or MagickWand? I’ve been looking all over, and I can’t really find an explanation of the differences or advantages of one or the other.
Any input would be appreciated!
#24 by Jeremy on August 16, 2009 - 6:51 pm
Quote
As an experiment to try and get used to the Imagick PHP extension I was trying to convert the IM Watermarking with Text (http://www.imagemagick.org/Usage/annotating/#wmark_text) example but what I’ve been able to accomplish with using Imagick doesn’t exactly match with that the IM CLI produces.
Pingback: Prospettive trasformazioni « Tech Blog
#25 by James on October 1, 2009 - 2:00 pm
Quote
Hey Mikko,
Another great post! I have a challenge for you that would be awesome, but im not even sure if it is possible. How about making a function that turns an image into a sketch and fills parts of it red, white and blue in the same manner of the Obama face images seen everywhere– http://tinyurl.com/y87xcv5. Probably way to complex to do, but i’d though I would ask.
James
#26 by Kreso on February 19, 2010 - 8:52 pm
Quote
Hey Mikko! Hey guys!
I am having the same issue with transparency as MrABC & phord. I’m using Windows 6.59 Q 16, Php 5.2.
I’m wondering if anyone figured out what the problem is?
#27 by Ivan on February 25, 2010 - 7:41 am
Quote
Hi!
How I can use opacity with this:
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$im->setImageMatte(true);
I’m trying to put perspective images to background images, and need some opacity. But I got black transparent rectangle about image:
http://share.devel.asmer.org.ua/darkFrame.png
without opacity:
http://perspective.devel.asmer.org.ua/render/perspectiveRenderer.php?text=change%20it
I’m tried to insert setImageOpacity before and after perspective transformation, but it isn’t helps to get opacity image without frame.
thank you for this example
#28 by g33kit on April 9, 2010 - 11:47 am
Quote
Hi, the Image after distortion have worse quality as before, is it posible to set Filter for Distortion?
or is there someway to make the quality better?
thx for any help
#29 by Eric on June 10, 2010 - 10:39 pm
Quote
Hi Mikko,
I’ve been working on a PHP script to warp images, but it seems like there is a problem with setImageVirtualPixelMethod(). For some reason, it is not applying imagick::VIRTUALPIXELMETHOD_TRANSPARENT. Instead, it defaults to VIRTUALPIXELMETHOD_EDGE, thus extending the pixels on the edge of the image outward to the borders of the actual file. (It even does this with your example above.) I started a thread on imagemagick.org, but everyone is stumped. Might you have any ideas as to how to solve this?
Here’s the thread. http://bit.ly/arszAx
Any help is much appreciated! Cheers!
#30 by neoMagic on July 30, 2010 - 3:27 pm
Quote
Hi there,
@Mikko: great post, makes things clear and sharp
@Erik (#28): we got the same problem, we managed to fix this using ImageMagick 6.5.1 with Imagick 3.0.0
Cheers!
#31 by neoMagic on July 30, 2010 - 3:28 pm
Quote
Sorry, I meant Eric instead of Erik
#32 by Anthony Thyssen on November 3, 2010 - 8:27 am
Quote
Be sure to have the very latest ImageMagick library. IM v6.6.5-0 saw a very marked improvement to the quality and sharpness of the output from the Distortion operator in ImageMagick Library. I should know I did the work on it
#33 by David on May 11, 2011 - 7:16 am
Quote
How did you come up with such tricks? It really impresses me. Keep it up!
#34 by Victoria on September 8, 2011 - 7:34 am
Quote
You seem to know a lot with codes. Where did you get these codes from?
#35 by Dianne on October 25, 2011 - 5:35 am
Quote
Sometimes I wonder if these codes are only there just for a show. Well, I know I’m wrong to think that way.
#36 by Sithu on October 29, 2011 - 11:49 am
Quote
I got this error :
Fatal error: Uncaught exception ‘ImagickException’ with message ‘invalid argument for option PerspectiveProjection : ‘Needs 8 coefficient values’ @ error/distort.c/GenerateCoefficients/811
#37 by Dianne on November 10, 2011 - 8:00 am
Quote
I tried tweaking my PC the other day and got myself a treat with the codes you shared. Thanks indeed!
#38 by very good on May 19, 2012 - 10:47 pm
Quote
i like this site ,is very good!