Convert to WebP Format - The Successor of JPEG

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.
web browser support
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.
ebay using webp
YouTube uses WebP format in certain large thumbnails. Below is an example we grabbed from YouTube’s homepage.
youtube webp format

Using Rewrite Rules to Deliver WebP#

Rewrite rules allow you to deliver WebP images by changing the MIME type of an image. However, this is not the recommended method to deliver WebP images since it can cause conflicts if also using a CDN (due to the current state of WebP browser compatibility). Therefore, for WordPress users, we recommend using the Cache Enabler plugin as outlined further below.
However, in the event that you still need to deliver WebP images and aren’t using WordPress nor a CDN, then you can experiment with the following rules.

RewriteRules for Apache (.htaccess)#

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="image.jpg" alt="Your Image">). The distribution of the correct image is ensured by the RewriteRule in the system file .htaccess:
<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 (file format) does not change.
Important: The snippet above .htaccess needs to be positioned relatively high up.

RewriteRules for Nginx (nginx.conf)#

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.

RewriteRules 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. WordPress Cache Enabler then delivers WebP images based on browser support.
ImageFile NameOriginal SizeCompressed JPGWebP FormatSize Difference
jpg to webp 1
jpg-to-webp-1.jpg480 KB407 KB43 KB89%
jpg to webp 2
jpg-to-webp-2.jpg659 KB578 KB113 KB80%
jpg to webp 3
jpg-to-webp-3.jpg787 KB715 KB127 KB82%
jpg to webp 4
jpg-to-webp-4.jpg617 KB543 KB61 KB88%
jpg to webp 5
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.
jpg to webp

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.io is one of the best solutions. Optimus offers a developer API to convert JPEG or PNG images to WebP. The service Optimus, which is powered by KeyCDN, allows to integrate the conversion process directly in your application. The service also has a WordPress plugin that converts images during the upload process. You can then combine it with the free WordPress 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.
wordpress webp images

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.io
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.

Post a Comment

0 Comments