20

I've installed PATCH SUPEE 9767 on my magento 1.9.2.4 shop.

Now i've uploaded a new Watermark but the background changes to black.

Is this a problem since the new update? On a other magento 1.9.2.4 installation where the update isn't installed there is the background still transparant.

29

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

  • Can you please open an issue at github.com/OpenMage/magento-lts? – sv3n Jun 9 '17 at 10:54
  • you saved me hours! thx! – Michael Leiss Jun 23 '17 at 13:55
  • Btw, after applying this to my Image.php, the Uploading of image seems to be stuck to "Uploading". Forever. O__O Anyone encountered the same problem? – jehzlau Jul 7 '17 at 6:20
  • I have seen a 1.9.2.3 site without the SUPEE-8788 patch experience admin upload issues after patching with SUPEE-9767. – Tim Sullivan Jul 8 '17 at 10:52
  • 1
    @TimSullivan I tried your solution but didn't works for me. – Deepak Mankotia Jul 21 '17 at 9:49
3

I'd try to save the image again (maybe with another programm). And if it does'nt help you can try this:

app/code/local/Varien/Image/Adapter/Gd2.php and copy the contents of /lib/Varien/Image/Adapter/Gd2.php

Change:

$this->_fillBackgroundColor($newImage);

To:

$this->_fillBackgroundColor($newImage, $frameWidth, $frameHeight);

Change:

if (!imagefill($imageResourceTo, 0, 0, $color)) {

To:

if (!imagefilledrectangle($imageResourceTo, 0, 0, $w, $h, $color)) {

Source: https://www.gravitywell.co.uk/latest/how-to/posts/fixing-black-magento-adds-to-image-backgrounds/


Edit: this has been fixed in Magento 1.9.3.4 / SUPEE-9767 V2

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

Changed from:

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;
    }

To:

if ($image !== false) {
    $img = imagecreatetruecolor($imageWidth, $imageHeight);
    imagealphablending($img, false);
    imagecopyresampled($img, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight);
    imagesavealpha($img, true);

    switch ($fileType) {
         case IMAGETYPE_GIF:
            $transparencyIndex = imagecolortransparent($image);
            if ($transparencyIndex >= 0) {
                imagecolortransparent($img, $transparencyIndex);
                for ($y = 0; $y < $imageHeight; ++$y) {
                    for ($x = 0; $x < $imageWidth; ++$x) {
                        if (((imagecolorat($img, $x, $y) >> 24) & 0x7F)) {
                            imagesetpixel($img, $x, $y, $transparencyIndex);
                        }
                    }
                }
            }
            if (!imageistruecolor($image)) {
                imagetruecolortopalette($img, false, imagecolorstotal($image));
            }
            imagegif($img, $filePath);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($img, $filePath, 100);
            break;
        case IMAGETYPE_PNG:
            imagepng($img, $filePath);
            break;
        default:
            break;
    }
  • I tried your both solution 1st one throwing error of undefined variable and second is not working. I am using magento 1.9.3.1 – Deepak Mankotia Jul 21 '17 at 10:32
  • Have you tried to apply full latest patch SUPEE-9767 V2? – sv3n Jul 21 '17 at 10:36
  • I have tried after applying SUPEE-9767 V2 patch – Deepak Mankotia Jul 21 '17 at 11:48
0

I've created an Magento module according to Tim Sullivan's answer that fixes that issue:

https://github.com/CopeX/PNGUploadFix

  • Try to provide some code details here rather then just posting a link. – Priyank Jul 5 '17 at 6:58
  • @pointiA I tried your module and didn't fix the issue for me – Deepak Mankotia Jul 21 '17 at 9:52
0

I have create a patch file easily install on your magento root folder.

URL : Download from here

0

I found that adjusting the files Image.php and GD2.php as suggested in above answers works, but for me it meant that JPEG thumbnails that were not completely square suddenly had black backgrounds. So in GD2.php I changed

if (!imagefilledrectangle($imageResourceTo, 0, 0, $w, $h, $color)) {
            throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
        }

to

if($this->_fileType == IMAGETYPE_JPEG){
        if (!imagefill($imageResourceTo, 0, 0, $color)) {
            throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
        }
    } else {
        if (!imagefilledrectangle($imageResourceTo, 0, 0, $w, $h, $color)) {
            throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
        }
    }

in order to keep the old situation for JPEG's.

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.