John Davidson

Convert PNG files with transparency to WebP using PHP GD

0 comments
Message:


I have a utility class that loads image files and, among other operations, converts them to other formats. It uses PHP GD.


Everything works fine except PNG files that have transparency are done wrong when converting to WebP. The result image has a black background where the transparency should be.


This is my code:


class OImage {
private ?GdImage $image = null;
private ?int $image_type = null;

public function load(string $filename): void {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];

switch ($this->image_type) {
case IMAGETYPE_JPEG: { $this->image = imagecreatefromjpeg($filename); }
break;
case IMAGETYPE_GIF: { $this->image = imagecreatefromgif($filename); }
break;
case IMAGETYPE_PNG: { $this->image = imagecreatefrompng($filename); }
break;
case IMAGETYPE_WEBP: { $this->image = imagecreatefromwebp($filename); }
break;
}
}

public function save(string $filename, int $image_type=IMAGETYPE_JPEG, int $compression=75, int $permissions=null): void {
switch ($image_type) {
case IMAGETYPE_JPEG: { imagejpeg($this->image, $filename, $compression); }
break;
case IMAGETYPE_GIF: { imagegif($this->image, $filename); }
break;
case IMAGETYPE_PNG: { imagepng($this->image, $filename); }
break;
case IMAGETYPE_WEBP: {
imagepalettetotruecolor($this->image);
imagealphablending($this->image, true);
imagesavealpha($this->image, true);
imagewebp($this->image, $filename);
}
break;
}
if (!is_null($permissions)) {
chmod($filename, $permissions);
}
}

...
}

The class has many other functions to resize or scale, but are not related to my problem here. I have tried setting imagealphablending and imagesavealpha to true or false with the exact same outcome.


I was also thinking on switching to Imagick, but they don't have yet a PHP 8 extension.


I am using PHP 8 on Debian 9 and GD 2.2.4


Any help?


Thanks!



My Answer:
> image_type = $image_info[2];_ switch ($this->image_type) {_ case IMAGETYPE_JPEG:_ $this->image = imagecreatefromjpeg($filename);_ break;_ case IMAGETYPE_PNG:_ $this->image = imagecreatefrompng($filename);_ break;_ default:_ throw new Exception('Unsupported image type');_ }_ }_ _ public function saveAsWebP(string $filename, int $quality = 80): void {_ if ($this->image_type === IMAGETYPE_PNG) {_ imagepalettetotruecolor($this->image);_ imagealphablending($this->image, true);_ imagesavealpha($this->image, true);_ }_ imagewebp($this->image, $filename, $quality);_ }_}_// Example usage_$image = new OImage();_$image->load('image.png');_$image->saveAsWebP('image.webp');_In order to preserve transparency when converting PNG files to WebP using PHP GD, you need to ensure that the alpha channel is properly handled. Here are the modifications you can make to your code:_1. After loading the PNG image, you need to enable alpha blending and save alpha channel information:_php_ if ($this->image_type === IMAGETYPE_PNG) {_ imagealphablending($this->image, false);_ imagesavealpha($this->image, true);_ }__2. When saving the image as WebP, you should use the `imagewebp()` function with the `WEBP_LOSSLESS` flag to preserve transparency:_php_ imagewebp($this->image, $filename, $quality, WEBP_LOSSLESS);__By making these changes, the transparency in PNG files should be preserved when converting them to WebP using PHP GD.

Rate this post

5 of 5 based on 9107 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us