8

I use PHP and Imagick to change the color of a transparent PNG. The image in the PNG is a simple shape with a transparent background.

I use the colorizeImage function to change the color.

$img = new Imagick("shape.png");
$img->colorizeImage("#99ccff",0.0);

The problem is that Imagick show a dark version of my HEX code (#99ccff)?

Is there a way to get the exact color (#99ccff)?

(my PNG is PNG 32 - and the shape is black)

http://www.2shared.com/photo/N3rGdoHG/shape3.html

j0k
  • 22,600
  • 28
  • 79
  • 90
pelelive
  • 617
  • 1
  • 6
  • 14

3 Answers3

5

I thought I would answer this question despite that it is old. This is for anyone else having this problem.

I solved this for a project I am working on by simply using "Clut" instead, like so:

$img = new Imagick("shape.png");
$clut = new Imagick();
$clut->newImage(1, 1, new ImagickPixel('#99ccff'));
$img->clutImage($clut);
$clut->destroy();

Hope it helps anyone else having this issue.

Eric
  • 7,787
  • 5
  • 19
  • 34
MrE
  • 1,124
  • 3
  • 14
  • 28
  • Two things: 1) this should be marked as the correct answer as it entirely avoids the "darkening issue" the original question referenced... which I also experienced and how I ended up here. 2) I'm 100% certain the $image you use above was supposed to be $img. Great great answer. Thank you MrE. – Art Geigel Mar 05 '15 at 22:54
  • @ArtGeigel agreed I edited the typo. It should be img. – Eric Nov 18 '16 at 16:54
2
 $img = new Imagick("shape.png");
 $img->colorizeImage("#99ccff",0.0);

That second parameter is opacity. If you set it to 1.0, it will match #99ccff 100%. You can set it to 0.5 to meet 50% over the original layer, etc:

 $img = new Imagick("shape.png");
 $img->colorizeImage("#99ccff", 1.0);
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • 1
    Thank you for your reply. This still gives me a darker color. I had to change the 1.0 to 1 to get it work, otherwise its transparent. – pelelive Nov 19 '11 at 09:21
  • Confirmed on my end too... had to just make it "1" and not "1.0" for it to behave as intended. – Art Geigel Jan 20 '15 at 21:29
1

You must provide opacity, and opacity value MUST be integer 1,

$img->colorizeImage('#99ccff', 1); 

or it does not work, i have tested a bit and i think to work with transparency you need to provide alpha channel.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • Thank you for your reply. This still gives me a darker color, do you have any example to change the alpha? – pelelive Nov 19 '11 at 09:25