php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69734 transparency sometimes works for PNGs
Submitted: 2015-05-30 20:43 UTC Modified: 2015-06-01 15:27 UTC
From: degnan dot 68k at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: GD related
PHP Version: 5.4.41 OS: MacOS
Private report: No CVE-ID: None
 [2015-05-30 20:43 UTC] degnan dot 68k at gmail dot com
Description:
------------
Two PNG files handled the same way, and one works with transparency and the other does not.  The included code cuts a transparent "hole" in each file, but only one has the red back ground show through.

I'll be happy to look into patching it, but I need to confirm it's a problem.  It show this behavior on all of my machines, but I cannot determine if it's a PHP or a libgd issue.



Test script:
---------------
<?php
sometimesworks("https://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=12&size=400x400&maptype=hybrid", "google");
sometimesworks("https://lh4.googleusercontent.com/UQvV_dpBa3_rVf25pvLXKD3OwzF4FtPnHBHkzdWqjCQ5mlFqcFfId9echIgDMv_xYRRYzLaKEXphw7g=w2447-h1106", "myhouse");

function sometimesworks($p_image, $p_prefix)
{
$image_top =imagecreatefrompng($p_image);
$brush = imagecolorallocate($image_top, 0, 0, 0);  //create a black brush
imagefilledpolygon($image_top, explode(",", "10,10, 120,22, 80,280, 200, 191"), 4, $brush);  //fill a polygon
imagecolortransparent($image_top, $brush);  //turn the black to be transparent
imagepng($image_top, './'.$p_prefix.'.transparent.png');  //save the file to confirm that it is working.

//create a big red square
$image_bottom = imagecreatetruecolor(imagesx($image_top), imagesy($image_top));
$red = imagecolorallocate($image_bottom, 255, 0, 0);
imagefill($image_bottom, 0, 0, $red);
imagepng($image_bottom, './'.$p_prefix.'.red.png');

//merge the top onto the bottom.
$out = imagecreatetruecolor(imagesx($image_top), imagesy($image_top));
imagecopy($out, $image_bottom, 0, 0, 0, 0, imagesx($image_top), imagesy($image_top));
imagecopy($out, $image_top, 0, 0, 0, 0, imagesx($image_top), imagesy($image_top));
imagepng($out, './'.$p_prefix.'.out.png');
}

Expected result:
----------------
Both files should have red seen through the alpha mask holes in the PNG images.

Actual result:
--------------
One file shows transparency, and the other does not; however, the code is identical as function.  Only the files are different.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-31 14:53 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2015-05-31 14:53 UTC] cmb@php.net
I can't access the second image (403 Forbidden). Please double-check the URL, and perhaps upload the image somewhere else.
 [2015-06-01 15:27 UTC] cmb@php.net
-Status: Feedback +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-01 15:27 UTC] cmb@php.net
degnan dot 68k at gmail dot com wrote:

| The PNG file seems to have had bad header information, and I've
| confirmed that it is not a PHP problem or GD problem.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 10:01:28 2024 UTC