ID: 47946 User updated by: jcolby at acsol dot net Reported By: jcolby at acsol dot net Status: Assigned Bug Type: GD related Operating System: openSuse, CentOS, FreeBSD PHP Version: 5.2.9 Assigned To: pajoye New Comment:
Looking at it, I did initialize the srctrans variable incorrectly. If you have a better method have at it! I'm not a C developer of any form. Since imagecreatetruecolor does a black fill and defaults to alpha off, in order to have a transparent background on the srcback/temp convolution image the reverse becomes necessary or all alpha areas of the image will inherit the black background of srcback. Previous Comments: ------------------------------------------------------------------------ [2009-04-12 16:59:40] paj...@php.net Assigned to me, not completely happy with the fix. ------------------------------------------------------------------------ [2009-04-12 14:45:23] il...@php.net This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. ------------------------------------------------------------------------ [2009-04-10 16:31:46] jcolby at acsol dot net Missing function from test case: function array_flatten($array) { (array)$tempArray = array(); foreach ( $array as $value ) { if ( is_array($value) ) { $tempArray = array_merge($tempArray, array_flatten($value)); } else { $tempArray[] = $value; } } return $tempArray; } ------------------------------------------------------------------------ [2009-04-10 16:26:01] jcolby at acsol dot net 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 this bug report at http://bugs.php.net/?id=47946&edit=1