Skip to content

How to compress image

Yuan Gao edited this page Oct 28, 2019 · 1 revision

当你在上传会议纪要的时候,发现没有通过测试,并且报错显示类似下面的这种

not ok 3 docs/assets/2019/seminar-13/after.jpg exceed the max limit: width: 4032, size: 5061884. use "./scripts/fit-image.sh <FILE>" to adjust it fit.
  ---
    operator: fail
    at: <anonymous> (/Users/yuangao/git/bot5.club/tests/integration.spec.ts:45:9)
    stack: |-
      Error: docs/assets/2019/seminar-13/after.jpg exceed the max limit: width: 4032, size: 5061884. use "./scripts/fit-image.sh <FILE>" to adjust it fit.
          at Test.assert [as _assert] (/Users/yuangao/git/bot5.club/node_modules/tape/lib/test.js:225:54)
          at Test.bound [as _assert] (/Users/yuangao/git/bot5.club/node_modules/tape/lib/test.js:77:32)
          at Test.fail (/Users/yuangao/git/bot5.club/node_modules/tape/lib/test.js:318:10)
          at Test.bound [as fail] (/Users/yuangao/git/bot5.club/node_modules/tape/lib/test.js:77:32)
          at /Users/yuangao/git/bot5.club/tests/integration.spec.ts:45:9
          at Generator.next (<anonymous>)
          at fulfilled (/Users/yuangao/git/bot5.club/tests/integration.spec.ts:6:58)
          at process._tickCallback (internal/process/next_tick.js:68:7)
  ...

那就说明,你需要压缩一下图片了

bot5.club 这个项目里面集成了一个脚本来帮你完成压缩图片,但是在这之前,需要安装一些依赖

以下是Mac系统下的使用教程

首先,你可以在命令行里面运行一下

mogrify

如果显示

command not found: mogrify

那就说明你电脑里面还没有安装这个包,你可以运行

brew install ImageMagick

来安装这个包

当然,你可能本地也没有homebrew这个Mac系统里的包管理神器,不用担心,运行下面这个指令就可以安装homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果安装出了问题,你可以参考这里

你也有可能会遇到另外一个问题,像下面这种的提示:

==> Summary
🍺  /usr/local/Cellar/sqlite/3.29.0: 11 files, 3.9MB
==> Installing imagemagick dependency: python
Error: Xcode alone is not sufficient on Mojave.
Install the Command Line Tools:
  xcode-select --install

那么可以按照提示安装一下xcode-select

xcode-select --install

以上都完成之后,再安装ImageMagick就没什么问题了

完成了ImageMagick的安装之后,可以来压缩我们的图片啦

在上面报错信息里面找到对应的文件路径,比如最上面的报错里面,文件路径是docs/assets/2019/seminar-13/after.jpg,那么可以运行这个指令

./scripts/fit-image.sh docs/assets/2019/seminar-13/after.jpg

会有如下的log

fit-image: docs/assets/2019/seminar-13/after.jpg is file
resizing docs/assets/2019/seminar-13/after.jpg ...
docs/assets/2019/seminar-13/after.jpg JPEG 4032x3024 4032x3024+0+0 8-bit sRGB 4.82739MiB 0.320u 0:00.347
docs/assets/2019/seminar-13/after.jpg JPEG 4032x3024=>1920x1440 1920x1440+0+0 8-bit sRGB 325265B 2.390u 0:00.337

然后这个图片就被压缩好啦