1

I have the following code to create a png swatch with a drop shadow (to dynamically insert in a PDF file using FPDF.)

$shadowSwatch = $pm->clone() ;
$shadowSwatch->setImageBackgroundColor('#000000') ;

//Angle & Offset of Drop Shadow based on photoshop settings
$angle = deg2rad(45) ;
$xOffset = round(sin($angle) * 18, 0) ;
$yOffset = round(cos($angle) * 18, 0) ;

//Shadow Image seems to take extra time
ini_set('max_execution_time', 300) ;
$shadowSwatch->shadowImage(8, 8, $xOffset, $yOffset) ;

//Overlay original image on its shadow
$shadowSwatch->compositeImage($pm, Imagick::COMPOSITE_OVER, 0, 0) ;

//Attempts at forcing consistent output
$shadowSwatch->flattenImages();
$shadowSwatch->setImageColorspace(13);
$shadowSwatch->setImageDepth(32);
$shadowSwatch->setImageFormat('PNG32');

//Save Swatch
$shadowSwatch->writeImage($swatchDestination) ;

My problem is that I need to have it consistently output the same bit depth on writeImage ... and it isn't. Occasionally it outputs 64 bit PNGs when FPDF can only handle 32bit (8 per RGBA.)

Any advice on getting consistent bit depth from Imagick PNGs will be greatly appreciated!

danielml01
  • 441
  • 2
  • 10
  • Posslbly the writer is confused by the setImageDepth(32) which means 128-bit RGBA. Try using (8) or just omitting the line. – Glenn Randers-Pehrson Jun 01 '15 at 23:11
  • @GlennRanders-Pehrson Thanks for your response. Unfortunately, if I follow your advice, it still gives inconsistent bit-depth. By setting setImageDepth to 8 or removing it, some images output as 8-bit depth and some as 32-bit depth. I'm checking bit depth by looking at the image properties in windows. My issue is the inconsistency. The 8-bit files render with solid black shadows (very ugly) rather than semi-trans shadows in fpdf. – danielml01 Jun 02 '15 at 12:22
  • The answer, but not the question is the same as http://stackoverflow.com/a/19664192/778719 – Danack Jun 03 '15 at 00:39

1 Answers1

3

I found the answer. To provide consistent bit depth in PNGs using imagick replace:

$shadowSwatch->setColorspace(13);
$shadowSwatch->setImageDepth(32);

with:

$shadowSwatch->setOption('png:color-type', 6);
$shadowSwatch->setOption('png:bit-depth', 8);
danielml01
  • 441
  • 2
  • 10