Patch: Avatar PNG Transparency (Bugs)

by beornlake, Wednesday, June 18, 2014, 20:56 (3571 days ago)

We have several users who want to use PNG avatars with transparency. The PNG handling methods in PHP default to using a black background, so alpha channels are lost when uploaded to our forum. We patched our installation's resize_image() function in functions.inc.php with the following to preserve alpha channels when PNGs are uploaded:

Between these two lines (Sourceforge link):

if(empty($error)) $new_image=imagecreatetruecolor($new_width,$new_height) or $error = true;
if(empty($error)) imagecopyresampled($new_image,$current_image,0,0,0,0,$new_width,$new_height,$image_info[0],$image_info[1]) or $error = true;

We splice in this block:

if(empty($error)) {
  imagesavealpha($new_image, true);
  $transparency_fill = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
  imagefill($new_image, 0, 0, $transparency_fill);
}

This fills the image with transparency before pixels are copied to the new canvas. This implementation appears to be working properly on our installation, but further testing is always prudent. ;-)


Complete thread:

 RSS Feed of thread