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

Brad
  • 159,648
  • 54
  • 349
  • 530
emtecif
  • 195
  • 2
  • 9
  • 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. http://regex.info/exif.cgi?imgurl=https%3A%2F%2Flh4.ggpht.com%2FuaLB-tbci94IubJJhDZ4n6vJwGF4i9MpFLXl28LBHjVzLIy-K6fhoSILbM4yJcKqq9I%3Dh900-rw – Brad Sep 20 '15 at 20:32
  • 1
    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.jpg` – Mark Setchell Sep 20 '15 at 21:49
  • so, it is can't fixed? – emtecif Sep 21 '15 at 09:20

2 Answers2

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

timclutton
  • 12,682
  • 3
  • 33
  • 43
1

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);
Sven Liivak
  • 1,323
  • 9
  • 10