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

Humble Award - Dr. A.N. Sreevatsan

Dr. A.N. Sreevatsan , ENT specialist located in Adyar, Chennai is one of the gems available in medicines. No assistants in his consulting room and he'd take at least 15mins for every patients. So far I have referred many and all are happy with his approach. Every wannabe doctor should visit him personally to understand his approach. I wish him to be more famous than now. Update (2010-11-21) : Added link to Google Map Keywords : Sreevatsan, Srivatsan, ENT, Adyar, Chennai, Hospital, Doctor

Top Indian web startups

Disclaimer: This list contains some of our team's projects Sites that are useful and usable erail.in - created by a single person and serves useful to quickly find availability in Indian trains ClearTrip - seems to be inspired by many other sites like Vayama , but able to get better UI RediffMail - speed is the big boost Myntra - good idea, but poor UI redBus - good idea, but poor UI and execution Sites from The Times Group are also of good idea, but poorly executed/architected. They clearly try and target the web startup "gap" Sites that are innovative (world's first/never seen before kind) SocialAV - World's first and only antivirus building community (by our team) CSSilize - World's first and only project management tool for slicing works (by our team) jobreapr - World's first and only fixed price job search engine (by our team)

Think Different - truth and creative people behind

Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules, and they have no respect for the status quo. You can quote them, disagree with them, glorify and vilify them. About the only thing you can't do is ignore them because they change things. They push the human race forward. And while some may see them as crazy, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do. --  Think different, Apple's TV commercial Kids of Mac toys often attribute everything to Steve Jobs. But, I recently happened to read the people and story behind the inspiring "Think Different" ad . I think, everyone must read it--to be inspired.