微信小程序选择图片优化
This article was sponsored by Cloudinary. Thank you for supporting the partners who make SitePoint possible.
本文由Cloudinary赞助。 感谢您支持使SitePoint成为可能的合作伙伴。
When adding images to a website, most of us will instinctively know to avoid using a GIF for a photo and avoid using a JPEG for a graph. The lazy ones among us – i.e., me – may just opt for PNG for everything and hope for the best.
当将图像添加到网站时,我们大多数人会本能地知道要避免对照片使用GIF,而对图形不使用JPEG。 我们中间那些懒惰的人(即我)可能只是为所有内容选择PNG,并希望做到最好。
So why do we do this? It all comes down to the encoding used to create the image in a particular file format. JPEG suits photography (the “P” stands for “Photographic”) since it blurs sharp edges but preserves smooth gradients. Encoding an image with large, sharply-defined blocks of color using JPEG causes loss of definition and inefficient encoding.
那么为什么我们要这样做呢? 全部归结为用于以特定文件格式创建图像的编码。 JPEG适合摄影(“ P”代表“摄影”),因为它模糊了锐利的边缘但保留了平滑的渐变。 使用JPEG对带有大尺寸,清晰定义的颜色块的图像进行编码会导致清晰度下降和编码效率低下。
Conversely, GIF suits logos or simple block graphs, since it focuses on the changes between uniform regions of color. This is obviously pretty useless for photos, where the colors differ almost pixel by pixel.
相反,GIF适合徽标或简单的方框图,因为它着重于颜色均匀区域之间的变化 。 对于颜色几乎逐个像素变化的照片,这显然是毫无用处的。
As an example, this image comes in at 106Kb as a JPEG:
举例来说,此图片以JPEG格式传入时为106Kb :
![JPEG photograph](https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2017/03/1489555500kite_xikqxa-768x1024.jpg)
However, it reaches a whopping 517Kb encoded as a GIF. That’s almost 5 times bigger just by using an inappropriate image format. Oh dear.
但是,它达到了编码为GIF的高达517Kb 。 仅使用不适当的图像格式,它的大小几乎是原来的5倍 。 噢亲爱的。