We looked into the plugin’s code and found that it was using PHP’s native imagewebp function, which should definitely be compatible with transparent PNGs.
Through some research online we were able to fix the issue by making a small edit to the PNG switch case in the create_webp function in lib/class-pluswebp.php file.
Original:
case 'image/png':
$src = imagecreatefrompng( $filename );
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
imagealphablending( $img, false );
imagesavealpha( $img, true );
break;
Fixed:
case 'image/png':
$src = imagecreatefrompng( $filename );
$img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
imagealphablending( $img, false );
imagesavealpha( $img, true );
$transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
imagefill( $img, 0, 0, $transparent );
break;
After updating that code, we ran the conversion again on our client’s website and successfully converted every PNG image to WebP without breaking any transparent backgrounds.
-
This reply was modified 1 year, 7 months ago by
SiteBolts.
Thanks your report.
I’m unable to download the image you specified, so we cannot test it. ( Sorry, you have been blocked
You are unable to access sitebolts.com )
@katsushi-kawamori – Sorry about that. I’ve loosened our firewall rules; are you able to access the testing images now? Otherwise I can upload them to Dropbox.
Images could be retrieved.
I added them to the media library.
Plugin deactivated
- The original image is now grayed out and invisible.
Plugin activated
- Original code
- The original image becomes grayed out and invisible.
- The webp image is correctly generated into a normal black and white image.
- Your code
- The original image and the webp image will be grayed out and invisible.
@katsushi-kawamori – To clarify, the image is meant to be gray with a transparent background, not a black background. It sounds like maybe you were testing it on a white or light gray background, which made it difficult to see? Google Chrome also adds a light gray background when viewing transparent images in their own tab, which can make it more difficult to spot the issue.
If you change the background to something like green or purple then the issue should be a bit more evident. Here’s an example: https://wctest.zilbr.com/webp-transparency-issue/
I did a bit of testing and found that the bug only affects “indexed” PNGs but not “RGB” ones. The patched code appears to be able to handle both cases correctly.
It is blocked and I can’t download it.
@katsushi-kawamori – I’ve adjusted the firewall rules for that site now too.