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

IP to ISP/Country/City (GeoIP) using PHP

I've noted that many people are searching here about how to find out City/Country/ISP details from IP; often referred as GeoIP. Here, I've compiled my replies that once I posted to comp.lang.php Get the IP Refer http://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xml and see which whois server should be used for the whois lookup Now, do the whois lookup. e.g., whois -h whois.apnic.net 61.x.x.x Parse the results. AFAIK, it will have the ISP, City and Country info For whois lookup, may use rwhois protocol through below PHP code: <?php function whois($host, $command) { $fp = fsockopen ($host, 43, $errno, $errstr, 100); if (!$fp) { $result = $errstr . $errno . "\n"; } else { fputs ($fp, $command . "\r\n\r\n"); $result = ''; while (!feof($fp)) { $result .= fgets ($fp, 128); } fclose ($fp); } return $result; } //debug... echo whois('whois.internic.net', 'php.net'); ?> Re...

How girls ruin...?

மது, à®®ாது, சூது (liquor, girl, crookedness) - Tamil tantras list down one's reason for fall/collapse. In recent times, I'm hearing many relational breakups and personal collapse due to "girl". Some even seem to have damaged career, business, friends, family, etc. I'm thinking, how come such girls are getting cabal and ruin things... Not to blame it on them--but to blame it on guys...

Beware of Chennai Book Fair crime - mobile theft

Unfortunately, lost my iPhone 4 that was gifted my boss on Jan 9, 2011 (Sunday) at the Chennai Book Fair held at St.George Anglo Indian School (Opp to Pachayappan college, Poonamalee High Road, Aminjikarai, Chennai - 600 030). When I visited T.P.Chatram Police Station to file the complaint, I have noted about 20 mobile theft complaint from the visitors of the book fair! So, there is a big chance that a mob is targeting the book fair. Beware! Steps to be taken (general guideline) Try to reach the mobile from other phone (to confirm if it's been stolen) File a complaint with nearest police station Deactivate your lost SIM with the operator, especially if it's a post paid connection. Things to be mentioned in police complaint Handset model IMEI Cost of the mobile Last used SIM (mobile number) Date and time of loss Place of loss Alternate contact details (if the phone is recovered) Have to receive the CSR (Complaint Statement Receipt(?)) as a receipt. Chennai...