Imagick: Maintain (fake) transparency when saving as JPEG

I haven't blogged in a while (have been busy you know), so I've decided to share this small piece of knowledge I've obtained by experimenting. I wrote a small test app (it's for a feature of the next version of Zend Server - maybe I'll share it one day when the API is stable), which does some image manipulation with the ImageMagick extension.

For those of you who don't know ImageMagick allows one to preform pretty cool stuff on images - except for the usual drawing, conversion, rotation, rescaling etc., it also exposes some API to easily preform neat effects, like drop shadow, round corners and my newest favorite (apparently only available in the very latest builds of the extension) - the Polaroid effect.

In his blog Mikko Koppanen, the author of the ImageMagick PHP extension, shows how to create drop shadows (as well as other neat things - you should check out his blog!), but in his examples Mikko will always save as PNG, which is something I dare to say most web users will not do, and prefer saving as JPEG.

Problem with many of those effects, is that they leave parts of the image as transparent. When saving the picture as JPEG (as I do, since saving as PNG produces too big files), these transparent areas appear as black.

So after some experimenting, I've found out that the way to work around this is to composite another opaque layer as your background layer, filled with your background color of choice (white in my case). You will of course loose the ability to place the picture on other background colors and still have a nice "transparency" look - but as long as you stick to the background color you've set, it will look great.

Here is a code sample producing the same thumbnail + drop shadow as in Mikko's example, but saving it with white matte color as JPEG:

PHP:
  1. <?php
  2.  
  3. $bgColor = '#ffffff'; // End result will have a white background
  4.  
  5. /* This was taken from Mikko's example */
  6. $im = new Imagick( 'strawberry.png' );
  7. $im->thumbnailImage( 200, null );
  8. $im->roundCorners( 5, 5 );
  9.  
  10. $shadow = $im->clone();
  11. $shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
  12. $shadow->shadowImage( 80, 3, 5, 5 );
  13. $shadow->compositeImage( $im, Imagick::COMPOSITE_OVER, 0, 0 );
  14.  
  15. /* My addition: clone the entire image again to create the background layer */
  16. $bg = $shadow->clone();
  17.  
  18. /* I'm using colorFloodFiilImage with high tolerance to paint it all white - maybe there are 'cleaner' ways to do it though */
  19. $bg->colorFloodFillImage($bgColor, 100, '#777777', 0, 0);
  20. $bg->compositeImage($shadow, Imagick::COMPOSITE_OVER, 0, 0);
  21. $bg->setImageFormat('jpeg');
  22. $bg->flattenImages();
  23.  
  24. /* Display the image */
  25. header( "Content-Type: image/jpeg" );
  26. echo $bg;

While there's another step in the way, and the image will only look good on white backgrounds, you can now save it as a JPEG file with good compression and acceptable file size.

How much is listening to your customers worth?

I normally don't write about work. The reason is that I feel that the slight chance that someone might feel I'm being biased towards a product that comes from the company I work for and dismiss my thoughts as "guerilla marketing" is not worth it.

However, I'm going to make an exception - and that's because I prefer selling Zend here rather than doing it on Lukas Smith's blog :)

Lukas raises the question of what commercial PHP distribution should be used as an alternative to RHEL outdated packages. My answer on that would be, surprisingly - use Zend Server! (well, ...once it's out of beta, of course).

Lets put the features and SLA you get from Zend Server aside for a moment.

The real reason I think you should use Zend Server is because the Zend Server product manager (hey, that's me!) reads your blog. I'm serious about this.

I'm not sure I can quantify this, but I think that a vendor that listens so closely to what potential users (and the community) has to say is worth quite a lot in the long run. And yes, Zend has not been perfect in listening to the community - but I can honestly and whole-heartedly say that we are trying harder. The recent feedback on Zend Server gives me the feeling that we are doing ok too.

Finally, it’s out: Zend Server

I normally try not to write about work related stuff... but this is a special occasion.

Zend Server is finally out for public beta. \o/

I was working so hard on this for the last year, It kind of feels like I've just crapped an Elephpant ;)

Seriously now, I really like this product. I think it has great potential. I know a bunch of very good people who worked very hard on it, and deserve every bit of gratitude. We went over some rough times at Zend and we still were able to release this wonderful product! I'm so proud... :)

Attending Adobe Max next week

As part of my job at Zend, I was invited by Adobe to Adobe Max in San Francisco - how cool is that? It's a huge conference (thousands of participants - nothing like any PHP conference I know!) with so many presentations to sit in it's just hard to choose.

Of course, I am no designer and tend to stick to the server side - so for me choosing was easier, but still confusing.

In any case if you are there, or in down town San Francisco, come and say hi!

ZendCon 2008 Slides

Well, ZendCon is over and it was much fun! I got home today (well, does 4:30 am count as "today" ?) and am still very tired - I had to stay around after the conference for some meetings (yes, the title "manager" causes some PITA even if you do not really manage anyone) which was a bit exhausting but fruitful never the less.

I gave this presentation about Zend Platform:

It's the first time ever I'm giving a presentation about proprietary Zend technology in an open-source conference so I was a bit nervous - but to my surprise I got a full room (I estimate some ~100 people were there, and only a few Zenders) and there seemed to be a lot of interest. In general this ZendCon felt a bit more "business-oriented" than usual, but still had a good mix of community and hacker-spirit to it.

Another thing is that Siddhartha - our VP of Sales for North America actually HUGGED me after the talk. He was sitting in the room and the guy knows a lot about selling Zend Platform - but I suppose that hearing the value of the different features and some good example use cases for Zend Platform from a technical perspective gave him and the rest of the sales team some good insight into what customers are looking for in such a product.

Anyway enjoy the slides and if you have questions just post a comment. I will probably post some more about the previous week - if only I will be able to get my hands off my new iPod Touch ;)