Home > Article > Web Front-end > High-performance WEB development image compression
1. Reduce image size
When there are many images, reducing the image size is the most direct way to increase download speed.
1.
Use PNG8 instead of GIF (non-animated image), because PNG8 has the same effect, but the image size is smaller than GIF.
2.
Use Fireworks to process PNG images. Many PNG images in our products are directly exported by artists using Photoshop.
Later, I asked the artist to use Fireworks to process the PNG (the approximate method is to choose to save it as PNG8 and delete the background color).
After processing, the size of the 100K image is basically reduced by 3/4, but the image quality will also be slightly reduced, it depends on whether you can accept it.
3.
Use Smush.it (http://www.smushit.com/ysmush.it/) to compress images. Smush.it is an online image compression website made by the YUI team.
This website removes some metadata from the image without affecting the quality of the original image, so you can use this website for compression with confidence,
But this compression ratio is also relatively limited.
2. Merge images and split images
1. CSS
Everyone knows that Sprites combine images to reduce the number of requests and improve performance. But don’t merge too many pictures. If they are too big, the display of this page will be affected by this one picture.
2. Sometimes we need to split a large picture into multiple small pictures. For example, there are few pictures on the product homepage, so there is only one large banner picture.
Since all browsers can download images concurrently, if you do not split and only use 1 large image, the download speed will be slower.
3. Transparent image processing
IE6 cannot display transparent PNG images, which is a headache for many developers. The advantages and disadvantages of several methods are introduced below.
1. Use AlphaImageLoader, IE6 supports filter, use the following CSS code to make IE6 support PNG
#some-element { background: url(image.png); _background: none; _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.png', sizingMethod='crop'); }Advantages: Easy to use
Disadvantages: The performance loss is huge. AlphaImageLoader will spend a lot of resources to process transparent images. Using AlphaImageLoader, IE’s memory usage will increase rapidly.
Also, all processing of AlphaImageLoader is performed synchronously in the same thread, so when there are too many AlphaImageLoaders, it will block the rendering of the UI.
Use _filter, IE7 can also recognize it. In fact, IE7 can recognize PNG transparent images. If you use the above code under IE7, IE7 will not use the image directly, but use AlphaImageLoader.
Note: Personally, it is recommended to avoid using AlphaImageLoader as much as possible
2. JS processing
Using DD_belatedPNG (http://www.dillerdesign.com/experiment/DD_belatedPNG/), you can easily process all transparent images on the interface in the same way.
Advantages: Simple to use (simpler than AlphaImageLoader)
Disadvantages: When there are many pictures to be processed on the page, the speed is relatively slow, and the pictures cannot be changed dynamically.
3. VML
IE6 supports VML. VML can use transparent images. The code is as follows:
Modify the html code header
html xmlns
="http://www.w3.org/1999/xhtml" xmlns:v >
head >
style type
="text/css" >
v:* { behavior : url(#default#VML) ; }
span
style="color: rgb(128, 0, 0);">style >
span style="color: rgb(128, 0,
0);">head >
body >
v:image src ="image.png" />
span
style="color: rgb(128, 0, 0);">body >
span style="color: rgb(128, 0,
0);">html >
Advantages: good performance, fast speed
Disadvantages: It is complicated to use and does not support browsers such as Firefox. It is necessary to determine whether different browsers output different HTML codes.
4. Download pictures from multiple domain names
Because each browser can only send fixed requests to the same domain name at the same time, for example, IE6 seems to have 2 requests, so you can open multiple domain names to request image resources.
For example, img1.abc.com, img2.abc.com. But don’t open too many domain names, because it takes time to resolve domain names and open new connections. If there are too many domain names, it may be slower. Generally, 2-4 domain names are enough.
5. Caching background images under IE6
IE6 background image caching is a troublesome thing. Many people know to use the following JS to let IE6 cache background images
try{
document.execCommand("BackgroundImageCache", false, true);
}catch(e){}
But the effect of this is not very good. When the background image is changed by mouse movement, IE6 will always send an image request (even though the background image has been downloaded).
Although the return result is 304, it still takes a lot of time. In this case, you can use the following workaround:
Use a DIV element directly on the page to load the image, so that the loaded image can be truly cached, and no request will be sent when the mouse moves.
6. Preload images
Use the following code to preload the images of the next page after the page is loaded. When entering the next page, there is no need to download the images again
window.onload=function(){ var img = new Image(); img.src = "images/image.png"; img = null; };The above is image compression, developed by WEB Content, please pay attention to the PHP Chinese website (www.php.cn) for more related articles!