简体   繁体   English

WebP 后备图像未加载

[英]WebP fallback images not loading

I'm using a <picture> tag to set a responsive image with WebP support.我正在使用<picture>标签来设置支持 WebP 的响应式图像。 I'm pulling a desktop image and mobile image URLs that have both .web and .jpg files.我正在提取具有.web.jpg文件的桌面图像和移动图像 URL。 for each media, I'm giving the .webp version and the .jpg version.对于每个媒体,我提供.webp版本和.jpg版本。

What I expect is that if the .webp version does not exist is that the website will take the .jpg file that exists.我期望的是,如果.webp版本不存在,则该网站将采用存在的 .jpg 文件。

any idea what is wrong here?知道这里有什么问题吗?

$image_desktop = get_field( 'desktop_image' ); // can be an .webp image or .jpg image
$image_mob     = get_field( 'mobile_image' ); // can be an .webp image or .jpg image
<picture>
  <source media="(min-width: 480px)"
  srcset="<?php echo esc_url( $image_desktop['url'] . '.webp' ); ?>"
  type="image/webp">

  <source srcset="<?php echo esc_url( $image_mob['url'] . '.webp' ); ?>" 
  type="image/webp">

  <source media="(min-width: 480px)" srcset="<?php echo esc_url( $image_desktop['url'] ); ?>"
  type="image/jpeg">

  <source srcset="<?php echo esc_url( $image_mob['url'] ); ?>" type="image/jpeg">

  <img class="header-image"
  src="<?php echo esc_url( $image_desktop['url'] ); ?>"
  alt="<?php echo esc_attr( $image_desktop['url'] ); ?>">
</picture>

All URLs that you put in the srcset attributes have to be valid and have to exist.您放在srcset属性中的所有 URL 都必须有效并且必须存在。

Browsers that have support for WebP will try to load the URL from the <source type="image/webp"> and all other browsers will try to load the URL from <source type="image/jpeg"> .支持 WebP 的浏览器将尝试从<source type="image/webp">加载 URL,所有其他浏览器将尝试从<source type="image/jpeg">加载 URL。 If the URL selected by the browser does not exist it will not fall back to one of the other <source> s.如果浏览器选择的 URL 不存在,它不会回退到其他<source>

You should check the existence of the image files on the server side.您应该检查服务器端的图像文件是否存在。

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

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