Convert to WebP Format - The Successor of JPEG

By Sven Baumgartner
Updated on September 4, 2022
Convert to WebP Format - The Successor of JPEG

Web users well-versed in technology have surely heard of WebP - an image format developed by Google to ensure superior compression of photos. JPEG is considered outdated and too inefficient. The WebP format is a sophisticated alternative method for convenient image compression that is now entering the market. JPEG vs. WebP: reductions in file size of up to 80% are commonly achievable.

Browser support for WebP format

Sounds promising: higher quality than JPEGs, with much smaller files. But every light has its shadow. Browser support is the biggest current disadvantage of the WebP technology. According to caniuse.com, at this time, Opera and Chrome support the modern image format WebP. Firefox has announced support for WebP images in the future and it is still being discussed in this thread. Internet Explorer and Safari are left out completely.

Nevertheless, it is worth it to deliver images in WebP format to the browsers named above. At the moment there are two browsers that officially support the format, but soon there will be three with WebP support - if Firefox sticks to their plan. Looking at the statistics of your own websites lets you know whether it will be worth it for your project to include images in two formats.

WebP for Chrome & Opera - JPEG for all other browsers.

WebP in the wild

Several big names like Google, Facebook, and eBay are campaigning for the use of WebP and are even actively using it. Here is an example we grabbed from eBay's homepage. At the time of writing this, about 95% of the images on their homepage were in the WebP format.

YouTube uses WebP format in certain large thumbnails. Below is an example we grabbed from YouTube's homepage.

Using rewrite rules to deliver WebP

Rewrite rules allow you to deliver WebP images by changing the MIME type of an image. For WordPress users, we recommend using the Cache Enabler plugin instead as outlined further below.

However, in the event that you need to deliver WebP images and aren't using WordPress, then you can experiment with the following rules. If you're using a CDN you must ensure the CDN supports caching based on the Accept request header. KeyCDN supports WebP caching.

Rewrite rule for Apache

A forwarding rule on the server level takes care of distributing the requests for the correct file formats, independently of the browser. In plain terms, the embedding of pictures in the HTML source code does not change in any way and is not affected (e.g. <img src="/img/example.jpg" alt="example">). The distribution of the correct image is ensured by the rewrite rule in the .htaccess server configuration file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_ACCEPT} image/webp
    RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
    RewriteRule ^(path/to/your/images.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]
</IfModule>

<IfModule mod_headers.c>
    Header append Vary Accept env=REDIRECT_accept
</IfModule>

AddType image/webp .webp

When a JPEG or PNG image is displayed in the browser, it is exchanged for a WebP file by the server and assigned the MIME type image/webp. The file extension does not change.

Note: The snippet above in the .htaccess needs to be positioned relatively high up.

Rewrite rule for Nginx

Analogously to the Apache web server, there is a solution for Nginx:

# http config block
map $http_accept $webp_ext {
    default "";
    "~*webp" ".webp";
}

# server config block
location ~* ^(/path/to/your/images/.+)\.(png|jpg)$ {
    set $img_path $1;
    add_header Vary Accept;
    try_files $img_path$webp_ext $uri =404;
}

The image/webp web; entry in the Nginx system files mime.types is very important in this context-that's how Nginx sends the correct MIME type to the browser.

Rewrite rule for IIS

And here are the rules for IIS.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="webp">
                <match url="(.+)\.(jpe?g|png)$" ignoreCase="false" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_ACCEPT}" pattern="image/webp" ignoreCase="false" />
                    <add input="{DOCUMENT_ROOT}/{R:1}.webp" matchType="IsFile" />
                </conditions>
                <action type="Rewrite" url="{R:1}.webp" logRewrittenUrl="true" />
              <serverVariables>
                  <set name="ACCEPTS_WEBP" value="true" />
              </serverVariables>
            </rule>
        </rules>

        <outboundRules>
          <rule name="Set Vary header" preCondition="IsWebp">
            <match serverVariable="RESPONSE_Vary" pattern=".*" />
            <action type="Rewrite" value="Accept"/>
          </rule>
          <preConditions>
             <preCondition name="IsWebp">
              <add input="{ACCEPTS_WEBP}" pattern="true" ignoreCase="false" />
             </preCondition>
            </preConditions>
        </outboundRules>

    </rewrite>
    <staticContent>
        <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>
</system.webServer>

Cost-benefit ratio

Back of the envelope calculation: two file formats per image. For CMS systems such as WordPress, multiply that with the number of all created file sizes. That sounds complicated and like a lot of work. What's the point of all of that? Is the effort worth it? You might come to different decisions based on the project and the target group.

The fact of the matter is: smaller image file sizes lead to faster website load times. We have mentioned several times that website performance is indisputably a factor affecting SEO. Readers are also happy when the blog sites they visit are displayed quickly.

The savings in traffic should also not be forgotten: depending on the project, a tidy sum can quickly accumulate. Having to deliver fewer bytes ultimately means that the server has to shoulder a smaller burden.

WebP vs JPEG resulted in a 77% decrease in page size! And WebP vs PNG, a 27% decrease.

- WebP Case Study

JPG to WebP test

We ran a test in WordPress with 5 large JPG images to demonstrate how much compression actually takes place and the significant size difference between .jpg and .webp files. We are using lossless compression with Optimus to optimize the images and also convert to WebP format upon upload to the media library. The Cache Enabler plugin then delivers WebP images based on browser support.

ImageFile nameOriginal sizeCompressed JPGWebP formatSize difference
jpg-to-webp-1.jpg480 KB407 KB43 KB89%
jpg-to-webp-2.jpg659 KB578 KB113 KB80%
jpg-to-webp-3.jpg787 KB715 KB127 KB82%
jpg-to-webp-4.jpg617 KB543 KB61 KB88%
jpg-to-webp-5.jpg605 KB543 KB70 KB87%

We then ran a page comparison test with GTmetrix, JPG vs WebP, and you can see the total difference in page size. WebP resulted in a 77% decrease in page size.

Tools

Currently there is a rather small selection of tools available for the creation of WebP graphics-understandable considering that the method is still in its infancy and many development statuses are marked as experimental.

WordPress WebP

If you are wanting to convert images in WordPress to WebP format Optimus is one of the best solutions. The service also has a WordPress plugin that converts images during the upload process. You can then combine it with the free Cache Enabler plugin which will automatically deliver WebP images in Chrome and PNG and or JPEG in Firefox and IE. It does this by creating to separate cached files. You can see below we are using this setup on this blog and and so all of our images are WebP.

Analysis

If WebP variants of images were placed on the server and the forwarding rule was added to the .htaccess file, the optimized image files are ready to be delivered. But how exactly is it controlled whether the WebP images were actually delivered to the selected browser? After all, there is no visual difference within the browser: JPEG and WebP images look almost identical.

The answer is simple: the graphics are always returned to the browser by the server in conjunction with information on the MIME type: for JPEGs it is image/jpeg, for PNGs it is image/png and for WebP it is correspondingly image/webp. That way any type of graphics format can be easily identified and allocated, even if the file has a "foreign" file extension.

Browsers such as Chrome and Opera include so-called developer tools which allow you to display information such as the MIME type of a file.

Activating the developer tools in browsers:

Chrome: Chrome Menu/More tools/Developer tools > Tab Network
Opera: Page/Developer tools/Opera Dragenfly > Tab Network

TL;DR

  • WebP has smaller file sizes than JPEG
  • WebP looks better than JPEG even at higher compression rates
  • WebP is supported by Chrome, Opera, and soon Firefox
  • WebP speeds up load times and saves traffic
  • WebP has to be converted using tools or Optimus

A huge advantage: You can immediately start outputting WebP images in your project. No change to the embedding of graphics in the html code is necessary. The forwarding rule for .htaccess described above verifies whether there is a WebP version for the accessed (JPEG) image file on the server - if yes, WebP format will be sent to the supported browser, otherwise the default (JPEG) image is used.

  • Share

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started

Comments

Comment policy: Comments are welcomed and encouraged. However, all comments are manually moderated and those deemed to be spam or solely promotional in nature will be deleted.
  • **bold**
  • `code`
  • ```block```
KeyCDN uses cookies to make its website easier to use. Learn more