From:             jcolby at acsol dot net
Operating system: openSuse, CentOS, FreeBSD
PHP version:      5.2.9
PHP Bug Type:     GD related
Bug description:  ImageConvolution overwrites background (fix included)

Description:
------------
When using imageconvolution on an image containing alpha, the background
color on the resource image will be replaced with opaque black regardless
of any alpha or background settings. This is because of the gdimagecopy
internal to the function without the proper savealpha flags being set & no
transparent image fill after gdimagecreatetruecolor is used inside the
function. 

This is similar to bug #34992 but I've had a chance to break it open and
actually fix it.

This affects all versions of php 5, up to the latest 5.2.9 stable build,
and I wouldn't doubt it currently affects 6 as well. 

Now, I managed to fix it in my own build, but I don't know how to get it
advanced from there. This is not my realm of experience, I just had to
repair it.

Patch: php-5.2.9\ext\gd\libgd\gd.c

Add in var initialization:
/* patch */
gdImagePtr  srctrans;
/* patch */

Add after "srcback = gdImageCreateTrueColor..."
/* patch */
srcback->saveAlphaFlag = 1;
srctrans = gdImageColorAllocateAlpha(srcback, 0, 0, 0, 127);
gdImageFill(srcback, 0, 0, srctrans);
/* end patch */

Thats all it requires.


Reproduce code:
---------------
  <?php
  function makeFilter($resource, $matrix, $offset=1.0) {
        global $$resource;
        (float)$divisor = array_sum(array_flatten($matrix));
        if ($divisor == 0) {
          $divisor = .01;
        }
        return imageconvolution($resource, $matrix, $divisor, $offset) ?
true : false;
  }  
  
  $edgeMatrix = array(array(1, 0, 1), array(0, 5, 0), array(1, 0, 1));

  $file = "images/anypngwithalpha.png"; // path to png image
  $im = imagecreatefrompng($file); // open image
  imagesavealpha($im, true); 
  makeFilter($im, $edgeMatrix);

  header('Content-type: image/png');
    
  imagepng($im);
  imagedestroy($im);
  ?>

Expected result:
----------------
Convolutionmatrix should apply to all opaque or semi opaque pixels, and
background should remain unchanged.

Actual result:
--------------
Convolutionmatrix applies to all opaque and semi opaque pixels, background
reverts to solid opaque black regardless of any external settings.

-- 
Edit bug report at http://bugs.php.net/?id=47946&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=47946&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=47946&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=47946&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=47946&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=47946&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=47946&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=47946&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=47946&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=47946&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=47946&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=47946&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=47946&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=47946&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=47946&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=47946&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=47946&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=47946&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=47946&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=47946&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=47946&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=47946&r=mysqlcfg

Reply via email to