如何检测支持WebP的浏览器以及如何为它们提供WebP图片?

发布于 2024-12-17 00:19:51 字数 369 浏览 3 评论 0原文

看到 WebP 图片尺寸的惊人减小(对于无损 + alpha 图片,比“pngcrushed”PNG 小约 28%),我们希望向支持 WebP 的浏览器提供 WebP 图片。

如何从 Java webapp 服务器检测客户端浏览器是否支持 WebP?

这里有一个关于如何从 JavaScript 执行此操作的问题:

Detecting WebP support

但我想知道如何从 Java 中做到这一点。如果从 Java 执行意味着在客户端调用 JavaScript,那么我想知道如何做到这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-12-24 00:19:51

有一种简单的方法可以检测客户端上的 webP 支持。然后,您可以在客户端上设置 cookie 并读取服务器上的值。

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

更多信息请参见此处

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.

忘年祭陌 2024-12-24 00:19:51

您无法仅使用 Java 来确定 Web 浏览器功能。 Java/JSP 在 Web 服务器中运行,而不是在 Web 浏览器中运行。为此,您确实需要使用 JavaScript。您可以使用已在相关问题中找到的代码。你能做的最简单的事情就是让 JS 检查页面加载是否还没有一些 cookie 表明浏览器支持 WebP,然后进行相应的功能检测,最后通过 document.cookie 设置一个长期存在的 cookie并通过location.reload()重新加载页面。

在服务器端,您只需通过 servlet 中的 HttpServletRequest#getCookies()${cookie} 检查 cookie 是否存在和/或其值在 JSP 中发送/设置适当的图像 URL 之前。

除了设置 cookie 之外,您始终可以让 JS 将数据作为 ajax 请求的请求参数发送,但这意味着您必须在每个请求或会话中执行此操作。

You can't determine web browser features using Java alone. Java/JSP runs in webserver, not in webbrowser. You really need to use JavaScript for this. You can use the code for this which you already found on the related question. Easiest what you can do, is to let JS check on page load if there isn't already some cookie present indicating that the browser supports WebP and then do the feature detection accordingly and finally set a long living cookie by document.cookie and reload the page by location.reload().

In the server side, you'd just have to check for the presence of the cookie and/or its value by HttpServletRequest#getCookies() in servlet or ${cookie} in JSP before sending/setting the appropriate image URL.

Other than setting a cookie, you can always let JS to send the data as request parameters of an ajax request, but this means that you have to do it on every request or session.

小姐丶请自重 2024-12-24 00:19:51

上面的答案看起来有点老了?看起来您可以检查 Accept 标头以查看它是否包含 webp 字符串...

   public boolean hasWebPSupport(HttpServletRequest req) {
        return req.getHeader("Accept").contains("image/webp");
    }

The answers above seem kinda old? Looks like you can check the Accept header to see if it contains the webp string...

   public boolean hasWebPSupport(HttpServletRequest req) {
        return req.getHeader("Accept").contains("image/webp");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文