In addition to script style files, most of the traffic consumed by refreshing a page is actually downloaded pictures. A picture can easily be dozens of KB, and the traffic optimized by optimizing style and script files is actually not as large as a picture. This article introduces how to optimize image traffic from two perspectives. The premise of image traffic optimization in this article is for mobile terminals. webp First of all, let's start with the image format. Webp (Google's official website) is an image format launched by Google. Its advantage is that under the same image quality, its volume is more than 25% smaller than jpg and png. Take two jpg and png images as an example:
From the table, we can see that converting the image to webp format makes the image smaller than the image after tinypng compression, and the image quality is even higher than tinypng compression. Although webp images are smaller and higher quality than png and jpg, their compatibility is only around 70% globally. (caniuse as of 20160911) According to caniuse, all mobile Android models above 4.4 support it, but iOS does not support it at all. Our users are 55% iOS and 55% Android. Supporting webp can provide a smaller image experience for at least half of our users. And it is said that the iOS 10 system will support webp, so our products will have a higher support for webp. iOS 10 is expected to support webp Image server supports webp conversion Our company originally had a picture thumbnail function based on nginx+lua+graphicsmagick, and the specific usage is similar to http://cdn1.shwojoy.com/images/34/xxxxx.png http://cdn1.shwojoy.com/images/34/xxxxx.png.300x300.png In order to support webp conversion, the original lua script needs to be modified to add recognition of the .webp suffix, so that it can recognize and convert domain names such as xxxxx.png.300x300.png.webp or xxxxx.png.webp. What the nginx+lua+graphicsmagick solution actually does is that nginx intercepts the domain name, and the lua script matches the domain name suffix rules, such as 300x300.png/.webp and similar suffixes. After the matching is completed, the graphicsmagick command is called in lua to perform some image conversion, cropping and other tasks. lua script snippet
It is worth noting that graphicsmagick version 1.3.20 and above only supports webp download Graphicsmagick can convert webp, but you still need to download and compile libwebp. Graphicsmagick supports webp tutorial Once the image server supports webp conversion, it can convert images in webp format in real time, providing technical support for subsequent webp compatible solutions.
webp compatible solution The current method for determining whether webp is supported on the browser side is the feature detection method. According to the detection result, the value of whether webp is supported is stored in a cookie for later use where webp compatibility needs to be determined. Feature detection script: (reference)
If the browser supports webp format images, set the flag in the cookie and set webpaclassName on the html tag. The purpose of this classname is to be compatible with webp images in less files. webp compatibility in less files
This less code replaces the original code describing the background image in the less file. For example, the original definition of the background image
Now the compatible solution is:
Webp images in HTML files are compatible Our company currently uses Java's velocity to write the HTML part, and the webp images that are synchronously delivered to the page will be compatible in the following way.
The $!{isWebp} variable is used by the backend to determine whether the previously defined webp_showjoy=available is in the cookie requested by the browser. If yes, it returns the .webp suffix, otherwise it returns nothing. The problem with this is that when a user browses the product page for the first time, the background judgment cookie is always false, so it is impossible for the user to return a picture with the .webp suffix when browsing for the first time. Another situation is that when images are used in large quantities, we will use lazy loading to delay the loading of images. At this time, we can modify the lazy loading plug-in and dynamically make it compatible with webp images in the plug-in.
This completes the support of webp format images on mobile terminals. This is also one of the solutions for optimizing image traffic. Retina compatible image flow optimization Front-end developers should all know that images with a magnification of @2x or @3x should be used on retina screens to ensure image clarity. However, for the convenience of image cutting, some companies will uniformly cut out @2x images during the image cutting stage, regardless of whether the browsing device is a retina screen or a normal screen, @2x images will be used. There are two disadvantages to this. First, @2x images will be downsampled on non-retina screens. Although this will not affect the clarity, it will lack some sharpness. Second, compared with @1x images, @2x images are larger in size, which results in a waste of traffic and affects page opening performance. Therefore, the correct approach is to use different sizes of images for retina screens. Image cropping has been implemented on the image server. When considering retina, webp compatibility is also required. The two together will greatly reduce the size of the image.
less file compatible with retina
To replace the original background-image: url(), you can write it as follows:
HTML files are retina compatible In HTML, this solution intends to use the HTML5 feature srcset attribute. The purpose of the secset attribute is to allow developers to specify a set of sources for an image attribute, where the sources of these images are set based on the pixel resolution of the client display. For example, define image resources in the volecity template:
This image definition specifies that a 300x300 image source should be used on retina (2x) screens, and a 150x150 image source should be used on non-retina (1x) screens. Lazy loading images in html Since the lazy loaded images will have their src assigned in the plugin, the image URL can be modified directly in the lazy loaded plugin based on window.devicePixelRatio.
Under this solution, all images must be edited with a size suffix (_num_x_num_). With the support of our image server, there is a special benefit: the image server will crop the image with the size suffix and compress it at the same time. If a browser does not support the webp format, the image will be compressed to reduce the image size as much as possible. The srcset attribute currently has very good compatibility on mobile devices, but is not supported by Android 4.x. Summarize There are two solutions for image traffic optimization, one for webp image format and the other for retina. ***The following are the scenarios for writing image code:
If you have any questions or comments about my understanding above, please feel free to chat with me privately~ Weibo-Siamese who writes front-end |
<<: Summary of Android memory leaks (with memory detection tools)
>>: The new version of iMessage is an invisible browser in iOS 10
At the beginning of the new year, many friends to...
1. Think outside the box of operations and look a...
At the end of June, Douyin released the "Dou...
Recently, the question of "where has the new...
Fifty years ago, in 1972, the United Nations held...
What are the main differences between humans and ...
Course Contents: 1. Section 1: Course Creation Man...
Did you know that some paints are not as glamorou...
Recently, the "Knowledge is Power" scie...
gossip Recently, Zibo barbecue has become popular...
With the gradual improvement of current Internet ...
Researchers have discovered microplastics for the...
What does a bad beginning look like? Before we ta...
A flat Earth would look very different from space...