PNG looses transparency on rotate

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?".
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

PNG looses transparency on rotate

Post 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 } ?>
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: PNG looses transparency on rotate

Post 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.
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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.
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG looses transparency on rotate

Post by fmw42 »

Perhaps you should provide your input image, so others can check and test it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: PNG looses transparency on rotate

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: PNG looses transparency on rotate

Post 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.
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post by raffaele181188 »

I already tried it. It doesn't work
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNG looses transparency on rotate

Post 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
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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!
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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'))?
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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?
raffaele181188
Posts: 7
Joined: 2012-02-09T05:51:29-07:00
Authentication code: 8675308

Re: PNG looses transparency on rotate

Post 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?
Post Reply