简体   繁体   English

右击“另存为”选择jpg图片而不是webp,但是显示的是webp

[英]Right click "save as" selects jpg image instead of webp, but webp is displayed

I'm using .htaccess to serve webp images to browsers that support it.我正在使用 .htaccess 向支持它的浏览器提供 webp 图像。

RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{DOCUMENT_ROOT}/$1.webp -f
RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]
Header append Vary Accept env=REQUEST_image
AddType image/webp .webp

Source: WebP images with htaccess 来源:带有 htaccess 的 WebP 图片

Basically what it does is return a .webp image instead of the requested jpg/png/gif, from the same location, if it exists and is supported.基本上它所做的是从同一位置返回一个 .webp 图像而不是请求的 jpg/png/gif,如果它存在并且受支持。

This works fine in Chrome, Firefox, IE11, Edge... Haven't tested Safari.这在 Chrome、Firefox、IE11、Edge 中运行良好……尚未测试 Safari。

My issue is however that when I right click on the website, "save as", it automatically selects a .jpg version.然而,我的问题是,当我右键单击网站时,“另存为”,它会自动选择 .jpg 版本。 If a .jpg version doesn't exist it tries to save the whole html.如果 .jpg 版本不存在,它会尝试保存整个 html。 Intended behaviour would be for it to save the displayed .webp version, if it is used.预期行为是它保存显示的 .webp 版本(如果使用)。 Weirdly enough it does try to save the .webp in Firefox when doing the same, but when I try the same thing using Page info in Firefox (ctrl+i on Windows), it selects the .jpg.奇怪的是,在执行相同操作时,它确实尝试在 Firefox 中保存 .webp,但是当我在 Firefox 中使用页面信息(Windows 上的 ctrl+i)尝试相同的操作时,它选择了 .jpg。

How is this even possible?这怎么可能? Is there something missing/wrong with this?这有什么遗漏/错误吗?

I checked that the displayed image is an actual webp, and it is.我检查了显示的图像是否是实际的 webp,确实如此。 Both content-type and file size matches that of the webp.内容类型和文件大小都与 webp 相匹配。

my working solution is:我的工作解决方案是:


Check if browser supports WebP images检查浏览器是否支持 WebP 图片

RewriteCond %{HTTP_ACCEPT} image/webp

Check if WebP replacement image exists检查 WebP 替换图像是否存在

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} -f

Serve WebP image instead改为提供 WebP 图像

RewriteRule (.+)\.(jpe?g|png|gif)$ $1.webp [T=image/webp,E=REQUEST_image]

so try to change RewriteCond %{DOCUMENT_ROOT}/$1.webp -f to所以尝试将RewriteCond %{DOCUMENT_ROOT}/$1.webp -f更改为

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} -f

Tested on Safari and Firefox.在 Safari 和 Firefox 上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM