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

Malayalis may not have valued Nedumudi Venu, but ChatGPT did

Back in the late 1980s (exactly in 1988, according to some searches)—there was a much-loved Malayalam serial called Mandan Kunju . Ever since then, our family developed a deep appreciation for Nedumudi Venu. His acting was often compared to that of Sivaji Ganesan, who was widely celebrated at the time. There were even debates about how Nedumudi was surpassing Sivaji with his unmatched natural style. Even in his 30s, Nedumudi would appear with grey hair, convincingly portraying elderly characters. About 35 years ago, I happened to watch a film in which Nedumudi Venu actually played the lead role—something quite rare in his career. I couldn’t remember the plot or the supporting cast, but what stayed with me vividly was the setting. The film had been shot in Munnar’s Madupetty Estate, with the estate school serving as one of the locations. I also remembered a Carnatic-influenced song filmed outdoors in the estate, which left a strong impression on me. For years, I tried to rediscover ...

The American College, Madurai - Famous alumni

I'm trying to constantly update this list. It should have been titled "remarkable" or "notable"--instead of "famous". Prof. Kalyani - Social Activist ( link ) Mahendran (birth name: Alexander) - 54PUC?? - film director of Mullum malarum, etc Prof. Solomon Pappiah - 53TAM?? ( link ) Dr. R. ​Prabhakar Vedamanickam - 80ECO?? - Professor, Music Director, Font Creator, known for creativity M. Vallalar IAS - 84ENG?? - Collector of Dindigul District Bala - 83TAM?? - film director of Sethu, etc Vivek (birth name: Vivekanathan) - 78COM59 - comedy actor Ramki - 78?? - actor Thota Tharrani - 64PUC?? - art director Shihan Hussaini - 84ZOO?? - Karate expert and actor Karu. Palaniappan - 90ENG?? - film director of Partiban Kanavu, etc Ram (birth name: Rama Subbu) - 93TAM?? - film director of Katrathu Tamil, etc Shanmugharajan - 99TAM?? - actor (acted in Virumandi, etc Jacob Sahaya Kumar Aruni - 93PHY?? - Chef and VJ

Attraction, Status, and the Curious Pattern Behind Some Marriages

There has been a lot of discussion lately about celebrity divorces. As someone who closely observes human behavior, I have been thinking about a possible pattern behind some relationships. Before explaining my thought, I should mention something about my own background. From childhood, I was never particularly drawn to the idea of physical beauty. In fact, I even had a somewhat negative view of it. This may have come from the value systems around me while growing up. I also remember a religious gathering where someone said that a woman's beauty reflects her inner mind. Even at that time, I instinctively rejected that idea. Over the years, however, I have started noticing certain patterns in couples. When couples walk closely together, sit near each other, and interact affectionately in public, there often seems to be an interesting dynamic. In many such cases, the woman appears more attractive than the man. Sometimes she also seems more powerful in...