Search by

yireo / magento2-webp2

yireo

Magento 2 module to add WebP support to the Magento frontend

Package info

github.com/yireo/Yireo_Webp2

Homepage

Type:magento2-module

pkg:composer/yireo/magento2-webp2

Statistics

Installs: 1 465 843

Dependents: 7

Suggesters: 2

Stars: 212

Open Issues: 1

0.15.0 2026-12-02 15:43 UTC

README

Magento version PHP version License Latest Version

This module adds WebP support to Magento 2.

About this module

  • When <img> tags are found on the page, the corresponding JPG or PNG is converted into WebP and a corresponding <picture tag is used to replace the original <img> tag.
  • The Fotorama gallery of the Magento core product pages is replaced with WebP images without issues as well. However, the Fotorama effect loads new JPG images again, replacing the original <picture> tag. This shows that the Fotorama library is not scalable and may be a bad decision to use. We recommend you replace it with the Notorama module instead.

Warning

Hyvä support is built into the latest versions of this module. Please do not use the deprecated compatibility module anymore.

Instructions for using composer

Use composer to install this extension. Not using composer is not supported. Next, install the new module plus its dependency Yireo_NextGenImages into Magento itself:

composer require yireo/magento2-webp2
bin/magento module:enable Yireo_Webp2 Yireo_NextGenImages
bin/magento setup:upgrade

Enable the module by toggling the setting in Stores > Configuration > Yireo > Yireo WebP > Enabled.

System requirements

Make sure your PHP environment supports WebP: This means that the function imagewebp should exist in PHP. We hope to add more checks for this in the extension itself soon. For now, just open up a PHP phpinfo() page and check for WebP support. Please note that installing libwebp on your system is not the same as having PHP support WebP. Check the phpinfo() file and add new PHP modules to PHP if needed. If in doubt, simple create a PHP script test.php and a line <?php echo (int)function_exists('imagewebp'); to it: A 1 indicates that the function is available, a 0 indicates that it is not. Alternatively you can check for WebP using the command php -r 'var_dump(gd_info());'. Make sure your CLI binary is the same as the one being called by the webserver though.

An alternative is that the cwebp binary from the WebP project is uploaded to your server and placed in a generic folder like /usr/local/bin. Make sure to grab a copy from this binary from the rosell-dk/webp-convert project. This method is preferred because it is the fastest. But it assumes also that the binary is placed in a folder by the server administrator.

We recommend you to work on making all options work, not just one.

Please note that both tasks should be simple for developers and system administrator, but might be magical for non-technical people. If this extension is not working out of the box for you, most likely a technical person needs to take a look at your hosting environment.

FAQ

Does this module support GraphQL?

Yes, but only via the additional Yireo Webp2GraphQl module

Why are images with a dynamic Alpine.js :src binding not converted?

The <img> tags are replaced with <picture> tags in PHP, while the page is being rendered. Literal image paths in the attributes src, data-src, :src and :data-src are converted. But a binding like :src="category.thumbnail_image" is a JavaScript expression that only resolves to a URL in the browser (for instance after an AJAX call), so there is no image URL to convert yet.

In that case, add the WebP URL to the data that is passed to your JavaScript component:

  • If the data comes from GraphQL, use the Yireo Webp2GraphQl module to add WebP URLs to the GraphQL output.
  • If the data comes from your own PHP code, add the WebP URL there.
  • In your Alpine.js component, pick the WebP URL when the webp class is set on the <body> (added by view/frontend/templates/hyva/add-webp-class-to-body.phtml) and the original URL otherwise. See view/frontend/templates/hyva/gallery-additions.phtml for an example of swapping in WebP URLs (*_webp properties) in JavaScript.

Does the Hyvä product gallery use WebP images?

Yes. The Hyvä gallery renders its images with Alpine.js bindings like :src="image.img", which can not be converted in PHP. Instead, the Yireo NextGenImages module adds WebP URLs (img_webp, full_webp and thumb_webp) to the gallery data of getGalleryImagesJson(). It wraps the global initGallery() function to dispatch a gallery-init event with this data. The template view/frontend/templates/hyva/gallery-additions.phtml then replaces the image URLs with their WebP URLs in the browser, before Alpine.js renders the gallery. Both the gallery data of Hyvä 1.5 (initImages) and of older versions (images and initialImages) are supported.

If you use a custom gallery template, make sure it still defines a global initGallery() function that returns the gallery data. Otherwise, dispatch the gallery-init event yourself with the gallery data as detail, before Alpine.js renders the gallery.

Do swatches in Hyvä product lists switch the WebP image?

Yes. When a swatch is selected, Hyvä dispatches an update-gallery-<productId> event that only changes the src of the <img>. The template view/frontend/templates/hyva/product-list-image-webp.phtml updates the WebP <source> of the <picture> as well, using the WebP image of the swatch response. Product list images are picked up once the page is loaded. If you add product cards later on (for instance via AJAX), call window.yireoWebp2BindProductListImages(element) for the new HTML.

Does this module work with a strict Content Security Policy (CSP)?

Yes. The Hyvä templates under view/frontend/templates/hyva/ add static inline scripts, which are whitelisted by their sha256 hash in etc/csp_whitelist.xml. Unlike a nonce, a hash keeps working on pages served from the Full Page Cache or Varnish. If you override one of these templates in your theme and change the script, the hash no longer matches: Add the hash of your own script to a csp_whitelist.xml file in your own module.

How do I know WebP is used?

Make sure to test things with the obvious caches disabled (Full Page Cache, Block HTML Cache). Once this extension is working, catalog images (like on a category page) should be replaced with: Their <img> tag should be replaced with a <picture> tag that lists both the old image and the new WebP image. If the conversion from the old image to WebP goes well.

You can expect the HTML to be changed, so inspecting the HTML source gives a good impression. You can also use the Error Console to inspect network traffic: If some webp images are flying be in a default Magento environment, this usually proofs that the extension is working to some extent.

My CPU usage goes up. Is that normal?

Yes, it is normal. This extension does two things: It shows a WebP on the frontend of your shop. And it generates that WebP when it is missing. Obviously, generating an image takes up system resources. And if you have a large catalog, it is going to do more time. How much time? Do make sure to calculate this yourself: Take an image, resize it using the cwebp binary and measure the time - multiply it by how many images there are. This should give a fair estimation on how much time is needed.

Note that this extension allows for using various mechanisms (aka convertors). Tune the Convertors settings if you want to try to optimize things. Sometimes, GD is faster than cwebp. Sometimes, GD just breaks things. It depends, so you need to pick upon the responsibility to try this in your specific environment.

Also note that this extension allows for setting an encoding. The default is lossy. The option auto creates both a lossy and a lossless WebP and then picks the smallest one, which takes about twice as long.

If you don't like the generation of images at all, you could also use other CLI tools instead.

The WebP image is larger than the original JPEG

WebP is not always smaller. Common causes are the lossless (or auto) encoding on photos, a JPEG that was saved with a lower quality than the configured Quality level while Auto-limit quality can't detect the JPEG quality (it requires Imagick, ImageMagick or GraphicsMagick), or a fallback to the GD convertor. With the setting Skip WebP images larger than the original (enabled by default), a WebP image that is not smaller than its original is not used and the original image is served instead. The WebP file is kept on disk, so it is not converted again on every request. To retry the conversion with other settings, remove the WebP file.

Class 'WebPConvert\WebPConvert' not found

We only support the installation of our Magento 2 extensions, if they are installed via composer. Please note that - as we see it - composer is the only way for managing Magento depedencies. If you want to install the extension manually in app/code, please study the contents of cmoposer.json to install all dependencies of this module manually as well.

After installation, I'm still seeing only PNG and JPEG images

This could mean that the conversion failed. New WebP images are stored in the same path as the original path (somewhere in pub/) which means that all folders need to be writable by the webserver. Specifically, if your deployment is based on artifacts, this could be an issue.

Also make sure that your PHP environment is capable of WebP: The function imagewebp should exist in PHP and we recommend a cwebp binary to be placed in /usr/local/bin/.

Last but not least, WebP images only work in WebP-capable browsers. The extension detects the browser support. Make sure to test this in Chrome first, which natively supports WebP.

Warning: filesize(): stat failed for xxx.webp

If you are seeing this issue in the exception.log and/or system.log, do make sure to wipe out Magento caching and do make sure that the WebP file in question is accessible: The webserver running Magento should have read access to the file. Likewise, if you want this extension to automatically convert a JPEG into WebP, do make sure that the folder containing the JPEG is writable.

After flushing the catalog images cache, WebP images are missing

When the folder pub/media/catalog/product/cache/ is wiped out while the Full Page Cache is still warm, the cached pages keep pointing to the JPEG and PNG images. WebP images are only generated again once those pages are regenerated. To generate the WebP images together with the resized catalog images, enable the setting Convert images on save (yireo_nextgenimages/settings/convert_on_save) of Yireo_NextGenImages and run:

bin/magento catalog:images:resize
bin/magento cache:clean full_page

With this setting enabled, every image that Magento saves (including resized catalog images) is converted to WebP as well, which makes the resizing slower.

Some of the images are converted, but others are not.

Not all JPEG and PNG images are fit for conversion to WebP. In the past, WebP has had issues with alpha-transparency and partial transparency. If the WebP image can't be generated by our extension, it is not generated. Simple as that. If some images are converted but some are not, try to upload those to online conversion tools to see if they work.

Make sure your cwebp binary and PHP environment are up-to-date.

PageBuilder background images (or their mobile images) are not converted

PageBuilder renders background images (including the separate mobile image of rows, banners and sliders) as CSS backgrounds, not as img tags. CSS backgrounds are only converted when the setting Convert CSS backgrounds (yireo_nextgenimages/settings/convert_css_backgrounds) of Yireo_NextGenImages is enabled. This setting is disabled by default. The original background-image is kept and an additional image-set() declaration with the WebP image is added, so browsers without WebP support keep displaying the original image.

This sucks. It only works in some browsers.

Don't complain to us. Instead, ask the other browser vendors to support this as well. And don't say this is not worth implementing, because I bet more than 25% of your sites visitors will benefit from WebP. Not offering this to them, is wasting additional bandwidth.

Some emails are also displaying WebP

It could be that your transactional email templates are including XML layout handles that suddenly introduce this extensions functionality, while actually adding WebP to emails is a bad idea (because most email clients will not support anything of the like).

If you encounter such an email, find out which XML layout handle is the cause of this ({handle layout="foobar"}). Next, create a new XML layout file with that name (foobar.xml) and call the webp_skip handle from it (<update handle="webp_skip" />). So this instructs the WebP extension to skip loading itself.

How do I prevent a specific <img> from being converted into a <picture> element?

Add the attribute data-nextgenimages-skip to the image (<img src="..." data-nextgenimages-skip>). This requires a version of Yireo NextGenImages that supports this attribute (newer than 0.5.18). Image HTML inside a <script> (like var html = '<img src="...">';) or a <textarea> is never converted (#133). To skip an entire page, use the webp_skip handle as described above.

error while loading shared libraries: libjpeg.so.8: cannot open shared object file: No such file or directory

Ask your system administrator to install this library. Or ask the system administrator to install WebP support in PHP by upgrading PHP itself to the right version and to include the right PHP modules (like imagemagick). Or skip converting WebP images by disabling the setting in our extension and then convert all WebP images by hand.

Can I use this with Amasty Shopby?

Yes, you can. Make sure to install the addition https://github.com/yireo/Yireo_Webp2ForAmastyShopby as well.

Does this work with image optimization parameters (Nginx image_filter)?

Yes. When Magento is configured to use image optimization parameters (Stores > Configuration > General > Web > Url options > Catalog media URL format), image URLs contain query parameters like ?width=320&height=320. These query parameters are kept in the URLs of the generated WebP images (requires yireo/magento2-next-gen-images with query string support). Make sure that your webserver or CDN also resizes WebP images: For instance, with Nginx, the location block that applies image_filter should also match .webp (and Nginx 1.11.6 or later is required for WebP support in image_filter).

How can I convert WebP images manually from the CLI?

Even though this extension (or actually its parent extension Yireo_NextGenImages) support a CLI (bin/magento next-gen-images:convert), using this extension for 1000s of images will definitely not be performant. If you want to convert all images at once from the CLI, it is better to look at other tools. For instance, the cwebp binary (part of the Google WebP project itself) could be installed. Or perhaps you could use convert of the Imagick project.

Next, a simple shell script could be used to build all WebP files. Note that (with the setting Add image hash disabled) the WebP file keeps the full source filename, so foo.jpg becomes foo.jpg.webp:

find . -type f -name \*.jpg | while read IMAGE do; convert $IMAGE $IMAGE.webp; done

Or another example (of @rostilos):

#!/bin/bash
start=`date +%s`
directory="../pub/media"

cd "$directory" || exit
find . -type f \( -iname \*.jpg -o -iname \*.jpeg -o -iname \*.png \) -print0 |

while IFS= read -r -d $'\0' file;
  do
    filename=$(basename -- "$file")
    new_filename="${filename}.webp"
    new_filepath="$(dirname "$file")/$new_filename"
    echo "Converting: $file -> $new_filepath"
    cwebp -q 80 -quiet "$file" -o "$new_filepath"
  done
end=`date +%s`
runtime=$((end-start))

echo "Execution completed in $runtime seconds."

Requesting support

First check to see if our extension is doing its job

Before requesting support, please make sure that you properly check whether this extension is working or not. Do not look at your browser and definitely do not use third party SEO tools to make any conclusions. Instead, inspect the HTML source in your browser. Our extension modifies the HTML so that regular <img> tags are replaced with something similar to the following:

<picture>
  <source type="image/webp" srcset="example.webp">
  <source type="image/png" srcset="example.png">
  <img src="example.png" />
</picture>

If similar HTML is there, but your browser is not showing the WebP image, then realize that this is not due to our extension, but due to your browser. Unfortunately, we are not browser manufacturers and we can't change this. Refer to https://caniuse.com/#search=webp instead.

Opening an issue for this extension

Feel free to open an Issue in the GitHub project of this extension. However, do make sure to be thorough and provide as many details as you can:

  • What browser did you test this with?
  • What is your Magento version?
  • What is your PHP version?
  • Did you make sure to use composer to install all dependencies?
    • Not using composer is not supported.
  • Which specific composer version of the Yireo WebP2 extension did you install?
  • Which specific composer version of the Yireo NextGenImages extension did you install?
  • Have you tested this after flushing all caches, especially the Full Page Cache?
  • Have you tested this after disabling all caches, especially the Full Page Cache?
    • The Full Page Cache will prevent you from seeing dynamic results. The Full Page Cache works with our extension, it just pointless to troubleshoot things with FPC enabled, unless you are troubleshooting FPC itself.
  • Are you testing this in the Developer Mode or in the Production Mode?
    • We recommend you to use the Developer Mode. Do not use the Default Mode anywhere. Do not use the Production Mode to make modifications to Magento.
  • Under Stores > Configuration > Yireo > Yireo WebP2 and Stores > Configuration > Yireo > Yireo NextGenImages, what settings do you see with which values?
    • Preferably post a screenshot with the settings.
  • Could you supply a URL to a live demo?
  • Could you please supply a snapshot of the HTML source of a product page?

Please note that we do not support Windows to be used to run Magento. Magento itself is not supporting Windows either. If you are stuck with Windows, make sure to use the WSL (Windows Subsystem for Linux) and use the Linux layer instead.

If the WebP configuration section is showing in the backend, but the HTML source in the frontend is not modified, please send the output of the command bin/magento dev:di:info "\Magento\Framework\View\LayoutInterface".

Issues with specific images

If some images are converted but others are not, please supply the following details:

  • The output of the command bin/magento next-gen-images:test-uri $URL where $URL is the URL to the original image (JPG or PNG)
  • The output of the command bin/magento next-gen-images:convert $PATH where $PATH is the absolute path to the original image (JPG or PNG)
  • Whether or not you are using a CDN for serving images.

Current status

Static Tests Unit Tests Integration Tests Playwright DI Compilation