1

I am using Imagemagick with PHP and want to get the position of the layer (x,y) but dont know how.

I read the PSD file in PHP and read every layer in it like this:

for ($i = 0, $num_layers = $im->getNumberImages(); $i < $num_layers; ++$i) { ...

1 Answers1

6
<?php

for ($i = 0, $num_layers = $im->getNumberImages(); $i < $num_layers; ++$i) {
    $im->setImageIndex($i);         //this
    $im->setIteratorIndex($i);      //or this is kinda redundant
    $pagedata=$im->getImagePage();

    print("x,y: " . $pagedata["x"].", ".$pagedata["y"]."<br />\n");
    print("w,h: " . $pagedata["width"].", ".$pagedata["height"]."<br />\n");

    //export layer
    //$im->writeImage('layer_' . $i . '.png');
}

?>
John
  • 155
  • 2
  • 15
micha
  • 1,036
  • 8
  • 12
  • 1
    For even more metadata use foreach($im->getImageProperties("*") as $k => $v) print("$k: $v
    \n");
    – micha Jul 24 '11 at 14:35
  • i already use getImageProperties but how to fetch also the comment of the psd file if you set one in the graphics program? –  Jul 24 '11 at 15:45
  • I'm sorry, you can't with ImageMagick AFAIK. You also can't extract slices, guides, text layers .. well .. basically nothing but rasterized layers WITH pixels (empty layers doesn't show up) .. – micha Jul 24 '11 at 16:16
  • or just plain php and reading some header of the bytecode from the image directly? –  Jul 24 '11 at 16:20
  • You could try that. There's a PSDReader class on PHPClasses ([link](http://www.phpclasses.org/browse/file/17603.html)). But I've looked over the source, but can't find any functionality which extracts the comment. – micha Jul 24 '11 at 23:56
  • I already saw that very old php class, but use my own using imagemagick. –  Jul 25 '11 at 13:43
  • Hi guys, anyone knows how to get the layers name using php? if that is possible then please help me out, any link reference or anything solution will be helpful. Thanks. – Himanshu Upadhyay Oct 26 '21 at 08:19
  • not 100% sure, but try `$im->getImageProperty('label')`. (You probably need to set the correct layer first through `setIteratorIndex`) @HimanshuUpadhyay – micha Oct 26 '21 at 13:08