0

I am using the PHPImageWorkshop library to layer up transparent images, (which is one of the things it is supposed to excel at, so I must be doing it wrong)!

When I just specify the base layer, (a transparent PNG logo), everything works as expected, see code:

<?php
use PHPImageWorkshop\ImageWorkshop;
require_once('lib/PHPImageWorkshop/ImageWorkshop.php');

$base_layer = ImageWorkshop::initFromPath( __DIR__.'/img/bg.png' );

$dirPath = dirname(__FILE__).'/output/';
$filename = "file.png";
$base_layer->save( $dirPath, $filename, false, null, 95 );
?>
<img src="/output/file.png">

... the result is a transparent PNG with my specified background image, as expected, (I'm not really doing anything here except copying the image).

The problem occurs when I try to add a smaller image on top of the background layer, instead of overlaying it, it appears to overwrite my background image, replacing it completely?

Here, I try to create a small grey square, (100 x 100), and place it in the middle of my background logo, (using my existing code):

<?php
use PHPImageWorkshop\ImageWorkshop;
require_once('lib/PHPImageWorkshop/ImageWorkshop.php');

$base_layer = ImageWorkshop::initFromPath( __DIR__.'/img/bg.png' );

// ADDED 2 LINES--------------------------------------------------
$layer_two  = ImageWorkshop::initVirginLayer( 100, 100, 'CCCCCC' );
$base_layer->addLayerOnTop( $layer_two, 0, 100, 'MT' );

$dirPath = dirname(__FILE__).'/output/';
$filename = "file.png";
$base_layer->save( $dirPath, $filename, false, null, 95 );
?>
<img src="/output/file.png">

Now my background logo is no longer visible, and I have a small grey square on a transparent layer, the size that my background image was.

What am I doing wrong, please?

Thanks for looking...

Community
  • 1
  • 1
Neil Hillman
  • 355
  • 4
  • 17

1 Answers1

0

I figured this out after 24 hours trying everything... it wasn't my code, it was the PNG image I was using for my background layer that was at fault.

It had been saved previously as an indexed color file, (instead of RGB), then re-saved as a PNG, so it was in the wrong color mode.

I eventually tried my same code using a stock PNG image, and everything worked fine!

Neil Hillman
  • 355
  • 4
  • 17