最新更新日期:Oct-31-2023
客户端提示允许 web浏览器通知服务器(或CDN层)要求的维度和像素密度利用此特征可允许单动态云式URL根据每个用户装置和浏览器响应网站的具体需求实际返回不同图像大小浏览器支持客户端提示HTTP请求信头发送提示相关响应图像自动化提示如下:sc-CH-DPR表示浏览器窗口流设备像素比)ec-ch-width表示宽度设备像素,图像将在响应布局内占有)视图港-Width表示宽度,CSS像素浏览器窗口
dpr_aut
并
w_aut
并带实值
客户端基于屏幕属性和视图港宽度自动特征记录在此允许您简化代码并执行动态决策
服务器端基于客户端提示的(CDN级别),但仅面向支持浏览器
HTML页面也必须启动客户端提示程序,这些功能可用HTML或HTTP实现要通过HTML启动客户提示程序,请在中页前置任选
,
, or
elements:
Alternatively, to enable Client-Hints via HTTP, add the following headers to your HTML document's response:
- If you are delivering images from a Private CDN or custom CNAME, replace
https://res.www.aaaalireno.com
in the snippets above with the origin of your image URLs. - Browsers will only attach client hints to requests for URLs that use the HTTPS protocol.
- In order to send the
Sec-CH-Width
hint, which is required for Cloudinary's automatic image width feature, theelement must have a
sizes
attribute.
Once Client-Hints are properly enabled, browsers attach hints to outgoing image requests, informing Cloudinary about the layout width of an image on a user’s device and the pixel density of their screen. Cloudinary can then decide on the size of the image the browser needs for displaying to the user, and select and deliver an optimal resource – all at the CDN level.
For example, to deliver the sample
image automatically scaled to the width available for the image in the responsive layout:
The code above generates the following HTML image tag:
Cloudinary automatically adapts the image to fit the viewport, layout and resolution on any device, ensuring a visually-seamless user experience while improving performance, using one of the following options:
- Automatically deliver images matching the DPR setting of the user's device, rounded up to the nearest integer.
- Automatically scale the image to match the width available for the image in the responsive layout, rounded up to the closest multiple of 100 pixels (by default).
- Automatically scale the image to match the width available for the image in the responsive layout using optimal responsive breakpoints
Automatic pixel density detection
To deliver an image in a resolution that automatically matches the DPR (Device Pixel Ratio) setting of the user's device, set the client_hints
parameter to true
and the dpr
transformation parameter to auto
(dpr_auto
in URLs). The device's DPR value is received at the CDN level and rounded up to the nearest integer in order to avoid creating extra derived images and consuming extra transformations (e.g., a DPR value of 1.5 will be rounded up to 2.0).
For example, to deliver the sample
image filled to a width of 300 pixels, a height of 200 pixels, and with a DPR value suitable for the user's device:
- The CDN returns a
Vary
response header set toSec-CH-DPR
in order to allow the same URL to be cached differently for each DPR value. - If Client-Hints are not supported by the user's browser or if they are not available, a URL with
dpr_auto
will be treated asdpr_1.0
. - Setting a value of
dpr_1.0
is treated the same way asdpr_auto
and will also be replaced with the device's DPR. If you want to forcedpr_1.0
, you should do so by removing thedpr
option from the URL completely.
Automatic image width
- Currently only works on Chromium-based browsers (e.g., Chrome, Edge, Opera, and Samsung Internet).
- For customers with a custom domain name or private CDN distribution, contact us to setup this feature.
To deliver an image automatically scaled to a width that matches the width available for the image in the responsive layout, set the:
-
width
parameter toauto
, -
crop
parameter tolimit
, -
client_hints
parameter totrue
, -
sizes
parameter to100vw
(this is the most common value to set, i.e., 100% of the available width, but you can give any value that the HTMLsizes
attribute accepts).
The width of the image is received at the CDN level and then rounded up in order to avoid creating extra derived images and consuming too many extra transformations. By default, the rounding up is made in steps of 100 pixels (e.g., 347 will be rounded up to 400 and w_auto
in this case will be treated as w_400
). To round up in steps of a different number of pixels, a numeric value can be appended to the auto
value, separated with a colon. For example, w_auto:50
means rounding up in steps of 50 pixels: 347 will be rounded up to 350 and w_auto:50
in this case will be treated as w_350
.
For example, to deliver the sample
image automatically scaled to the width available for the image in the responsive layout:
The code above generates the following HTML image tag:
Try the interactive demo to see this for yourself.
To avoid delivering large images, no matter what the width available for the image, a limit transformation can be chained with the w_auto transformation. For example, to deliver the sample
image filled to an aspect ratio of 16:9 and then automatically scaled to the width available for the image in the responsive layout (up to a maximum width of 1000 pixels):
The code above generates the following HTML image tag:
- The CDN returns a
Vary
response header set toSec-CH-Width, Sec-CH-DPR
in order to allow the same URL to be cached differently for each Width and DPR value. - If Client-Hints are not supported by the user's browser or if they are not available, by default
w_auto
will be treated as if no scaling is requested. This might mean delivering the original hi-res image dimensions, which is equivalent to omitting thewidth
parameter altogether. You can bypass this default behavior by specifying a default (fallback) value for the width that is only used if the information cannot be retrieved from a Client-Hints header. This default width value can be concatenated to the transformation value: e.g.,w_auto:40:360
means that Cloudinary will use a rounding step of 40 pixels, and in the absence of a Width hint from the browser, it will substitute a default value of 360.
Responsive images using client hints interactive demo
Open the following URL in a Chromium-based browser to see a demo of automatic image width in action:
https://cloudinary-devs.github.io/cld-docs-assets/demos/client-hints.html
View the demo code in GitHub.
Strict transformations and signed URLs
To support strict transformations and signed URLs, the explicit pixels step value has to be specified and included in the signed value of the width parameter. e.g., w_auto:100
(i.e., there is no default value in this case and the pixels step value must be explicitly included in the parameter value). The CDN layer will send the original requested path (e.g., /w_auto:100/sample.jpg
) together with the actual required rounded up width (e.g., w_400
). Cloudinary's service will validate the signature according to original requested width rather than according to the actual required image width (e.g., w_auto:100
instead of w_400
).
Automatic image width using optimal responsive breakpoints
- Currently only works on Chromium-based browsers (e.g., Chrome, Edge, Opera, and Samsung Internet).
- For customers with a custom domain name or private CDN distribution, contact us to setup this feature.
The automatic width feature can be further enhanced using Cloudinary's intelligent responsive breakpoints generation feature. Arbitrarily rounding up the width value in constant steps (e.g., 100 pixels) does not optimize the process of reducing the file size vs. the number of images generated. Having too many image versions reduces the number of CDN cache hits for requested images and increases the average delivery time for images to your users. On the other hand, having too few image versions means delivering images to users that are larger than needed for the available width, and end up being scaled down by the browser.
Cloudinary can calculate the set of optimal rounding up steps, or breakpoints, by determining the optimal number of versions for every image that balances the number of image versions generated vs. the file size reduction between each version. The set of breakpoints are thus calculated based on a difference in the actual image file size at different widths.
To automatically determine the optimal width values of each image, individually set the:
-
width
parameter toauto:breakpoints
, -
crop
parameter tolimit
, -
client_hints
parameter totrue
, -
sizes
parameter to100vw
(this is the most common value to set, i.e., 100% of the available width, but you can give any value that the HTMLsizes
attribute accepts).
Cloudinary performs the breakpoints calculation and then rounds up the generated image width to the closest optimal breakpoint.
For example, to deliver the bike
image filled to an aspect ratio of 16:9 (ar_16:9,c_fill
) and then automatically scaled to the closest optimal breakpoint that is larger than the width available for the image in the responsive layout, where the optimal breakpoints are calculated using the default breakpoint request values (c_limit,w_auto:breakpoints
):
The code above generates the following HTML image tag:
Assuming (for the example above) that the Client-Hints request header returns a width of 347, and Cloudinary has calculated that the set of optimal breakpoints for this image are as follows: 50, 238, 356, 450, 542, 621, 692, 764, 834, 901, 955, 1000.
Then the selected breakpoint from the set will be 356 pixels (the first breakpoint greater than 347), and the delivery URL will be treated as ar_16:9,c_fill/c_limit,w_356
.
Breakpoints generation settings:
- Global breakpoints settings are applied with their default values as follows:
min_width=50
,max_width=1000
,bytes_step=20KB
,max_images=20
. For more information on these settings and possible values, see the responsive breakpoint request settings documentation. - The default settings can be overridden using a dynamic URL that includes the new values:
auto:breakpoints_[min_width]_[max_width]_[bytes_step_in_KBs]_[max_images]
e.g.,w_auto:breakpoints_200_1920_30_15
. - All 4 of the breakpoint request settings parameters must be included if you need to override any of them.
For example, to deliver the bike
image filled to an aspect ratio of 16:9 and then automatically scaled to the closest optimal breakpoint, where the optimal breakpoints are calculated using the following breakpoint request values - min_width=200
, max_width=1920
, bytes_step=30
, max_images=15
:
The code above generates the following HTML image tag:
Default value for browsers that don't support Client-Hints
If Client-Hints are not supported by the user's browser or if they are not available, by default w_auto:breakpoints
will be treated as if no scaling is requested. This might mean delivering the original hi-res image dimensions which is equivalent to omitting the width parameter altogether. You can bypass this behavior by specifying a default value for the width that is only used if the information cannot be retrieved from a Client-Hints header. This default width value can be concatenated to the transformation value, separated with a colon.
For example:
w_auto:breakpoints:500
tells Cloudinary to:
- Scale the image to the closest calculated breakpoint based on the width retrieved from the Client-Hints header, or
- Scale the image to the closest calculated breakpoint based on a default value of 500 pixels for the width, if the actual available width cannot be retrieved from a Client-Hints header (i.e., as if the Client-Hints header had returned a value of 500 for the width).
Calculated breakpoints delivery URL
Dynamic applications (e.g., JavaScript-based applications) can request that Cloudinary deliver the set of calculated breakpoints for an image instead of the transformed image itself. To request a JSON response containing the set of calculated breakpoints, set the width
transformation parameter to auto:breakpoints:json
. The breakpoints' JSON URLs are treated in a similar way to all other derived assets and are cached at the CDN layer like any derived image or file.
For example, to return the set of calculated breakpoints for the bike
image filled to an aspect ratio of 16:9:
Returned JSON example: