1

I want to crop out a polygon (that I have there a transparent area) of an image I saved from Google Maps Static API. Then I created a second image, saved it and tried also the same script on it but with the difference that the was no effect/change on the second tried image but it's the same image. Maybe a PHP bug? I'm using PHP 5.3.3.

<?php
$image = imagecreatefrompng('map.png');
$image2 = imagecreatefrompng('map2.png');


$black = imagecolorallocatealpha($image, 0, 0, 0, 127);
$black2 = imagecolorallocatealpha($image2, 0, 0, 0, 127);


imagefilledpolygon($image, array(0,0, 20,20, 0,20), 3, $black);
imagefilledpolygon($image2, array(0,0, 20,20, 0,20), 3, $black2);

header('Content-Type: image/png');
imagepng($image);
#imagepng($image2);
?>

Image 1: 1

Image 2: 2

hakre
  • 193,403
  • 52
  • 435
  • 836
Poru
  • 8,254
  • 22
  • 65
  • 89
  • From [the manual](http://www.php.net/manual/en/function.imagecolorallocatealpha.php) "*alpha*: A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent." So you're drawing a wholly transparent polygon, and you're seeing right through it? Sounds like it's working as intended. – Conspicuous Compiler Dec 23 '11 at 23:53
  • It should replace this polygon with transparency. The first examples works also correct, here's the working result of the first image: http://i.imgur.com/91Ska.png – Poru Dec 24 '11 at 00:00
  • 1
    Drawing with a polygon which is transparent doesn't replace the area you're drawing over, but composites the underlying image with the color of the polygon, according to the alpha value. If you're seeing a solid color appear with the code you've written above, I speculate you are writing an image which doesn't have blending turned on. Try calling `imagealphablending($image, TRUE)` and `imagealphablending($image2, TRUE)` and I bet you'll find that you get the same results then. – Conspicuous Compiler Dec 24 '11 at 00:10
  • imagealphablending seemed not to work, I still get the same result. Just try my example code from above with the uploaded images. – Poru Dec 24 '11 at 01:04

1 Answers1

0

I found the solution: You have to set imagealphablending and imagesavealpha settings for the images to get this transparency working. The problem also that these images have different bits. The working images had 8bit while the not working had 24bit.

imagealphablending($image2, false);
imagesavealpha($image2, true);

This comment helped me much: http://www.php.net/manual/en/function.imagecreatefrompng.php#47083

Poru
  • 8,254
  • 22
  • 65
  • 89