ImagickException: Unable to set image alpha channel

AndyB

Well-known member
Hello,

In my Convert Image v4.2 add-on located here:

https://xenforo.com/community/resources/convert-image.2521/

I have the need to create a thumbnail. The issue I'm having is that when a .png image with a transparent background is converted to a .jpg image, the new .jpg image would turn the background to black, so I use this bit of code to change the background to white before creating the .jpg thumbnail.

PHP:
// check if image has transparency
if ($imagick->getImageAlphaChannel())
{
    // remove alpha channel
    $imagick->setImageAlphaChannel(11);
   
    // set image background color
    $imagick->setImageBackgroundColor('white');

    // merge layers
    $imagick->mergeImageLayers(imagick::LAYERMETHOD_FLATTEN);           
}

This works fine on 99% of the servers the add-on is installed on, but some servers throw out this error:

Code:
ImagickException: Unable to set image alpha channel

The error is thrown by this line of code:

PHP:
$imagick->setImageAlphaChannel(11);

I have verified the setImageAlphaChannel is available using SSH:

Code:
php --rc Imagick | grep -i setImageAlphaChannel

and Imagick has been compiled against ImageMagick version 6.3.8 or newer.

Is there a better way to create thumbnails? Is there a XenForo function I can call to create a thumbnail image?

Thank you.
 
Hi Nobita.Kun,

Thank you for your suggestion. I looked very carefully at the XenForo functions related to thumbnail creation, however I was not able to understand the code well enough.

I opted to use GD for images that have transparencies, here's the code I'm testing now and seems to work perfectly.

PHP:
// if image has transparency
if ($imagick->getImageAlphaChannel())
{
    // use GD to change transparency to white
    $input = imagecreatefrompng($attachmentFullPath);
    list($width, $height) = getimagesize($attachmentFullPath);
    $output = imagecreatetruecolor($width, $height);
    $white = imagecolorallocate($output,  255, 255, 255);
    imagefilledrectangle($output, 0, 0, $width, $height, $white);
    imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
    imagejpeg($output, $thumbpath);
    
    // use GD to resize the image
    $source_image = imagecreatefromjpeg($thumbpath);
    $width = imagesx($source_image);
    $height = imagesy($source_image);
    $desired_height = floor($height * ($attachmentThumbnailDimensions / $width));
    $virtual_image = imagecreatetruecolor($attachmentThumbnailDimensions, $attachmentThumbnailDimensions);
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $attachmentThumbnailDimensions, $attachmentThumbnailDimensions, $width, $height);
    imagejpeg($virtual_image, $thumbpath);
}
 
@AndyB

PHP:
$processor = XenForo_Image_ImageMagick_Pecl::createFromFileDirect(...);
$processor->thumbnail($maxWidth, $maxHeight);
$processor->output(...);

It is special if you want to using ImageMagick.
 
Hi Nobita.Kun,

Thank you for the help. I first tried without the $inputType variable, but got an error indication the second variable was missing. So I looked at the XenForo code and saw there needed to be a $inputType variable. So I tried the following code:

PHP:
$processor = XenForo_Image_ImageMagick_Pecl::createFromFileDirect($attachmentFullPath, $inputType);
$processor->thumbnail($attachmentThumbnailDimensions, $attachmentThumbnailDimensions);
$processor->output($thumbpath);

But I get the following error:

upload_2017-2-2_14-59-19.webp

I can't seem to find where in the XenForo code the $inputType variable is set so I'm not able to see what sort of data it needs to be.
 
Got it.

If the image type is a .gif then $inputType = 1

If the image type is a .jpg then $inputType = 2

If the image type is a .png then $inputType = 3
 
The code below will create a thumbnail fine under most cases, but I still need to address the following two issues:

1) If the hot linked image is an animated gif, the thumbnail will not be produced.

2) If the hot linked image has a transparent background, the transparency turns black in the thumbnail.

PHP:
$processor = XenForo_Image_ImageMagick_Pecl::createFromFileDirect($attachmentFullPath, $inputType);
$processor->thumbnail($attachmentThumbnailDimensions, $attachmentThumbnailDimensions);
$processor->output($outputType, $thumbpath, $quality = 85);
 
Last edited:
Back
Top Bottom