imagecreatefromwebp() -> imagejpeg() results in blue channel missing
Asked Answered
S

2

7

I am losing color information, seemingly the blue channel, from an image after using GD to read from the WebP version and output a JPEG. Why does this happen and how can I fix it?

Original Image

enter image description here

Code

$pic = imagecreatefromwebp('https://lh4.ggpht.com/uaLB-tbci94IubJJhDZ4n6vJwGF4i9MpFLXl28LBHjVzLIy-K6fhoSILbM4yJcKqq9I=h900-rw');
imagejpeg($pic, './example.jpg', 80);
imagedestroy($pic);

Resulting Image

pic here http://savepic.ru/7812459.png

Sough answered 20/9, 2015 at 19:41 Comment(3)
I suspect that this is a bug in GD for reading the WebP image. Or, the original image has a problem preventing a proper conversion. The original WebP version has no colorspace, but that shouldn't be a problem in most cases. regex.info/…Shovel
Just FYI, ImageMagick has no problem reading it and making a JPEG of it... convert https://lh4.ggpht.com/uaLB-tbci94IubJJhDZ4n6vJwGF4i9MpFLXl28LBHjVzLIy-K6fhoSILbM4yJcKqq9I=h900-rw image.jpgShedevil
so, it is can't fixed?Sough
H
3

This looks like PHP bug #70102, "imagecreatefromwebm() shifts colors".

It is fixed in PHP >= 5.6.12 (release notes).

Your script works correctly for me (there is no yellow tint) in PHP 5.6.13

Hindmost answered 21/9, 2015 at 15:58 Comment(4)
my PHP version => PHP Version 5.6.13-1+deb.sury.org~trusty+3, and don't workSough
Same here, my php version is 5.6.17 and I have the yellow hue as well :/Displant
I have PHP 5.6.4 on Apache on a windows machine, and I'm getting a similar error with the transparency.Antabuse
php7.0 and same situation exists here. By the way on windows wamp server setup everything is fine. there is no problem.Tantalum
S
2

Seems like there's couple of bits too many...

function fixBlue($im)
{
    $f_im = imagecreatetruecolor(imagesx($im),imagesy($im));
    $c = imagecolorallocate($f_im, 255, 255, 255);
    imagefill($f_im, 0, 0, $c);

    for($y=0;$y<imagesy($im);$y++)
    {
        for($x=0;$x<imagesx($im);$x++)
        {
            $rgb_old = imagecolorat($im, $x, $y);
            $r = ($rgb >> 24) & 0xFF;
            $g = ($rgb >> 16) & 0xFF;
            $b = ($rgb >> 8) & 0xFF;
            $pixelcolor = imagecolorallocate($f_im, $r, $g, $b);
            imagesetpixel($f_im, $x, $y, $pixelcolor);
        }
    }
    return $f_im;
}

and then just:

$img = imagecreatefromwebp('https://lh4.ggpht.com/uaLB-tbci94IubJJhDZ4n6vJwGF4i9MpFLXl28LBHjVzLIy-K6fhoSILbM4yJcKqq9I=h900-rw');
$img_f = fixBlue($img, 60);

ob_end_clean();
header('Content-type: image/jpeg');
imagejpeg($img_f, null, 80);

imagedestroy($img);
imagedestroy($img_f);
Sprint answered 12/9, 2018 at 20:27 Comment(1)
I can test with PHP 5 only, dont know about PHP 7.Sprint

© 2022 - 2024 — McMap. All rights reserved.