0

I have an issue where my transparent images are filling in black. This is only happening in the CMS blocks, not on product images. Also, when I navigate to the media/wsywig folder on my computer, the images are there with the correct transparent background.

So for some reason, only in CMS blocks, images are rendering with the black background even though they are stored with the transparent one. I have done a ton of searching and found answers on how to fix it with modules, but this isn't working for the CMS.

I have installed SUPEE-9767 v2 with no change in behavior.

Has anyone had this issue before?

0

I had the same issue after patching 1.9.2.2 and 1.9.2.3. SUPEE-9767 adds an extended validate method in

app/code/core/Mage/Core/Model/File/Validator/Image.php

Mine was:

public function validate($filePath)
{
    $fileInfo = getimagesize($filePath);
    if (is_array($fileInfo) and isset($fileInfo[2])) {
        if ($this->isImageType($fileInfo[2])) {
            return null;
        }
    }
    throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid MIME type.'));
}

And changed to:

public function validate($filePath)
{
    list($imageWidth, $imageHeight, $fileType) = getimagesize($filePath);
    if ($fileType) {
        if ($this->isImageType($fileType)) {
            //replace tmp image with re-sampled copy to exclude images with malicious data
            $image = imagecreatefromstring(file_get_contents($filePath));
            if ($image !== false) {
                $img = imagecreatetruecolor($imageWidth, $imageHeight);
                imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
                switch ($fileType) {
                    case IMAGETYPE_GIF:
                        imagegif($img, $filePath);
                        break;
                    case IMAGETYPE_JPEG:
                        imagejpeg($img, $filePath, 100);
                        break;
                    case IMAGETYPE_PNG:
                        imagepng($img, $filePath);
                        break;
                    default:
                        return;
                }
                imagedestroy($img);
                imagedestroy($image);
                return null;
            } else {
                throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid image.'));
            }
        }
    }
    throw Mage::exception('Mage_Core', Mage::helper('core')->__('Invalid MIME type.'));
}

The problem seems to be the imagecopyresampled call without setting transparency first as it merges the default black background from imagecreatetruecolor.

What I did was move imagecopyresampled into the switch statement and add the transparency calls before imagecopysampled in the png case (you could also use it for gif).

So now my if/switch looks like this:

if ($image !== false) {
    $img = imagecreatetruecolor($imageWidth, $imageHeight);

    switch ($fileType) {
        case IMAGETYPE_GIF:
            imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
            imagegif($img, $filePath);
            break;
        case IMAGETYPE_JPEG:
            imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
            imagejpeg($img, $filePath, 100);
            break;
        case IMAGETYPE_PNG:
            imagecolortransparent($img, imagecolorallocatealpha($img, 0, 0, 0, 127));
            imagealphablending($img, false);
            imagesavealpha($img, true);
            imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
            imagepng($img, $filePath);
            break;
        default:
            return;
    }
    imagedestroy($img);
    imagedestroy($image);
    return null;
}

This kept my png transparency during product image uploads. Dont know if this will help with the watermark and obviously if you do use this copy the file into your local folder.

app/code/local/Mage/Core/Model/File/Validator/Image.php

Reference check - Watermark get black background when transparent

  • Yes, this is the module I tried to add. It works for my product images, but not for the CMS block. – bsquared Aug 18 '17 at 20:52
  • can you try to re-upload all the images and check after clearing cache – Abhishek Panchal Aug 18 '17 at 20:53
  • It's the same result. I think this is a separate issue, this is the wsywig editor as opposed to the product image uploader. They seem different – bsquared Aug 18 '17 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.