Hi,
I tested this great module with my images - PNG files. But webp files were not generated.
After some digging I've found in Webp.php file on line 86 this condition:

    if ($sourceImage !== NULL && $mimeType !== 'image/png') {

Tha's why my png was not converted.

1) why is there this condition - is it safe to remove it?
2) it should be at least mentioned it on the project page on in the README file

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Bohus Ulrych created an issue. See original summary.

ckaotik’s picture

Status: Active » Needs review
FileSize
521 bytes

I have no idea why PNGs are disabled, they can be generated just fine. I've attached a patch to remove this restriction.

lobodakyrylo’s picture

I suppose WebP doesn't support transparency so it's disabled. Maybe it's better to create an option to enable/disable PNG support on the setting page.

RedEight’s picture

WebP does support transparency though. It also supports animations so it could possibly replace gif as well.

seaarg’s picture

I was facing the same issue and xdebug trace showed me the same issue. After removing the image/png mimetype conditional everything started working fine. Patch in comment #2 worked for me.
Using 7.3.4-1+ubuntu16.04.1+deb.sury.org+3 with GD ext.

If you have the time, can you please explain why that conditional was there? Perhaps there are specific PNG conditions when webp generation fails?

lobodakyrylo’s picture

After some research, I've found that this module uses imagewebp PHP function. For some reason, it doesn't convert PNG files correctly. It skips the alpha layer. At least it doesn't work for PHP 7.0.33-0ubuntu0.16.04.3. Transparent images don't even work for 7.3.4-1+ubuntu16.04.1+deb.sury.org+3 with GD ext.

lobodakyrylo’s picture

Maybe I have a corrupted GD version, I downloaded it using apt-get command though.

I forced the module work with transparent images after installing cwebp on my server and changing the module code.

WebP installation instruction: https://developers.google.com/speed/webp/download

Be sure that my patch not safe. It probably will not work on servers that do not support exec function.

Nigel Cunningham’s picture

Re the patch in 7, it needs quotes around the filenames to deal with spaces in file and directory names. Here's a modified version.

Bart Vanhoutte’s picture

IIRC I disabled PNG support because of a bug in some version of libwebp where transparancy in PNGs is not converted properly. Feel free to add a configuration option to enable it or add some code that detects the version of the library and act accordingly.

alexmoreno’s picture

this patch does not apply anymore, as the code in 1.0@beta already contains that if condition removed

    // If we can generate a GD resource from the source image, generate the URI
    // of the WebP copy and try to create it.
    if ($sourceImage !== NULL) {
alexmoreno’s picture

re-rolled against dev.

IMO makes sense to use exec instead of native php. I have heard the png issue, if this fixes it it's a great addition, thanks a lot everyone that has contributed to this.

If anyone could re-test this patch and confirm it's working for them too, that'd awesome as well :-)

alexmoreno’s picture

FileSize
1.59 KB

patch re-rolled against dev changes

  • alexmoreno committed e4adb69 on 8.x-1.x
    Issue #2992795 by lobodacyril, alexmoreno, NigelCunningham, ckaotik: Why...
alexmoreno’s picture

Status: Needs review » Fixed

done several tests, with and without patch. Looks good to me, so merging it

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

KlemenDEV’s picture

I don't think using exec is the right approach. Many servers and website hostings have exec disabled for security reasons and I think the module should offer an option to select which backend to use for WebP conversion.

alexmoreno’s picture

Status: Closed (fixed) » Needs work
alexmoreno’s picture

@lobodacyril did you find any specific reason why imagewebp would not work?

I would like to re-open re-review this, agree with @Klemen Pevec

This is a blocker before releasing a stable version or even a new beta or RC

KlemenDEV’s picture

I have checked some old commits of this module and my guess based on a quick read on PHP GD is that one needs to enable alpha channel on the image before calling imagewebp like this:

imageAlphaBlending($sourceImage, true);
imageSaveAlpha($sourceImage, true);

I did not test this as I am not really familiar with PHP GD or this module's codebase, but I thought this might help.

rrotari’s picture

FileSize
968 bytes

This is a version without exec function although doesn't always have same results, is server dependent.

alexmoreno’s picture

+++ b/src/Webp.php
@@ -95,6 +95,12 @@ class Webp {
+      if ( $mimeType !== 'image/png') {

would those functions not be compatible with png? I'd rather find a function that works for all instead

alexmoreno’s picture

FileSize
1.72 KB

removing the exec function and adding imageAlphaBlending and imageSaveAlpha

alexmoreno’s picture

Status: Needs work » Needs review

would be great to have this tested. Once we are all happy and check that it's working I want to cut the last beta, wait a reasonable amount of time and launch a first stable version, so we can also get some eyes from the sec team

alexmoreno’s picture

I have done some tests and looks pretty good. I will try a few more png's from different sources and scenarios and merge it before end of weekend, if anyone wants to have a test you are welcome :-)

Thanks all for working and helping with this.

  • alexmoreno committed 1c00333 on 8.x-1.x
    Issue #2992795 by alexmoreno, lobodacyril, NigelCunningham, ckaotik,...
alexmoreno’s picture

Status: Needs review » Fixed
KlemenDEV’s picture

Thank you for fixing this. I hope this gets in a release soon.

Bohus Ulrych’s picture

Yes, thank you for fixing this.

alexmoreno’s picture

Absolutely, if we can confirm that this is working, I am happy to release a new beta preparing for the first stable release.

Just install the dev version and let me know. Thanks a lot, good teamwork :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.