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

Technology prediction for 2018

Bubbles of bitcoin, blockchain, machine learning, deep learning, artificial intelligence and more People usually appreciate me for my predictions on technologies. As I already noted our team will quickly hit the edge case for any use cases and that's supposed to be the reason for my better prediction rate. And so, here are my predictions… Bitcoin bubble When Bitcoin became talk of the town/industry, I informed my boss and colleagues that I find no real use case for this digital currency--except in underground markets like drugs, etc. In fiat currencies, there is at least a way to take legal route, in case, if you've transferred the money and not received the goods. But, in Bitcoin, this is not possible at all as it is not a legal currency. So, if there is no trust, the chances that other person can cheat you by not sending the goods and or not transferring bitcoins after receiving goods. My stand above vindicated by the article Ten years in, nobody has come up with a...

RTaKeyman Bookmarklet v2.0

RTaKeyman The above bookmarklet is for English to Tamil transcription. This should work like a Input Method Editor on web page forms. The first version I tried didn't work well and so this bookmarklet version. Version 2.0 2004-12-27 1.0 2004-12-12 Usage Make a bookmarklet with the above link. Point to a web page where you want to enter Tamil characters and click the bookmarklet. This will initialize the keyman for that page. Then start typing on the forms; RTaKeyman will transcribe the characters whenever it sees a space, carriage return and tab. To-do Turning on/off keyman (i.e., to enter both Tamil and English). Help and other user interfaces. Credits Sundar for informing the necessity and for being a beta tester

Mrs. Sujatha, Marriage, and Misunderstandings

I recently watched an interview featuring Mrs. Sujatha on the Aval Vikatan YouTube channel, and I couldn’t help but write this post in response. In the video, she indirectly expresses regret about her relationship with the late writer Sujatha, implying that they lacked the kind of emotional bonding that today’s couples supposedly enjoy (?!). She also mentions that he would get angry at home, in contrast to his calm public persona. She suggests that they didn’t talk much—ironically, I vividly remember Sujatha’s own writing about the relationship between actress Shobha and Balu Mahendra, in which he quoted something his wife had said! Toward the end of the interview, she talks about the wealth and royalties he left behind, suggesting that material things don’t matter. ( It’s ironic—men spend their entire lives working to build wealth and security for their wives and families. ) What struck me even more was the comment section. Most of the viewers seemed critical of writer Sujatha, e...