ImageConvolution overwrites background (fix included)
| Bug #47946 | ImageConvolution overwrites background (fix included) | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2009-04-10 16:26 UTC | Modified: | 2017-01-22 14:44 UTC |
|
||||||||||
| From: | jcolby at acsol dot net | Assigned: | pajoye (profile) | |||||||||||
| Status: | Closed | Package: | GD related | |||||||||||
| PHP Version: | 5.2.9 | OS: | openSuse, CentOS, FreeBSD | |||||||||||
| Private report: | No | CVE-ID: | None | |||||||||||
[2009-04-10 16:26 UTC] 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.
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2009-04-10 16:31 UTC] 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-12 14:45 UTC] iliaa@php.net
[2009-04-12 16:59 UTC] pajoye@php.net
[2009-04-12 17:58 UTC] jcolby at acsol dot net
[2009-06-23 00:57 UTC] kalle@php.net
[2017-01-22 14:29 UTC] cmb@php.net
-Status: Assigned +Status: Closed