imagekit_使用ImageKit为您的网站提供响应式和美术指导图像

imagekit

Images are essential for all websites. Whether it is product images for an e-commerce store, photographs of picturesque locations on a travel website or images to support the content of a blog.

图片对于所有网站都是必不可少的。 无论是电子商务商店的产品图片,旅游网站上风景如画的地点的照片,还是支持博客内容的图片。

While they help communicate a lot more than just plain text, images are huge in terms of the bandwidth utilized. So much so, that images make up for almost 60% of the page weight of an average page on the internet. And because images are so heavy, to ensure a fast page load time, we go to great lengths to ensure that they are always optimized - compressed, in the right format and the right size.

尽管它们不仅可以传达纯文本,还可以传达更多信息,但是就所使用的带宽而言,图像是巨大的。 如此之多,图像几乎占据了互联网上平均页面的页面重量的60%。 而且由于图像非常沉重,以确保快速的页面加载时间,因此我们竭尽全力确保始终对其进行优化-以正确的格式和正确的大小进行压缩。

我需要做更多的图像优化吗? ( Do I need to do more with image optimization? )

YES.

是。

In the last 10 years, the number of smartphones and similar devices has grown manifold. Combine that with cheaper and more accessible data plans, more and more people have started accessing the internet from mobile devices.

在过去的十年中,智能手机和类似设备的数量呈不断增长的趋势。 将其与更便宜,更易访问的数据计划相结合,越来越多的人开始从移动设备访问Internet。

Therefore, we need to optimize our website, including the images as well, not just for our desktop users, but more importantly, for our mobile users, including the new devices with different screen sizes and resolutions that get introduced every year.

因此,我们不仅需要为台式机用户,而且还需要为移动用户(包括每年推出的具有不同屏幕尺寸和分辨率的新设备)优化网站,包括图像。

本教程需要什么? ( What will I need in this tutorial? )

  1. Basic HTML and CSS - To create a responsive web page and the styles for it

    基本HTML和CSS-创建响应式网页及其样式
  2. ImageKit.io - It is a CDN-enabled automatic image optimization server that also allows you to transform (resize, crop, blur, watermark and more) images in real-time with just a few minutes of setup. The workflow for delivering responsive images becomes significantly simple with real-time resizing.

    ImageKit.io-这是一个启用CDN的自动图像优化服务器,它还允许您仅需几分钟的设置就可以实时转换(调整大小,裁剪,模糊,水印等)图像。 通过实时调整大小,用于传递响应图像的工作流程变得非常简单。

In the examples that follow, we make use of images uploaded to the media library of a demo account created on ImageKit.

在以下示例中,我们利用上载到在ImageKit上创建的模拟帐户的媒体库的图像 。

什么是响应式图像? ( What are responsive images? )

If we keep desktop devices in mind, an 800px width image would be great. However, a mobile screen requires only a 360px width image and any image significantly wider than this would consume a lot of unnecessary bandwidth. If we do the opposite, i.e. load a smaller width image on desktop devices, then it appears grainy when stretched to fit the layout on a desktop screen.

如果我们牢记桌面设备,那么800px宽的图像将是不错的选择。 但是,移动屏幕仅需要360px宽度的图像,任何宽得多的图像都将消耗大量不必要的带宽。 如果我们做相反的事情,即在桌面设备上加载较小宽度的图像,则在拉伸以适合桌面屏幕上的布局时,它将显得粗糙。

The ideal solution would be to have our images “respond” differently (or load differently) for different devices. This method of loading images is known as responsive images.

理想的解决方案是让我们的图像针对不同的设备以不同的方式“响应”(或以不同的方式加载)。 这种加载图像的方法称为响应图像。

常规img标签 ( The regular img tag )

In this article, we restrict ourselves to the img tag in HTML for our discussion related to responsive images.

在本文中,我们将自己限制在HTML中的img标签上,以进行有关响应式图像的讨论。

Typically, the image tag looks like this

通常,图片标签如下所示

Normal image tag

The src attribute of the img tags allows us to specify only one image URL for our web page. However, there are two more attributes of the img tag which allow us to create responsive images. These have been discussed later in this post.

img标签的src属性允许我们仅为网页指定一个图像URL。 但是, img标签还有另外两个属性,这些属性使我们能够创建响应图像。 这些已在本文后面讨论。

First, let’s try to mimic a real-life use case - a product listing page on an e-commerce website. Typically on mobile devices, we would follow a one-column layout for the products (images occupy 100% of the screen width), on tablet devices, a two-column layout (images occupy 50% of screen width) and on desktop devices, a three-column layout (images occupy almost 33% of viewport width).

首先,让我们尝试模拟现实生活中的用例-电子商务网站上的产品列表页面。 通常在移动设备上,我们会按照产品的一列布局(图像占据屏幕宽度的100%),在平板电脑上遵循两列布局(图像占据屏幕宽度的50%)以及在台式设备上,三列布局(图像几乎占据了视口宽度的33%)。

Here is a code demo for this layout using the img tag with just the src attribute

这是仅使用src属性的img标签用于此布局的代码演示

On both desktop and mobile devices, the same original image gets loaded, which is not the right thing to do.

在台式机和移动设备上,都加载了相同的原始图像,这不是正确的选择。

如何切换到响应图像? ( How to switch to responsive images? )

As mentioned earlier, the img tag provides us with two attributes that allow us to deliver responsive images to our users. These attributes are srcset and sizes.

如前所述, img标签为我们提供了两个属性,这些属性使我们能够向用户传递响应图像。 这些属性是srcsetsizes

Before we discuss in detail about the use of these two image attributes, let’s look at how does our HTML change for the above example.

在详细讨论这两个图像属性的使用之前,让我们看一下上面示例中HTML如何更改。

Responsive image tag

srcset和size属性是什么,浏览器如何使用它们? ( What are the srcset and sizes attributes and how does a browser use them? )

To load the right image on particular screen size, we need to have the following data

要在特定屏幕尺寸上加载正确的图像,我们需要以下数据

  1. The device dimensions - what is the device width?

    设备尺寸-设备宽度是多少?
  2. The layout - what is the size occupied by a particular image for the current device width?

    布局-当前设备宽度下,特定图像占用的大小是多少?
  3. The appropriate image needed to fit the current layout - the image URL

    符合当前布局所需的适当图像-图像URL

The srcset and sizes attributes help answer these questions in the HTML.

srcsetsizes属性有助于在HTML中回答这些问题。

The browser already knows the device width. Then the sizes attribute, written similar to CSS media queries, specifies the layout for the image for different device widths.

浏览器已经知道设备的宽度。 然后,类似于CSS媒体查询编写的sizes属性指定了不同设备宽度的图像布局。

For example, from the sizes attribute in the responsive image tag above, if the device width is less than 480px (a mobile device), the image occupies the full viewport width (100vw). If the device width is less than 720px, the image occupies 50% of the viewport width (50vw), and for all other cases (desktop devices), the image occupies 33% of the viewport width (33vw).

例如,根据上面的响应图像标签中的sizes属性,如果设备宽度小于480px(移动设备),则图像将占据整个视口宽度( 100vw )。 如果设备宽度小于720px,则图像占据视口宽度( 50vw )的50%,对于所有其他情况(台式设备),图像占据视口宽度( 33vw )的33%。

The srcset attribute, on the other hand, provides a comma-separated list of image URLs that the browser can use for different layouts along with their actual pixel width.

另一方面, srcset属性提供了一个逗号分隔的图像URL列表,浏览器可将其用于不同的布局以及它们的实际像素宽度。

For example, from the responsive image tag above, we provide the browser with 3 image URLs and their corresponding absolute widths. We take advantage of ImageKit’s real-time image resizing here. Using the width transformation (specified using w parameter), we are able to generate different sizes of an image in real-time.

例如,从上面的响应图像标签,我们为浏览器提供3个图像URL及其相应的绝对宽度。 我们在这里利用ImageKit的实时图像调整大小。 使用宽度转换(使用w参数指定),我们可以实时生成不同大小的图像。

The image URL https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-320 is 320px wide and we communicate this absolute width to the browser using 320w after the image URL. Similarly https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-480 is 480px wide (width specified to the browser using 480w)and https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-720 is 720px wide (width specified to the browser using 720w).

图片网址https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-320为320px宽,我们使用图片网址后的320w向浏览器传达此绝对宽度。 类似地https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-480是480像素宽(宽度指定为使用浏览器480w )和https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-720https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-720 (使用720w为浏览器指定的宽度)。

Here is how the browser evaluates which image to load on a device with width 400px

这是浏览器如何评估在宽度为400px的设备上加载哪个图像的方法

  1. Device width is 400px which is less than 480px (the first rule that matches in set specified by sizes attribute), so the image occupies 100% of the viewport width (100vw)

    设备宽度为400像素,小于480像素(第一个匹配sizes属性指定的集合中的规则),因此图像占据了视口宽度( 100vw )的100%
  2. Which means that I need a 400px wide image.

    这意味着我需要一个400px宽的图像。
  3. The closest size, that is also larger than the required 400px width, from the srcset attribute is https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-480 which is 480px wide as per the srcset specification.

    srcset属性中最接近的尺寸(也大于所需的400px宽度)为https://ik.imagekit.io/demo/resp-img/image1.jpg?tr=w-480 ,其宽度为480px srcset规范。
  4. Let’s load this image.

    让我们加载此图像。

响应图像的示例代码和浏览器行为 ( Example code and browser behavior with responsive images )

Here is a sample code that uses responsive images using the srcset and sizes attributes. Note that the CSS remains the same for both the cases, normal vs. responsive images, only the img tag in the HTML changes.

下面是使用应用响应图像的示例代码srcsetsizes属性。 请注意,无论是普通图像还是响应图像,CSS均保持不变,只有HTML中的img标签会更改。

From the screenshot below, that is taken at a viewport width of 719px, the browser automatically triggers loads for 480px wide images (note tr=w-480 in all the requests in the network panel). Also, ImageKit automatically converted them to WebP ensuring even smaller image size and faster load.

在下面的屏幕截图中(视口宽度为719px),浏览器自动触发了480px宽图像的加载(请注意网络面板中所有请求中的tr = w-480)。 另外,ImageKit会自动将它们转换为WebP,以确保更小的图像尺寸和更快的加载速度。

On the other hand, for a desktop device, the browser automatically triggers loads for 720px wide images.

另一方面,对于台式设备,浏览器会自动触发720px宽图像的加载。

An important thing to note here is that if the browser has already loaded a larger image, even on browser resize, it is not going to trigger a request for a smaller image. In such cases, it is better to resize the image on the browser-side, than to issue another request over the network.

这里要注意的重要一点是,如果浏览器已经加载了较大的图像,即使在调整浏览器大小时,也不会触发请求较小图像的请求。 在这种情况下,最好在浏览器端调整图像大小,而不是通过网络发出另一个请求。

如何构建高密度显示器? ( How to build for high-density displays? )

Modern laptops and mobile devices like the MacBooks and the iPhones have high-density displays. While it is a lengthy topic in itself, a simplistic explanation would be, that for a high-density display you would need to load a larger image than a standard display, taking into account the pixel density of the high-density display.

诸如MacBook和iPhone之类的现代笔记本电脑和移动设备具有高密度显示屏。 尽管这本身是一个冗长的话题,但简单的解释是,对于高密度显示器,您需要加载比标准显示器更大的图像,同时要考虑到高密度显示器的像素密度。

DPR or Device Pixel Ratio is the multiplier used to specify the screen density. For example, if a high-density display has a DPR of 2, then you need to load twice the image size as compared to a standard screen for the same visual sharpness.

DPR或设备像素比率是用于指定屏幕密度的乘数。 例如,如果高密度显示器的DPR为2,则对于相同的视觉清晰度,您需要加载的图像大小是标准屏幕的两倍。

There can be cases where we don’t have a responsive website and instead have a separate desktop and a separate mobile website. In such a case, we don’t need different image sizes for different screen widths, but we still want to cater to multiple DPR values (a high-density screen of MacBook Pro vs. a standard screen of any other laptop - same width, but different DPR).

在某些情况下,我们没有响应式网站,而是有单独的桌面和单独的移动网站。 在这种情况下,我们不需要为不同的屏幕宽度使用不同的图像尺寸,但是我们仍然想满足多个DPR值(MacBook Pro的高密度屏幕与其他任何笔记本电脑的标准屏幕-相同的宽度,但DPR不同)。

Here is an example where we provide different image URLs for different DPR values. We use ImageKit’s dpr transform to deliver images for different DPR values. Note that we use 1x, 2x and 3x to specify the size multiplier used for each image.

这是一个示例,其中我们为不同的DPR值提供了不同的图像URL。 我们使用ImageKit的dpr转换来传递不同DPR值的图像。 请注意,我们使用1x2x3x来指定用于每个图像的大小乘数。

The browser automatically picks up the most appropriate image from the list specified in the srcset attribute depending on the DPR value of the device. For example, if the DPR is 2, then the browser uses the 2x image.

浏览器会根据设备的DPR值从srcset属性中指定的列表中自动选择最合适的图像。 例如,如果DPR为2,则浏览器使用2x图像。

DPR responsive image tag

Here is a live demo of responsive images to support multiple screen densities

这是响应式图像的实时演示,可支持多种屏幕密度

艺术指导-使响应式图像更有意义 ( Art Direction - Making responsive images more meaningful )

In previous examples, we were able to load different images for different screen sizes and different DPR values. However, we were loading the resized versions of the same image.

在前面的示例中,我们能够针对不同的屏幕尺寸和不同的DPR值加载不同的图像。 但是,我们正在加载同一图像的调整大小版本。

Different devices may not just need different sizes of the same image but may be different images altogether to make the visual experience even better. Let’s take a look at the example below -

不同的设备可能不仅需要相同图像的大小不同,而且可能完全是不同的图像,以使视觉体验更好。 让我们看下面的例子-

In this example, the image used on desktop devices contains the complete landscape along with the girl on the swing in the center. On mobile, if we use just a simple resize of the original image, then the subject of the image (the girl on the swing) becomes too small and may not have the effect intended from the image.

在此示例中,台式设备上使用的图像包含完整的风景以及中间挥杆的女孩。 在移动设备上,如果仅使用原始图像的简单调整大小,则图像的主题(荡秋千的女孩)会变得太小,可能无法达到图像的预期效果。

In such cases, we need to use a different image, where we crop a smaller, tighter region around the girl and use that for smaller screens. This art direction, utilizing different images on different devices, makes the subject stand out.

在这种情况下,我们需要使用不同的图像,在该图像上我们在女孩周围裁剪出一个较小,较紧的区域,并将其用于较小的屏幕。 在不同设备上利用不同图像的这种艺术指导使对象脱颖而出。

如何获得不同的艺术指导图像? ( How do I get different images for art direction? )

For art direction, the first step is to get a tighter crop image for the smaller screen. ImageKit makes this simple with its smart crop transformation. It automatically focuses on the most crucial part of the image to create the output image. Here is the smart crop transform in action

对于艺术指导,第一步是为较小的屏幕获取更紧密的裁剪图像。 ImageKit通过智能作物转换使这一过程变得简单。 它自动聚焦于图像的最关键部分以创建输出图像。 这是行动中的智能作物转型

Original Image URL - https://ik.imagekit.io/demo/resp-img/responsive_images/image1_BJona-M_4.jpg

原始图片网址-https: //ik.imagekit.io/demo/resp-img/sensitive_images/image1_BJona-M_4.jpg

With ImageKit’s Smart Crop (with fo-auto parameter for auto focus) - https://ik.imagekit.io/demo/resp-img/tr:w-200,h-400,fo-auto/responsive_images/image1_BJona-M_4.jpg

随着ImageKit的智能作物(与FO-auto参数进行自动对焦) - https://ik.imagekit.io/demo/resp-img/tr:w-200,h-400,fo-auto/responsive_images/image1_BJona-M_4 .jpg

用于艺术指导的图片标签 ( The picture tag for art direction )

The next step is to use the more powerful picture tag instead of the srcset and sizes attribute. This tag allows you to provide more complex specifications for which image to load, than what is possible using the srcset and sizes attributes. Here is how the updated picture tag looks like with art direction.

下一步是使用功能更强大的picture标签,而不是srcsetsizes属性。 这个标签可以让你提供一种用于图像加载更多复杂的规格,比什么是可能使用srcsetsizes属性。 这是带有美术指导的更新后的picture标签的外观。


    
    
    

Inside the picture tag, the source tag is used to provide the specification for the img tag. For desktop screens (more than width 1081px) we use ImageKit’s real-time resizing to deliver an image with width 800px, whereas for tablet and other devices we use a smart-cropped image obtained using ImageKit.

picture标签内部, source标签用于提供img标签的规范。 对于台式机屏幕(宽度大于1081px),我们使用ImageKit的实时调整大小来提供宽度为800px的图像,而对于平板电脑和其他设备,我们使用通过ImageKit获得的智能裁剪图像。

A working demo with art direction can be found here

可在此处找到具有艺术指导的工作演示

Here is a comparison of the same page and layout, with and without art direction. Clearly, with art direction, the subject of the image stands out in the final image.

这是在有和没有美术指导的情况下,相同页面和布局的比较。 显然,在艺术指导下,图像的主题在最终图像中脱颖而出。

结论 ( Conclusion )

Responsive images are essential for your websites. Combined with art direction, not only do they help in building a better visual experience for your users but also ensure that you are not loading any extra bytes on the page than what is needed.

响应式图像对于您的网站至关重要。 与美术指导相结合,它们不仅有助于为用户提供更好的视觉体验,而且还可以确保您不会在页面上加载多余的字节。

ImageKit makes delivering responsive images even simpler with real-time URL-based resize and smart crop allowing us to generate different image sizes needed for different devices. Thereby, eliminating any manual efforts in building a responsive image experience (except for making the HTML changes ). Additionally, ImageKit automatically delivers images in the correct format (including WebP image format) using a CDN, ensuring the lightest and fastest possible delivery of all your images.

ImageKit通过基于URL的实时大小调整和智能裁剪功能,使响应图像的交付更加简单,从而使我们能够生成不同设备所需的不同图像大小。 因此,消除了构建响应式图像体验的任何手动工作(除了更改HTML之外)。 此外,ImageKit使用CDN自动以正确的格式(包括WebP图像格式)传送图像,从而确保最轻,最快的传送所有图像。

So, go ahead and get started with ImageKit. It starts free for up to 20GB bandwidth every month! Just a few minutes to integrate with your existing images and you are ready to deliver a great visual experience on your website.

因此,继续并开始使用ImageKit 。 它每月免费开始提供高达20GB的带宽! 只需几分钟即可与您现有的图像集成,您就可以在网站上提供出色的视觉体验。

翻译自: https://scotch.io/tutorials/deliver-responsive-and-art-directed-images-for-your-website-with-imagekit908237

imagekit

你可能感兴趣的:(python,java,人工智能,html,css)