14

I am planning to use WebP on my E-Commerce website.It can boost a performance a lot based on Lighthouse test. But the problem is. we still have user that use iOS which does't have support for WebP format. I need more information about the proper way to deliver the images also how to let user download the images in JPG.

On my server. I have both formats for fallback purpose.

Narendra
  • 4,514
  • 2
  • 21
  • 38
L.885
  • 425
  • 1
  • 5
  • 12
  • 2
    `I read some reference` what reference, so we don't just double up on the information you've already *researched* – Jaromanda X Dec 24 '18 at 03:39
  • **See Also**: [Cross-browser Webp images support](https://stackoverflow.com/q/53206746/1366033) – KyleMit Oct 08 '20 at 18:48

5 Answers5

23

The easiest way to use picture element. The browser will consider each child element and choose the best match among them; if no matches are found, the URL of the element's src attribute is selected.

<picture>
    <source srcset="/media/examples/surfer-240-200.webp" type="image/webp">
    <img src="/media/examples/painted-hand-298-332.jpg" />
</picture>

EDIT: As per Jaromanda's suggestion, we should look for fallback in img tag itself as internet explorer doesn't support picture element.

 <img src="image.webp" onerror="this.onerror=null; this.src='image.png'">

if we want to make sure that browser only downloads one file: a WebP or a fallback image; not both. We can use data-in attributes and assign the appropriate source based one browser support but in that we need to check for os/browser upfront.

 <img data-jpg="image.jpg" data-webp="image.webp" id="myimg"> 

and in JS

let img = document.getElementById('myimg');
 if(supportedBrowser){
   img.src = img.getAttribute('data-webp');
 }else{
   img.src = img.getAttribute('data-jpg');
 }
Regolith
  • 2,944
  • 9
  • 33
  • 50
Narendra
  • 4,514
  • 2
  • 21
  • 38
  • 3
    Also, Internet Exploder will effectively just display the ` – Jaromanda X Dec 24 '18 at 03:52
  • 1
    True, Also Apple is typically resistant to supporting formats founded by rivals, so in ios devices, even chrome also can't help as it must use Safari engine. – Narendra Dec 24 '18 at 03:57
3

To serve WebP images with HTML elements, you can use <picture>

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

If have a large number of pages or too little time to edit HTML code, then Apache's mod_rewrite module can help us automate the process of serving .webp images to supporting browsers. Edit or create if the file doesn't exist .htaccess

odule mod_rewrite.c>
  RewriteEngine On 
  RewriteCond %{HTTP_ACCEPT} image/webp
  RewriteCond %{REQUEST_URI}  (?i)(.*)(\.jpe?g|\.png)$ 
  RewriteCond %{DOCUMENT_ROOT}%1.webp -f
  RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1\.webp [L,T=image/webp,R] 
</IfModule>

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

AddType image/webp .webp

For more information click Here

Vinesh Goyal
  • 607
  • 5
  • 8
  • Can .htaccess server a jpg for iOS browser and webp for another browser that supported? – L.885 Dec 24 '18 at 05:40
  • It depends on browsers and `RewriteCond %{HTTP_ACCEPT} image/webp` condition will check the browser compatibility. When a browser makes a request, it includes a header to indicate to the server what the browser is capable of handling. In the case of WebP, the browser will send an Accept header containing image/webp. We will check if the browser sent that header using RewriteCond, which specifies the criteria that should be matched in order to carry out the RewriteRule. – Vinesh Goyal Dec 24 '18 at 06:16
  • Safari will never support WebP imho – L.885 Dec 24 '18 at 06:33
  • There are already hundreds of JPG images existing on server. Is there any way to convert these images in to .WebP format on server, or do we need to re-upload them? @VineshGoyal – Bhargav Joshi Dec 30 '18 at 06:13
  • 1
    No, you don't need re-upload images, you can generate WEBP images from JPG with the help of PHP or NODE tools. try https://www.npmjs.com/package/gulp-webp @BhargavJoshi – Vinesh Goyal Dec 31 '18 at 05:25
  • @BhargavJoshi even though this is an old question, you can use ImageMagick for that purpose. – Example person Dec 24 '20 at 04:30
1

Another approach is to use webp-hero: WebP images are displayed also on browser that don't support them. With this method, JPEG images are generated on-the-fly, you don't have to hold your images in both formats on your server!

<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script src="https://unpkg.com/webp-hero@0.0.0-dev.27/dist-cjs/polyfills.js"></script>
<script src="https://unpkg.com/webp-hero@0.0.0-dev.27/dist-cjs/webp-hero.bundle.js"></script>
<script>
    var webpMachine = new webpHero.WebpMachine()
    webpMachine.polyfillDocument()
</script>
</head>
<body>
This WebP image is also visible on Internet Explorer 10 and 11<br><br>
<IMG SRC="https://www.gstatic.com/webp/gallery/1.webp">
</body>
</html>

Live example: https://codepen.io/niente0/pen/dyvQQvO

Niente0
  • 504
  • 3
  • 11
0

As a suggestion:

The redirect rule makes the browser to download both JPG and webp format, which I suppose is not intended. In order to avoid this, the last letter of the redirect rule shoulbe be changed from [R] - redirecto - to [L]

RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1\.webp [L,T=image/webp,R] 


RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1\.webp [L,T=image/webp,L] 
István
  • 21
  • 5
-1

I just made a custom helper method that is used exactly like the built in image_tag helper, but will fallback to png if webp is not supported by the browser.

def image_tag_dynamic(source, options = {})

        options = options.symbolize_keys

        filepath_webp = source + ".webp"
        filepath_png = source + ".png"

        content_tag :picture do
            # the order of the <source> tags determines priority of image format. Browsers not supporting webp will default to png.
            content_tag(:source, srcset: image_path(filepath_webp)) do end + 
            content_tag(:source, srcset: image_path(filepath_png)) do end +
            image_tag(filepath_png, alt: options[:alt], title: options[:title], width: options[:width], height: options[:height], class: options[:clazz])

        end 
    end