This article is welcome to be shared and aggregated. There is no need to reprint the full text. Please respect copyright. The circle is so small. If you need it urgently, you can contact us for authorization. 1. PNG images that cannot be further compressed For example, the PNG image shown below (shown at 1/2 size) is still 122K after being compressed using the top PNG tools. The original image address is here: https://image.zhangxinxu.com/image/blog/202005/card.png The PNG size is as follows: If the project has only one such picture, it is fine. However, if there are many PNGs of this size on the page at once, the performance impact on the first load will be very obvious. For example, the four card pictures in the figure below are 500K. Since the background of the card is also a complex graphic, the edges and corners must be transparent. Changing it to JPG format will definitely not work (the edges and corners will become a solid color). Is there no way to further optimize it? Yes, that is to significantly optimize the size of PNG images with the help of CSS mask. 2. Mask-image and PNG size optimization The method is feasible, demo is here. The specific steps are as follows. 1. Convert PNG to JPG First save the PNG image to JPG, 50% of the image quality is enough, as shown below: At this point, the image size can be reduced by more than 50%! 2. Make a solid color PNG based on the PNG outline The reason why PNG images are large in size is because the colors are too rich. If we turn them into solid colors, the size can be reduced by more than 100 times. At this time, the size of the pure black filled PNG image is less than 1K: 3. Use a mask to make the JPG corners white and transparent Assuming that the JPG card image uses the <img src="card.jpg"> Use the following CSS code: img { -webkit-mask-image: url(card-mask.png); mask-image: url(card-mask.png); } You can get the same effect as the original 122K PNG image, with transparent corners and smaller size. Because the four corners of card-mask.png are transparent, after card.jpg applies card-mask.png as a mask image, the corners also become transparent. 4. Cross-domain restrictions on mask images With the security upgrade of Chrome and other browsers, there are currently cross-domain access restrictions on mask images, and there will be error prompts similar to the following:
We can convert the solid color mask image into base64 format, for example:
Seeing is believing, you can click here: PNG image size optimization demo using CSS mask The final effect of the JPG format card is shown in the following figure (the four corners are transparent): 3. Comparison of optimized effects The size comparison of the four pictures before and after optimization is as follows: The original size of 4 pictures is 458K, and the optimized picture size is 197K+1K, which is a 56.8% reduction in size! Queer~ Summarize This is the end of this article about how to significantly optimize the size of PNG images with the help of CSS mask. For more relevant CSS mask content, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! |
<<: Practical record of optimizing MySQL tables with tens of millions of data
1. Create a new virtual machine from VMware 15.5 ...
Copy code The code is as follows: wmode parameter...
Table of contents What is the rest operator? How ...
vmware vsphere 6.5 is the classic version of vsph...
CAST function In the previous article, we mention...
In development, it is often necessary to cache th...
Create a table create table order(id varchar(10),...
The Linux stream editor is a useful way to run sc...
General CSS code will only cause minor issues wit...
Table of contents Filters 01.What is 02. How to d...
1. Introduction to Nginx Nginx is a web server th...
Achieve results Implementation Code html <div ...
Table of contents Preface System environment Curr...
By using Nginx virtual domain name configuration,...
User Groups In Linux, every user must belong to a...