Search code examples
phpimagick

Setting color depth with Imagick


I'm assembling a bunch of .tif using Imagick. Everything is going good, except that the color depth of the output image is always reduced (16 on the input, 1 on the output). I'm using setImageDepth() during the process, but it looks like it has no effect at all. Here's a snippet:

$imagick = new Imagick();
foreach ($_pile_of_tiles as $_index_to_append) {
    $_tmp_buffer = new Imagick();
    $_tmp_buffer->readImage($_index_to_append . ".tif");
    $_imagick->addImage($_tmp_buffer);
}
$_imagick->resetIterator();
$_appended_images = $_imagick->appendImages(true);
$_appended_images->setImageFormat("tiff");
$_appended_images->setImageDepth(16);
file_put_contents("output.tif", $_appended_images);

That gives me a 1-bit image. I tried doing this using the command-line, and it works fine (-depth 16).

Does someone encountered a similar issue?


Solution

  • After looking at the code for the underlying ImageMagick library, apparently this works:

    $imagick = new Imagick();
    $imagick->newPseudoImage(200, 200, 'xc:white');
    $imagick->setType(\Imagick::IMGTYPE_TRUECOLOR);
    $imagick->setImageFormat('tiff');
    $imagick->setImageDepth(16);
    $imagick->writeImage("./output.tif"); 
    
    system("identify output.tif");
    

    Output is:

    output.tif TIFF 200x200 200x200+0+0 16-bit sRGB 241KB 0.000u 0:00.000
    

    No. That's not documented anywhere in the ImageMagick docs. Woo.