源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

页面利用渐进式JPEG来提升用户体验度

  • 时间:2020-04-27 00:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:页面利用渐进式JPEG来提升用户体验度
今天才认识到原来JPEG文件有两种保存方式他们分别是Baseline JPEG(标准型)和Progressive JPEG(渐进式)。两种格式有相同尺寸以及图像数据,他们的扩展名也是相同的,唯一的区别是二者显示的方式不同。 [b]Baseline JPEG [/b] 这种类型的JPEG文件存储方式是按从上到下的扫描方式,把每一行顺序的保存在JPEG文件中。打开这个文件显示它的内容时,数据将按照存储时的顺序从上到下一行一行的被显示出来,直到所有的数据都被读完,就完成了整张图片的显示。如果文件较大或者网络下载速度较慢,那么就会看到图片被一行行加载的效果,这种格式的JPEG没有什么优点,因此,一般都推荐使用Progressive JPEG。 [img]http://files.jb51.net/file_images/article/201412/2014120116070018.gif[/img] [b]Progressive JPEG [/b] 和Baseline一遍扫描不同,Progressive JPEG文件包含多次扫描,这些扫描顺寻的存储在JPEG文件中。打开文件过程中,会先显示整个图片的模糊轮廓,随着扫描次数的增加,图片变得越来越清晰。这种格式的主要优点是在网络较慢的情况下,可以看到图片的轮廓知道正在加载的图片大概是什么。在一些网站打开较大图片时,你就会注意到这种技术。 [img]http://files.jb51.net/file_images/article/201412/2014120116070019.gif[/img] 渐进式图片带来的好处是可以让用户在没有下载完图片就可以看到最终图像的大致轮廓,一定程度上可以提升用户体验。(瀑布留的网站建议还是使用标准型的) [img]http://files.jb51.net/file_images/article/201412/2014120116070120.jpg[/img] 另外渐进式的图片的大小并不会和基本的图片大小相差很多,有时候可能会比基本图片更小。渐进式的图片的缺点就是吃用户的CPU和内存,不过对于现在的电脑来说这点图片的计算并不算什么。 说了这边多下面就改讲讲怎么讲图片保存为或者转化为Progressive JPEG了。 [b]1、PhotoShop[/b] 在photoshop中有“存储为web所用格式”,打开后选择“连续”就是渐进式JPEG。 [img]http://files.jb51.net/file_images/article/201412/2014120116070121.png[/img]   具体教程参考 [url=http://www.1sucai.cn/photoshop/182198.html]http://www.1sucai.cn/photoshop/182198.html[/url] [b]2、Linux[/b] 检测是否为progressive jpeg : identify -verbose filename.jpg | grep Interlace(如果输出 None 说明不是progressive jpeg;如果输出 Plane 说明是 progressive jpeg。) 将basic jpeg转换成progressive jpeg:> convert infile.jpg -interlace Plane outfile.jpg [b]3、PHP[/b] 使用 imageinterlace 和 imagejpeg 函数我们可以轻松解决转换问题。
<?php
    $im = imagecreatefromjpeg('pic.jpg');
    imageinterlace($im, 1);
    imagejpeg($im, './php_interlaced.jpg', 100);
    imagedestroy($im);
?>
4、Python
import PIL
from exceptions import IOError
img = PIL.Image.open("c:\\users\\biaodianfu\\pictures\\in.jpg")
destination = "c:\\users\\biaodianfu\\pictures\\test.jpeg"
try:
  img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
except IOError:
  PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]
  img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
[b]5、jpegtran[/b] jpegtran -copy none -progressive <inputfile> <outputfile> [b]6、C#[/b]
using (Image source = Image.FromFile(@"D:\temp\test2.jpg")) {
  ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().First(c => c.MimeType == "image/jpeg");
  EncoderParameters parameters = new EncoderParameters(3);
  parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
  parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)EncoderValue.ScanMethodInterlaced);
  parameters.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)EncoderValue.RenderProgressive);
  source.Save(@"D:\temp\saved.jpg", codec, parameters);
}
以上就是使用渐进式JPEG图片来提升页面体验度的全部内容了,很简单实用,这里推荐给小伙伴们。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部