Tofandel
Tofandel

Reputation: 3585

imagecreatefromjpeg produces weird corrupted images with missing colors

A very basic code to retrieve and resize images produces corrupted images, the corruption seems to happen straight from imagecreatefromjpeg (imagecreatefromstring produces the same result)

$image = imagecreatefromjpeg( "./original.jpg" );
imagejpeg($image, "./result.jpg");

Here is the original image, which in any other program shows correctly:

original image

And the resulting image:

generated image

What is wrong with the original image, why is gd not able to read it correctly? Can I do something on php's side to fix this issue (You can download the image from SO, I just checked and it gives the same result)

Upvotes: 0

Views: 60

Answers (1)

Tofandel
Tofandel

Reputation: 3585

As pointed out in the comments, GD simply doesn't know how to handle any other color space than sRGB

So I switched to using imagick, I converted my resizing function to the following code, which produces the expected output

$image = new Imagick();
$image->readImageBlob(file_get_contents($original));

$image->thumbnailImage($max_width, $max_height, true);

$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(75);
$image->stripImage();
$image->writeImage($filename);

Upvotes: 0

Related Questions