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

Storing unicode texts in MySQL with phpMyAdmin

Today, I've received a personal mail/request from Sivanantham Hemamalini, working for IT leisure in Singapore. Since I was in company when received the mail, I couldn't answer immediately. If I understand the question right, it is about inputting Unicode texts especially Tamil in phpMyAdmin. PhpMyAdmin 's default characterset is iso-8859-1 and so if we enter anything in the form, browser will convert it into numerical html entities. Say for example, if we enter தமிழ் and submit the form, it will convert it to & #2980;& #2990;& #3007;& #2996;& #3021; . Because of this browser's behavior, it will be difficult to store the Unicode text as it is. Solutions Immediate solution I could think of is changing or forcing the browser's character encoding into utf-8. In Mozilla Firefox, it can be set via View -> Character Encoding -> Unicode (UTF-8) Another elegant solution might be changing the phpMyAdmin configurations so that it send...

Spotify's Greenroom Vs. Clubhouse - Missing Text Chat and Other Impacts

We have Clubhouse clones from Twitter, Facebook, Reddit, and now Spotify. Unlike others, Spotify seems to target directly into the Clubhouse audience/members. It is extremely obvious inside Clubhouse as you can find many spammy clubs and rooms created to attract the Clubhouse users. Greenroom Vs. Clubhouse I was stumbled upon one such room Clubhouse influencers are jumping off to Greenroom? - All things Hollywood . Few takeaways from the room are: Text chat is not available in Clubhouse, whereas it is available in Greenroom. This is the most talked-about USP. The recording feature is not available in Clubhouse and the same is available in Greenroom. Spotify has opened up a creator fund for Greenroom creators in the USA. However, I think, there are better indirect monetization options available for Clubhouse users. Stream's chat component & million-dollar opportunity? As I tweeted , Clubhouse can quickly bring the text chat feature by using Stream's chat component . If they...

Valen Smith, the English teacher videos are missing!

I vaguely remembered that sometimes around 2009, engVid on YouTube was very popular. I especially liked a teacher by name Valen Smith's videos. She was not only beautiful but also very good at explaining the English usage. I also vaguely remembered to check her channel ValenESL around 2015. But, not sure what happened to her or engVid, all her videos are sadly gone. I have also noted that many people on Twitter were asking the same. Not sure what happened. Some people seem to have reuploaded her videos and YouTube continuously removing them with a note "This video contains content from LearnVid, who has blocked it on copyright grounds." Strangely though, on Facebook, some people have managed to create a fake account using her old channel name ValenESL.