Page 1 of 2

PNG looses transparency on rotate

Posted: 2012-02-09T06:33:18-07:00
by raffaele181188
I'm using ImageMagick through PHP. My host service uses ImageMagick 6.5.4-7 2010-11-10 and when I rotate a PNG with "none" (or "transparent") background, I get a black one instead. I saw there are lots of bugfixes about PNG in changelog around the beginning of 2011 and I think this can be related, because on my Lucid box (ImageMagick 6.5.7-8 2010-12-02 Q16) everything works fine. Here is the PHP code that demonstrate this. Maybe I am missing something? (Note: the bug happens only on the host machine. On my development machine it works as expected)

test.php

Code: Select all

<?php
if (isset($_FILES["image"])) {
  $image = new Imagick($_FILES["image"]["tmp_name"]);
  $image->setImageFormat("png");
  $image->rotateImage("none", 10);
  header("Content-type: image/png");
  echo $image;
} else { ?>

<html>
<body>
<form method="post" enctype="multipart/form-data" action="test.php">
<input type="file" name="image" />
<input type="submit" />
</form>
</body>
</html>

<?php } ?>

Re: PNG looses transparency on rotate

Posted: 2012-02-09T06:58:30-07:00
by glennrp
I haven't tested this but you might be able to work around the problem on old versions of IM by using format png32 instead of png.

Re: PNG looses transparency on rotate

Posted: 2012-02-09T10:02:42-07:00
by Drarakel
Not sure what happens as we have no sample file.
One problem with some old versions was that a false background color (bKGD chunk) could get set. For example, when reading an indexed transparent PNG (with background color from the palette), it could get written as PNG32 as default, but the background color was set to 0,0,0=black. Sounds a bit like the described problem.
If it's such a case (and if the png32 format doesn't help), then I fear, you can't do much with that old IM version. You could add a "-background white" (don't know the PHP equivalent). Could be better than black at least.

Re: PNG looses transparency on rotate

Posted: 2012-02-09T17:36:06-07:00
by raffaele181188
This is the error
Image
Unfortunately "png32" doesn't work either

I just noticed one tricky issue. Both the correct and the wrong image aren't valid png, ie I serve them using echo $image, and the browser can correctly show them (I set content-type to image/png). Then I save them on disk and am not able to open the local files with any program. This is quite strange (and happen with all browsers) because if the stream weren't really a valid PNG, the how could the browser engine render it?

Re: PNG looses transparency on rotate

Posted: 2012-02-09T18:00:26-07:00
by fmw42
Perhaps you should provide your input image, so others can check and test it.

Re: PNG looses transparency on rotate

Posted: 2012-02-09T18:49:46-07:00
by anthony
If your input image does not have an alpha channel you will need to add one before doing the rotate with transparency background.

Re: PNG looses transparency on rotate

Posted: 2012-02-10T06:37:20-07:00
by raffaele181188
It happens with all images, no matter which size, format etc... Anyway, this is the image
Image

Can somebody point me to the code for adding the Alpha channel in PHP? This is the doc. I tried

Code: Select all

$image->setImageFormat("png32");
$image->setImageAlphaChannel(imagick::ALPHACHANNEL_ACTIVATE);
but it gives me this error
ImagickException: Unable to set image alpha channel in /data/www/test.php on line 5 Call Stack: 0.0003 321424 1. {main}() /data/www/test.php:0 0.1183 321912 2. Imagick->setimagealphachannel() /data/www/test.php:5

Re: PNG looses transparency on rotate

Posted: 2012-02-10T07:07:27-07:00
by glennrp
Setting the format to png32 only adds an alpha channel during the PNG-writing process.
As was said earlier in the thread, you must add the alpha channel before rotating the
image. I don't use this API but I would try "setImageMatte(true)", since on the
commandline it's "-matte" on older versions.

Re: PNG looses transparency on rotate

Posted: 2012-02-10T07:19:24-07:00
by raffaele181188
I already tried it. It doesn't work

Re: PNG looses transparency on rotate

Posted: 2012-02-10T10:49:09-07:00
by fmw42
Add the equivalent of -background none before the rotate.

This works fine for me in command line mode


convert androidsad.png -background none -rotate 30 androidsad_tmp1.png

Re: PNG looses transparency on rotate

Posted: 2012-02-10T11:20:07-07:00
by raffaele181188
Sorry, I want to remark a thing. The code I posted works flawlessly on my machine. It doesn't work on my deployment machine, which happens to have a slightly older version (2 months) and seems that in within those months a lots of bugs regarding PNG were fixed. But the code does work!

Re: PNG looses transparency on rotate

Posted: 2012-02-10T14:57:26-07:00
by Drarakel
OK. Seems to be a problem partly because of Imagick..? Because at the commandline, it should work with that file and with the background color "none" - even with that old IM version, I think.
Do you know the PHP/Imagick version of your host? Did you try to set the background color with a separate line - with perhaps.. $image->setBackgroundColor(new ImagickPixel('#00000000'))?

Re: PNG looses transparency on rotate

Posted: 2012-02-10T15:38:29-07:00
by raffaele181188
These are the version involved. I tried your suggestion but it doesn't work
I'm using ImageMagick through PHP. My host service uses ImageMagick 6.5.4-7 2010-11-10 and when I rotate a PNG with "none" (or "transparent") background, I get a black one instead. I saw there are lots of bugfixes about PNG in changelog around the beginning of 2011 and I think this can be related, because on my Lucid box (ImageMagick 6.5.7-8 2010-12-02 Q16) everything works fine

Re: PNG looses transparency on rotate

Posted: 2012-02-10T16:35:27-07:00
by Drarakel
OK. But I asked:
Do you know the PHP/Imagick version of your host?
You're using the Imagick syntax, aren't you?

Re: PNG looses transparency on rotate

Posted: 2012-02-11T02:10:59-07:00
by raffaele181188
Oh sorry. I don't know the PHP extension version, if that's what you need. And I don't know how to get it on the host machine. But I thought this doesn't matter: isn't the extension just a thin wrapper around the native code?