queryj

On the path to javascript enlightenment.

Detecting webP support

with 7 comments

If you are loading images on the fly, it may be worthwhile detecting support for Google’s webP format. It offers size reduction of 30 – 40% over png and jpeg without loss in quality. You can use lossless or lossy compression too.

To feature detect, use this code:

function testWepP(callback) {
    var webP = new Image();     
     webP.src = 'data:image/webp;base64,UklGRi4AAABXRUJQVlA4TCEAAAAvAUAAEB8wAiMw' + 
'AgSSNtse/cXjxyCCmrYNWPwmHRH9jwMA';
     webP.onload = webP.onerror = function () {
         callback(webP.height === 2);     
     }; 
};

testWebP(function(supported) {
     console.log((supported) ? "webP 0.2.0 supported!" : "webP not supported."); 
});

No image required – it uses a data uri to determine support.

About these ads

Written by jameswestgate

June 11, 2012 at 10:52 am

Posted in Javascript, jpeg, png, webp

7 Responses

Subscribe to comments with RSS.

  1. what exactly does this do? does it just optimize the images on page for google?

    chatterb0tX

    June 18, 2012 at 1:32 pm

    • webP is a new image format supported in Chrome and Opera – the key benefit is that images are about 30% smaller for the same quality – so if you have large images or if you have lots of images then it may be worth detecting webP support. The nice thing about this feature detect is that if in the future more browsers support webP they will automatically switch over. You have to remember to save your images as webP though and switch your img src to use .webp using script.

      jameswestgate

      June 18, 2012 at 1:43 pm

      • hey thanks for your quick reply glad i checked back, this seems pretty interesting i will test this out on a site as i have lots of png images.

        i can see it definitely doing wanders for some sites.

        chatterb0tX

        June 18, 2012 at 6:18 pm

      • but what about other browsers? If I start using webp on my site and people using explorer or firefox come and visit it will they still be able to see them? I´m a photographer, you see, so I´m having trouble with having low size images with good resolution withouth having google webmaster tools telling me to optimize image size.

        Ozmar Fraga

        September 26, 2012 at 7:17 am

      • Hi Ozmar – that is what the script is for – it feature detects for .webp and only uses that if the browser supports it. It does mean you have to create both .jpg and .webp versions of your images though ..

        See my previous posts on how to progressively enhance tags to load your preferred image on the fly

        jameswestgate

        October 10, 2012 at 7:38 am

  2. Hi,

    there’s few typos in the scripts. Also: is the base64-encoded webp data coming from libwebp 0.2.0 or a previous version?

    I’ve posted a corrected version of the script here:
    https://groups.google.com/a/webmproject.org/group/webp-discuss/msg/7dfaea4f23d52dfb

    skal

    skal

    August 31, 2012 at 12:33 pm

    • Thanks for this. I have updated the post with the latest code.

      jameswestgate

      October 10, 2012 at 7:45 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 104 other followers

%d bloggers like this: