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

I'm Zend (PHP 5) Certified Engineer:-)

As of January 13, 2007, I'm ZCE/ Zend Certified Engineer (PHP 5)

Prediction: Expensify will crash through its insane question-based hiring process

 I have huge respect for  David Barrett for his technical background. But, his company, Expensify is following a modern  psychometric test called a  question-based hiring process . The company claims to look for candidates with 'Talent, Ambition, and Humility'. The psychometric test was a fad in India about 20-years ago and the companies that followed the hiring approaches are no more and some have changed the approaches to survive. I just thought of blogging my thoughts here to see how Expensify evolves.

Save American College, Madurai

This post was written in 2008 and outdated now. For update, please check Save American College, Madurai (Update 2011) Update (2011-01-16) : Save American College, Madurai (Update 2011) I'm highly privileged to have 3 degrees (B.Sc. (Spl. Physics), PGDCA and MCA) from The American College, Madurai, South India . Unlike other "commercial" colleges, American College has given room for poor students and uplifted them. And unlike other "elite" colleges who'd give seat only for "intellectuals", American College has produced geniuses. In the recent months, the saddening thing is that the college is under divide (Principal Vs. Bishop). Here is the email I sent to alumnae lately informing about the informations that I received about the developments: Update (2011-01-16) : Save American College, Madurai (Update 2011) All: I was thinking that the " Save American College " campaign was a FUD . But, when I tried to understand the problem through my