Fixed XenForo_Image_ImageMagick_Pecl and transparency

Rasmus Vind

Well-known member
Considering you decided to solve it for GD, here it is for Imagick.

Transparency in GIF and PNG are not persisted when resizing images. In fact, a transparent background becomes black for one one type and white for the another.

I wrote a little addon that extends the class XenForo_Image_ImageMagick_Pecl class:
PHP:
<?php

class Ranimavatars_Imagick extends XFCP_Ranimavatars_Imagick
{
    protected function __construct(Imagick $image)
    {
        $image->setImageBackgroundColor(new ImagickPixel('transparent'));
        $image->setImageAlphaChannel(imagick::ALPHACHANNEL_REMOVE);
        $image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
        parent::__construct($image);
    }
}

But I think it makes more sense to make it part of XenForo.

It will make sure that transparency is kept when working with PNG and GIF.
 
This is again image specific as I'm unable to reproduce it with any test image that I have. Can you provide a reproduction case image?
 
Let's see. I have attached two images:

Imagick:

99.png gets a white background
99.gif gets a black background

GD:

99.gif gets a white background
99.png gets transparent background

But only for avatars.

My patch fixes Imagick, but there seems to also be a problem with GD.
 

Attachments

  • 99.gif
    99.gif
    15.6 KB · Views: 14
  • 99.webp
    99.webp
    2.9 KB · Views: 15
I can reproduce the GD issue mentioned there and I have a workaround for that.

However, I can't reproduce any Imagick issues. We've tried 3 different versions of the Imagick PECL extensions with different versions of ImageMagick itself (on different systems/OSes) and they output both images with a transparent background in all our cases.

What version of Imagick and ImageMagick are you using?
 
Code:
root@f88fc3117404:/tmp/hive2# php --ri imagick

imagick

imagick module => enabled
imagick module version => 3.4.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.8.9-9 Q16 x86_64 2016-01-17 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.8.9-9 Q16 x86_64 2016-02-02 http://www.imagemagick.org
ImageMagick copyright => Copyright (C) 1999-2014 ImageMagick Studio LLC
ImageMagick release date => 2016-02-02
ImageMagick number of supported formats:  => 215
ImageMagick supported formats => 3FR, AAI, AI, ART, ARW, AVI, AVS, BGR, BGRA, BIE, BMP, BMP2, BMP3, BRF, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DFONT, DJVU, DNG, DOT, DPX, DXT1, DXT5, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, EXR, FAX, FITS, FRACTAL, FTS, G3, GIF, GIF87, GRADIENT, GRAY, GROUP4, GV, HALD, HDR, HISTOGRAM, HRZ, HTM, HTML, ICB, ICO, ICON, INFO, INLINE, IPL, ISOBRL, JBG, JBIG, JNG, JNX, JPEG, JPG, JSON, K25, KDC, LABEL, M2V, M4V, MAC, MAP, MASK, MAT, MATTE, MEF, MIFF, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NRW, NULL, ORF, OTB, OTF, PAL, PALM, PAM, PANGO, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG00, PNG24, PNG32, PNG48, PNG64, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSB, PSD, PTIF, PWP, RADIAL-GRADIENT, RAF, RAS, RAW, RGB, RGBA, RGBO, RGF, RLA, RLE, RMF, RW2, SCR, SCT, SFW, SGI, SHTML, SIX, SIXEL, SPARSE-COLOR, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UBRL, UIL, UYVY, VDA, VICAR, VID, VIFF, VIPS, VST, WBMP, WMF, WMV, WMZ, WPG, X, X3F, XBM, XC, XCF, XPM, XPS, XV, XWD, YCbCr, YCbCrA, YUV

Directive => Local Value => Master Value
imagick.locale_fix => 0 => 0
imagick.skip_version_check => 0 => 0
imagick.progress_monitor => 0 => 0

and
Code:
root@f88fc3117404:/tmp/hive2# convert --version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-02-02 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib

You can reproduce it in Docker:
https://hub.docker.com/r/ralle/hive2base/~/dockerfile/
 
I was unable to reproduce it with your Docker container either. It did pull down php-imagick 3.4.2, so I suppose that's relevant. ImageMagick itself is the same version, but the release dates also differ.

I'm going to send you a test script to confirm that you can reproduce the issue there. It's a standalone version of some of the image manipulation classes and testing different permutations.
 
I just wanted to thank @Ralle for his fix. I switched from GD to ImageMagick a few days ago and also had the issue that the transparency got lost when pictures needed to be resized. After installing his addon this is issue is completely gone now. Thank you so much for that! :)
 
Back
Top Bottom