第 4 章:优化图片

1. 阅读参考资料

2. 选择合适的图片格式

操作步骤如下:

  • 启动网站 wd06npx http-server wd06
  • 用 Lighthouse 对网站 wd06 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Serve images in next-gen formats
  • 复制 wd06 文件夹,cp -r wd06 image-format && cd image-format
  • 根据优化建议详情,修改两个图片的格式,把 flower_photo.jpg 改成 webp 格式,把 flower_logo.png 改成 svg 格式
  • 全局安装 cwebpnpm i -g cwebp
  • cwebp 做图片格式转换,cwebp flower_photo.jpg -o flower_photo.webp
  • 检查 webp 和 jpg 格式的图片文件大小的差异,ls -lh
  • IMG2GOflower_photo.png 转换成 svg 格式
  • 检查 svg 和 jpg 格式的图片文件大小的差异,ls -lh
  • svgo 压缩 SVG 图片,npx svgo flower_logo.svg -o flower_logo.min.svg
  • 观察压缩前后 SVG 图片尺寸的变化,ls -al *.svg
  • 思考 SVG 尺寸变化背后的原因
  • 修改 index.html 文件,使用两个新格式的图片
  • 启动网站 image-formatnpx http-server image-format
  • 用 Lighthouse 对 image-format 网站做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Serve images in next-gen formats

3. 使用正确的图片尺寸

操作步骤如下:

  • 启动网站 wd06npx http-server wd06
  • 用 Lighthouse 对网站 wd06 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Properly size images
  • 复制 wd06 文件夹,cp -r wd06 image-dimensions && cd image-dimensions
  • 检查 Elements 面板,查看 flower_logo.png 的 CSS 宽高属性
  • 检查 flower_logo.png 图片的宽高属性,flower_logo.png 图片是否发生了缩放
  • 安装 ImageMagic 工具,sudo yum install -y ImageMagick
  • 调整图片 flower_logo.png 的尺寸,convert flower_logo.png -resize 50x50 flower_logo_50x50.png
  • 修改 index.html 代码,使用修改后的图片
  • 启动网站 image-dimensionsnpx http-server image-dimensions
  • 用 Lighthouse 对 image-dimensions 网站做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Properly size images
  • 思考为什么不调整 flower_photo.jpg 的尺寸

4. 响应式图片

操作步骤如下:

  • 启动网站 wd07npx http-server wd07
  • 用 Lighthouse 对网站 wd07 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Properly size images
  • 复制网站 wd07cp -r wd07 response && cd response
  • 针对 flower_photo 图片实现多种屏幕尺寸的支持(宽度:720px,1080px)
  • 用 ImageMagic 工具,生成多种图片尺寸
  • 使用三种不同的方案实现响应式图片,CSS 媒体查询,<img srcset><picture>
  • 启动网站 response, npx http-server response
  • 用 Lighthouse 对网站 wd07 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Properly size images
  • 调整设备尺寸,刷新页面,通过 Network 面板,查看不同图片资源的加载

5. 把 GIF 动画转换为视频

操作步骤如下:

  • 启动网站 wd08npx http-server wd08
  • 用 Lighthouse 对网站 wd08 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Use video formats for animated content
  • 复制 wd08 文件夹,cp -r wd08 gif2mp4 && cd gif2mp4
  • 安装 ffmpeg 工具:
    sudo yum install -y epel-release
    sudo rpm -v --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
    sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
    sudo yum install -y ffmpeg
    
  • 检查 ffmpeg 工具是否正常工作,ffmpeg -version
  • 转换 GIF 动画为 MP4,ffmpeg -i cat-herd.gif -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" -b:v 0 -crf 25 -f mp4 -vcodec libx264 -pix_fmt yuv420p cat-herd.mp4
  • 检查文件大小,ls -lh
  • 修改 index.html 文件,使用 video 标签播放视频,删除 img 标签
    <video autoplay loop muted playsinline>
      <source src="images/cat-herd.mp4" type="video/mp4">
    </video>
    
  • 注意video 标签需要支持自动播放,循环播放和静音功能
  • 启动网站 gif2mp4npx http-server gif2mp4
  • 用 Lighthouse 对网站 gif2mp4 做桌面性能评测
  • 查看 Lighthouse 的优化建议,是否有 Use video formats for animated content

6. 延迟加载图片

操作步骤如下:

  • 启动网站 wd09npx http-server wd09
  • 用 Network 面板对网站 wd09 做性能评测
  • 通过瀑布图,观察页面上图片加载的过程
  • 记录页面加载完成的两个时间:DCL 和 Load
  • 复制 wd09 文件夹,cp -r wd09 image-loading-lazy && cd image-loading-lazy
  • 修改 index.html 文件,在所有 <img> 标签中(除了前两个)添加属性 loading='lazy'
  • 启动网站 image-loading-lazynpx http-server image-loading-lazy
  • 用 Network 面板对网站 image-loading-lazy 做性能评测
  • 通过瀑布图,观察页面上图片加载的过程
  • 记录页面加载完成的两个时间:DCL 和 Load,跟优化前比较页面加载速度
  • 向下翻页观察 Network 面板图片延迟加载的效果

7. 用 lazysizes 延迟加载图片

操作步骤如下:

  • 启动网站 wd10npx http-server wd10
  • 用 Network 面板对网站 wd10 做性能评测,设置网络节流慢速 3G
  • 观察瀑布图,分析页面的五个图片是否延迟加载,记录 DCL 和 Load 两个时间
  • 复制 wd10 文件夹,cp -r wd10 lazysizes && cd lazysizes
  • 修改 index.html 文件,使用 lazysizes 实现非首屏图片的延迟加载
  • 启动网站 lazysizesnpx http-server lazysizes
  • 用 Network 面板对网站 lazysizes 做性能评测
  • 观察瀑布图,分析页面的五个图片是否延迟加载,记录 DCL 和 Load 两个时间
  • 分析优化后跟优化前 DCL 和 Load 有什么区别,为什么

8. 骨架屏

操作步骤如下:

  • 启动网站 wd09npx http-server wd09
  • 用 Network 面板对网站 wd09 做性能评测
  • 通过瀑布图,观察页面上图片加载的过程
  • 记录页面加载完成的两个时间:DCL 和 Load
  • 复制 wd09 文件夹,cp -r wd09 skeleton && cd skeleton
  • 修改 index.html 文件,给 img 添加 CSS 样式 background-color: lightgray;
  • 启动网站 skeletonnpx http-server skeleton
  • 用 Network 面板对网站 skeleton 做性能评测
  • 通过瀑布图,观察页面上图片加载的过程
  • 向下翻页骨架屏的效果

9. 渐进式 JPEG

操作步骤如下:

  • 启动网站 wd11npx http-server wd11
  • 在 Network 面板设置网络限流:慢速 3G
  • 刷新页面,观察图片加载的过程
  • 复制 wd11 文件夹,cp -r wd11 progressive-jpg && cd progressive-jpg
  • 用 ImageMagic 工具生成渐进式 JPEG,convert 6.jpg -interlace Plane 7.jpg
  • 修改 index.html 使用 7.jpg
  • 启动网站 progressive-jpgnpx http-server progressive-jpg
  • 刷新页面,观察图片加载的过程

10. 雪碧图

操作步骤如下:

  • 启动网站 wd12npx http-server wd12
  • 在 Network 面板设置网络限流:慢速 3G
  • 刷新页面,观察图片加载的过程,结合页面内容,是否观察到 6 个图标文件
  • 复制 wd12 文件夹,cp -r wd12 sprite && cd sprite
  • 用 svg-sprite 工具生成 SVG 雪碧图,npx svg-sprite --css --css-render-less --css-dest=less --css-sprite=../img/icons.svg --css-layout=diagonal img/icon-images/*.svg
  • 查看新生成的 LESS 文件:sprite.less 和生成的雪碧图:icons.svgls less img
  • 查看 sprite.less 文件内容,cat less/sprite.less
  • 修改 main.lessglobal_small.less 使用雪碧图
  • 运行构建脚本将 LESS 代码转成 CSS 代码,./less.sh
  • 启动网站 spritenpx http-server sprite
  • 刷新页面,观察图片加载的过程

11. 综合案例

操作步骤如下:

  • 一位毕业生咨询的项目情况如下:
部分聊天记录
图:部分聊天记录
  • 当时用网络面板对网站的性能评测如下图所示:
项目中有大量的图标资源,点击查看大图
图:项目中有大量的图标资源,点击查看大图
项目的时间线不理想,点击查看大图
图:项目的时间线不理想,点击查看大图
  • 网站地址:https://www.yunyingfile.com/
  • 目前该网站已经做了一些调整和优化
  • 请思考并回答以下问题:
    • 现在的网站性能还存在哪些问题?应该如何改进?
    • 之前的网站性能有哪些问题?应该如何改进?

2018 -  by 王顶. All rights reserved.本站访客人数  人次

results matching ""

    No results matching ""