note 124728 deleted from function.imagecreatefromwebp by cmb
| From: | cmb@php.net | Date: | Mon, 17 Feb 2020 13:36:57 +0000 |
| Subject: | note 124728 deleted from function.imagecreatefromwebp by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214162@lists.php.net to get a copy of this message | ||
Note Submitter: Adam
----
It seems that imagecreatefromwebp() function sometimes shifts the colors. I wanted to convert webp
image to jpeg format, but the converted image has lost its blue channel. I found a code, that helped
to fix the bug.
<?php
// Gets the webp image
$webp_image = imagecreatefromwebp('./source-image.webp');
// Creates a new tmp true color image
$width = imagesx($webp_image);
$height = imagesy($webp_image);
$image = imagecreatetruecolor($width, $height);
$c = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $c);
// Fixes blue channel
for($x=0; $x < $width; $x++) {
for($y=0; $y < $height; $y++) {
$rgb = imagecolorat($webp_image, $x, $y);
$r = ($rgb >> 24) & 0xFF;
$g = ($rgb >> 16) & 0xFF;
$b = ($rgb >> 8) & 0xFF;
$pixelcolor = imagecolorallocate($image, $r, $g, $b);
imagesetpixel($image, $x, $y, $pixelcolor);
}
}
// Save jpeg image
imagejpeg($image, './converted-image.jpg');
imagedestroy($image);
imagedestroy($webp_image);
?>