Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect browsers supporting WebP and how to serve them WebP pictures?

Tags:

java

jsp

webp

Seen the amazing size reduction of WebP pictures (about 28% smaller than "pngcrushed" PNG for lossless + alpha pictures), we'd like to serve WebP pictures to browsers supporting WebP.

How can I detect from a Java webapp server if the client's browser is supporting WebP?

There's been a question here about how to do it from JavaScript:

Detecting WebP support

But I'd like to know how to do it from Java. If doing from Java means calling JavaScript on the client side then I'd like to know how to to that.

like image 680
Cedric Martin Avatar asked Dec 28 '25 15:12

Cedric Martin


1 Answers

There is a simple way of detecting webP support on the client. You could then set a cookie on the client and read the value on the server.

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."); 
});

More information here.

like image 171
James Westgate Avatar answered Dec 31 '25 04:12

James Westgate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!