Rasmus Vind
Well-known member
I was using the Addon called Custom Node Icon. I was not very happy with the fact that when I uploaded a PNG, the transparency disappeared. Turns out that this is an issue with XenForo, not Custom Node Icon. Well, I suppose they could have rewritten XenForo_Image_Gd, but the whole point of XenForo is that we can all re-use what you guys have created for us. The Addon takes an image you upload, maybe resizes it and outputs it into a folder, but most of the time it does not process the image at all, it merely loads it in and saves it back to disk. This process should not cause any significant data loss I think. But it does, the transparency of PNG files (and possibly other formats too, GIF?) disappears. I found on StackOverflow that you have to add a few lines of code to make sure that transparency is kept. Here is my modified output function for XenForo_Image_Gd:
I read somewhere that this was a feature because you want people to use real life pictures as your profile picture. But if the image class has to be useful for Addons too, this quickly becomes a flaw.
I hope you will consider adding this to the XenForo codebase.
Thank you.
PHP:
public function output($outputType, $outputFile = null, $quality = 85)
{
switch ($outputType)
{
case IMAGETYPE_GIF: $success = imagegif($this->_image, $outputFile); break;
case IMAGETYPE_JPEG: $success = imagejpeg($this->_image, $outputFile, $quality); break;
case IMAGETYPE_PNG:
// Ralle: hack to save transparency
$background = imagecolorallocate($this->_image, 0, 0, 0);
imagecolortransparent($this->_image, $background);
imagealphablending($this->_image, false);
imagesavealpha($this->_image, true);
// Ralle end
// "quality" seems to be misleading, always force 9
$success = imagepng($this->_image, $outputFile, 9, PNG_ALL_FILTERS);
break;
default:
throw new XenForo_Exception('Invalid output type given. Expects IMAGETYPE_XXX constant.');
}
return $success;
}
I read somewhere that this was a feature because you want people to use real life pictures as your profile picture. But if the image class has to be useful for Addons too, this quickly becomes a flaw.
I hope you will consider adding this to the XenForo codebase.
Thank you.