通用API文档

响应式图片

💡 云策文档标注

概述

自 WordPress 4.4 起,通过为生成的图片标记添加 srcset 和 sizes 属性,原生支持响应式图片。这允许浏览器根据设备屏幕大小自动选择并加载合适的图片尺寸,优化带宽使用和页面加载速度。

关键要点

  • WordPress 自动为上传的图片创建多个尺寸(如大、中、缩略图),并支持通过 srcset 和 sizes 属性实现响应式图片。
  • 响应式图片策略将图片尺寸信息发送给浏览器,由浏览器根据视口宽度和显示密度智能选择加载,而非在服务器端决策。
  • 新增函数如 wp_get_attachment_image_srcset() 和 wp_calculate_image_sizes() 用于计算和添加 srcset 和 sizes 属性。
  • 引入了新的默认中间尺寸 medium_large(默认宽度 768px),以更好地利用响应式图片支持。
  • 可通过 wp_calculate_image_srcset 和 wp_calculate_image_sizes 过滤器自定义默认属性,或使用 wp_get_attachment_image_attributes 过滤器覆盖非嵌入图片的属性。

代码示例

<?php
$img_src = wp_get_attachment_image_url( $attachment_id, 'medium' );
$img_srcset = wp_get_attachment_image_srcset( $attachment_id, 'medium' );
?>
<img src="<?php echo esc_url( $img_src ); ?>"
     srcset="<?php echo esc_attr( $img_srcset ); ?>"
     sizes="(max-width: 50em) 87vw, 680px" alt="Foo Bar">

注意事项

  • 如果内容 HTML 中已存在 srcset 或 sizes 属性,WordPress 不会添加或修改它们,以确保与现有标记兼容。
  • 响应式图片没有可配置的设置,所有操作在后台自动完成。
  • max_srcset_image_width 过滤器可用于限制 srcset 属性中的最大图片宽度,默认值为 2048px。
  • medium_large 尺寸不会在插入图片的 UI 中显示,也无法通过媒体设置页面更改,但开发者可使用 update_option() 函数修改其宽度。

📄 原文内容

Since WordPress 4.4, native responsive images is supported by including srcset and sizes attributes to the image markup it generates. For background on this feature, read the merge proposal.

Some History

When users upload images in WordPress, it automatically crops new images to smaller sizes. For example, if you upload an image that’s 1500 x 706, the image sizes might look like this:

  • Full Size – 1500 x 706
  • Large – 500 x 235
  • Medium – 300 x 141
  • Thumbnail – 150 x 150

So WordPress automatically creates several sizes of each image uploaded to the media library. Additional sizes are created depending on the theme. If the full size image is attached to a post, users on desktop and mobile devices will see the full size image. However, it doesn’t make sense to use the full size image on mobile devices because of its display and file size.

Before responsive design was popular, many sites attempted to dynamically serve different layouts (including images) to browsers based on the device type (e.g. phone, tablet, etc.). In these cases, all of the dynamic stuff happened at the server, before the page was rendered. This strategy is usually associated with the term adaptive design.

Responsive design, on the other hand, uses tools like media queries to allow a single page to be rendered that will respond in the browser based on things like viewport width and display density.

Responsive images follows the second strategy and sends all of the information to the browser up front and lets the browser take care of loading the appropriate image rather than making those decisions on the server before the page is loaded.

How it works

By including the available sizes of an image into a srcset attribute, it allows the software to automatically use and display the right image based on a device’s screen size. If you attach a full size 1500 x 706 image to a post in WordPress, mobile devices will see the large or medium-sized image instead—potentially saving bandwidth and speeding up page load times in the process.

Note that for compatibility with existing markup, neither srcset nor sizes are added or modified if they already exist in content HTML. Responsive images don’t have any settings to configure as the magic happens behind the scenes.

Browser side

To help browsers select the best image from the source set list, WordPress also include a default sizes attribute that is equivalent to (max-width: {{image-width}}px) 100vw, {{image-width}}px. While this default will work out of the box for a majority of sites, themes should customize the default sizes attribute as needed using the wp_calculate_image_sizes filter.

A normal browser request goes to server, server sends back response. This response includes links to other resources – fonts, css, JS, and images. The browser notices these resources, and sends additional requests to the server and fetches those resources.

What this responsive image approach does is provide additional attributes to the image tag that alerts the browser to the different image files available for that particular image tag so that the browser can then intelligently request the right image file (source) for whatever window/viewport size or even resolution support the browser has. This means the browser can request the right “sized” image file for an image instead of being served an overly large image and resizing down to fit the space after the fact.

For a full overview of how srcset and sizes works, read Responsive Images in Practice, by Eric Portis over at A List Apart.

New functions and hooks

To implement this feature, the following new functions were added to WordPress:

  • <a href="https://developer.wordpress.org/reference/functions/wp_get_attachment_image_srcset/">wp_get_attachment_image_srcset</a>() – Retrieves the value for an image attachment’s srcset attribute.
  • <a href="https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/">wp_calculate_image_srcset</a>() – A helper function to calculate the image sources to include in a srcset attribute.
  • <a href="https://developer.wordpress.org/reference/functions/wp_get_attachment_image_sizes/">wp_get_attachment_image_sizes</a>() – Creates a sizes attribute value for an image.
  • <a href="https://developer.wordpress.org/reference/functions/wp_calculate_image_sizes/">wp_calculate_image_sizes</a>() – A helper function to create the sizes attribute for an image.
  • <a href="https://developer.wordpress.org/reference/functions/wp_image_add_srcset_and_sizes/">wp_image_add_srcset_and_sizes</a>() – Adds srcset and sizes attributes to an existing img element.

As a safeguard against adding very large images to srcset attributes, a <a href="https://developer.wordpress.org/reference/hooks/max_srcset_image_width/">max_srcset_image_width</a> filter has been added, which allows themes to set a maximum image width for images include in source set lists. The default value is 2048px.

A new default image size

A new default intermediate size, medium_large has been added to better take advantage of responsive image support. The new size is 768px wide by default, with no height limit, and can be used like any other size available in WordPress. As it is a standard size, it will only be generated when new images are uploaded or sizes are regenerated with third party plugins.

The medium_large size is not included in the UI when selecting an image to insert in posts, nor are we including UI to change the image size from the media settings page. However, developers can modify the width of this new size using the update_option() function, similar to any other default image size.

Customizing responsive image markup

To modify the default srcset and sizes attributes,  you should use the wp_calculate_image_srcset and wp_calculate_image_sizes filters, respectively.

Overriding the srcset or sizes attributes for images not embedded in post content (e.g. post thumbnails, galleries, etc.), can be accomplished using the <a href="https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/">wp_get_attachment_image_attributes</a> filter, similar to how other image attributes are modified.

Additionally, you can create your own custom markup patterns by using wp_get_attachment_image_srcset() directly in your templates. Here is an example of how you could use this function to build an <img> element with a custom sizes attribute:

<?php
$img_src = wp_get_attachment_image_url( $attachment_id, 'medium' );
$img_srcset = wp_get_attachment_image_srcset( $attachment_id, 'medium' );
?>
<img src="<?php echo esc_url( $img_src ); ?>"
     srcset="<?php echo esc_attr( $img_srcset ); ?>"
     sizes="(max-width: 50em) 87vw, 680px" alt="Foo Bar">

Need more developer details?
Learn more about customizing responsive images markup on this GitHub repository.

Sources