Skip to main content

Converting PSD with PHP/ImageMagick

After seeing feature rich options in Imagick PECL extension at Mikko Koppanen's (the author) website and also impressed with ImageMagick's features, I have decided to use it for the PSD to XHTML conversion website that I'm architecting and managing.

Since, the team wants programming help for converting PSD images, I have tried it (documentation is sparse on PSD handling)

Converting PSD to PNG/JPEG/etc


Note that, flattenImages() is needed for layered/multi-page PSD file.


<?php
$im = new Imagick('test.psd');
$im->flattenImages();
$im->setImageFormat('png');
$im->writeImage('test.png');
?>



Extracting PSD layers


One by one



<?php
$im = new Imagick('test.psd');
$im->setImageFormat('png');
for ($i = 0, $num_layers = $im->getNumberImages(); $i < $num_layers; ++$i) {
$im->setImageIndex($i);
$im->writeImage('layer' . $i . '.png');
}
?>


Note that, there is a better version below

In a single call with writeImages()



<?php
$im = new Imagick('test.psd');
$im->setImageFormat('png');
$im->writeImages('layer%d.png', true);
?>


Note: Second parameter of writeImages() is for adjoin

Bug

While doing so and comparing with the results of XnView, I have noted a same problem/issue as mentioned here in the forum--that is, most of the extracted layers/images are distorted. By the way, I have tried in WAMP and yet to try it in LAMP

Comments

Rinto Geroge said…
Great work Rajesh ,Thanks anyway
Rinto said…
Rajesh,I am experiencing some problem while exporting psd to png ,looses the resolution .Actually I converted 500 dpi psd to png ,but got 72 dpi png .How can keep the resolution ?
Rinto:

After second thought, you'd be better off with the command line "convert" instead of this extension

Popular posts from this blog

Gen Y workforce, a threat to Indian IT?

I belong to Gen X . I'm noticing high change in the way of thinking and approaches of Gen Y (born after 1982 or 1985) workforce. Whenever I sit for interviews, I feel getting quite old... Characteristics of Gen Y (as I noted) Freshers Everybody owns mobile phones and laptop. At least using them for 2-years. Only very few people know touch typing. Almost all are interested to start their career in programming; but they don't have any basic knowledge. Most of the people can speak in English. I'm noticing a huge change here; In our generation, many people would be having good programming skill but can't express them in English People from CS aka Computer Science background says that they don't use computer labs as they own laptop. I can remember my college days at The American College, Madurai during my PGDCA and MCA--we often used to demand more lab time. I still remember that I'd run to computer lab by 7am and would still find few guys...

Stampede and the "Dirty" "Dark" Crowd

Actor Ajith Kumar’s recent interview has sparked quite a few conversations on social media. The part that caught my attention, was his take on crowds. About 30 years ago, when I first joined a college in Madurai after growing up in other places, I experienced a few cultural shocks. Perhaps these weren’t unique to Madurai, but that’s where I first noticed them. One major thing that stood out was the behavior of crowds. For instance, if you suddenly see people rushing to board a bus, chances are there’s a pickpocket in action. During one of Madurai's annual festivals, I noticed some young men carrying water bags — not to distribute water, but to spill it on women, often on their chests. Shockingly, this was almost normalized; parents would quietly tolerate it to avoid public embarrassment, walking a little farther behind the crowd. When some women happened to witness this, they would just shoo the boys away instead of confronting them. The crowd, in such cases, became a kind of...