Problem with PNG and alpha channels

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dileso
Posts: 6
Joined: 2009-09-30T05:14:35-07:00
Authentication code: 8675309

Problem with PNG and alpha channels

Post by dileso »

Hallo,
I have a strange problem by using imageMagick. Even when I use PHP IMagick i post it here because I think it is more related to ImageMagich then to the Interface.

The Problem:
I do create an Image with a shadow and implement this image into a PDF file by using TCPDF.
When I see the created PNG file it looks like expected. Inside the PDF the shadow becomes just a black area.
So I thought it is TCPDF but other png files work fine.
after I open my created file with photoshop and save "for web and devices" as png24 it also works fine inside the PDF
Does anybody has an idea?

here is my code

Code: Select all

$file2 = '../storage/40/test2.png';
$image = new Imagick();
$image->newImage(144,144,'','PNG24');				
$shadow = $image->clone();
$shadow->setImageBackgroundColor(new ImagickPixel('black')); 
$shadow->shadowImage(50,10,50,50);
$shadow->compositeImage( $image, Imagick::COMPOSITE_OVER, 0, 0 ); 
$image = $shadow;
$image->writeImage($file2);
this is how it looks like: (left side is after saving with PS / left side is from ImageMagick)
Image

I got the same result in the shell by using convert:

Code: Select all

 convert -size 200x200 xc:white \( +clone  -background black  -shadow 70x3+10+10 \) +swap -background none   -layers merge  +repage   test1.png
Thanks for your help
Dirk
dileso
Posts: 6
Joined: 2009-09-30T05:14:35-07:00
Authentication code: 8675309

Re: Problem with PNG and alpha channels

Post by dileso »

After long searching and testing I think it is maybe TCPDF.
It maybe has a problem with images with alpha cannels > 8 bit.

Does anybody has an idea how to set the alpha channel to 8 bit?

Thanks
Dirk
dileso
Posts: 6
Joined: 2009-09-30T05:14:35-07:00
Authentication code: 8675309

solved: Problem with PNG and alpha channels

Post by dileso »

Hi again,

it's funny to be the only one who writes into this thread :D

to add a line to change the channel depth solves the problem the image looks fine inside the PDF.

Code: Select all

$shadow->setImageChannelDepth(0,8)
http://us2.php.net/manual/en/function.i ... ldepth.php

Doe to there is "no" docs for Imagick I have no idea what the first ($channel) means.
to change it 1, 2 ,3 or 4 has no change to the image.

Anyway it works...

So if anybody has the same problem you know how to solve it and if anybody knows what the 1st parameter is standing for or knows a better documentation bleas post it here.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problem with PNG and alpha channels

Post by magick »

You probably want to use setImageDepth(8) rather than setting the channel depth.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem with PNG and alpha channels

Post by fmw42 »

there is some Imagick Documentation at http://us2.php.net/imagick
dileso
Posts: 6
Joined: 2009-09-30T05:14:35-07:00
Authentication code: 8675309

Re: Problem with PNG and alpha channels

Post by dileso »

@magick:
yes this is working too and I can understand the only one parameter :lol:

@fmw42:
this docs are the same that I had. It's nearly in all functions:
Warning
This function is currently not documented; only its argument list is available.
So you only see the arguments but you never know what they standing for.
only sometimes there are some sample codes.

Thanks Dirk
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problem with PNG and alpha channels

Post by magick »

Channels are the color components of a pixels, typically red, green, blue, and alpha. iMagick should have tokens for each channel RedChannel, GreenChannel, etc. If not, they are probably using mappings, e.g. 0 = RedChannel. With channels you can operate on one or more channels. For example, suppose you only want to sharpen the red channel of an image, you would use use something like SharpenImageChannel( image, RedChannel ).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem with PNG and alpha channels

Post by anthony »

You could flatten the PNG onto white before saving into the PDF. That way no transparency is actually involved :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
silenceseeker

Re: Problem with PNG and alpha channels

Post by silenceseeker »

anthony wrote:You could flatten the PNG onto white before saving into the PDF. That way no transparency is actually involved :-)
And how do I do that using ImageMagick?

I am having a similar problem, using "convert -flatten". Its description is posted in this thread. Flattening the PNG onto white using PS or GIMP is what I am currently doing (interactively) but this is not the desired solution. I would like to use ImageMagick's convert (or some other command?) to automate this in a batch file.

Thanks.
Post Reply