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

Google used PHP?

I wanted to know what are the pages in Google are powered by PHP . I have tried various keywords including "inurl"--but it produced huge list. So I tried "filetype:php" and it worked very nice. The real keywords are php filetype:php site:google.com Google used PHP?

Work avoiders on the rise

roughly 80% of the effects come from 20% of the causes --  80–20 rule / Pareto principle One popular store (that is known for its less selling price, 6-storied air-conditioned showroom) is situated to close to our home. But, Heleena is allergic to it. Reason? Even though there are at least 50 sales people per floor, only 1 or 2 people will be working and responding to the customers' queries. In another shop, I even noticed that the owner was serving customer when many staffs were cleverly avoiding the show! I'm finding the same problem everywhere. Who're affected here? Obviously the people who're actually working--they will have to handle more work that the clever people have avoided. Two common gestures/characteristics I have noted in these clever people: 1. They'll pretend to be absolute ignorant (These people's agenda are merely to avoid works, but not to take credits.), 2. They'll create politics on anything (These people's agenda ar...