CentOs7 中安装 guetzli 教程,我的承诺,今天兑现。带你上车

周六的时候收到 36kr 推送的一篇文章: Google开源新算法,可将JPEG文件缩小35%

测试了结果,惊的我目瞪口呆。如果非要我用一个词形容的话:

牛逼

太牛逼

先来教大家如何在 CentOs7 中安装,项目的说明文档没有很好的说明如何完成 redhat 系列的 linux 如何安装。我是踩了不少坑才完成的。

这里边最简单的安装就是 mac 上了,直接通过 brew install guetzli 就ok。ubuntu 上的安装也是满多坑,如果出了问题,也可以借鉴本文。

Guetzli 项目地址


首先为自己的系统设置epel源

1
# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

上面这一步并不是必须的。我只是为了讲解后面的错误。

在安装 Guetzli 之前,需要先安装:

libpng的安装

先说 libpng 的安装。该库是一款C语言编写的比较底层的读写PNG文件的跨平台的库。

如果搞不懂c语言,你也不用管他,简单说就是 guetzli 内部会使用它,所以需要先把它安装好。否则 guetzli 没办法读取png图片(并且也不能编译通过)

这个库可以直接通过yum来进行安装。安装出来的是 1.5.13 。当前最新的版本是 1.6.29
由于我的源里边没有最新版,所以这里安装的版本就是:1.5.13

安装命令:

1
yum install libpng-devel

这里一定注意,不要直接安装 libpng 。安装 libpng-devel 会依赖安装 libpng

ps: libpng-devel 是 用于程序操作PNG图片的开发工具

The libpng-devel package contains header files and documentation necessary
for developing programs using the PNG (Portable Network Graphics) library.
If you want to develop programs which will manipulate PNG image format
files, you should install libpng-devel. You’ll also need to install
the libpng package.

如果没有安装依赖成功,请自行手动安装。命令

1
yum install libpng

检查是否安装成功:

1
yum info libpng libpng-devel

看到如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
已安装的软件包
名称 :libpng
架构 :x86_64
时期 :2
版本 :1.5.13
发布 :7.el7_2
大小 :602 k
源 :installed
来自源:base
简介 : A library of functions for manipulating PNG image format files
网址 :http://www.libpng.org/pub/png/
协议 : zlib

名称 :libpng-devel
架构 :x86_64
时期 :2
版本 :1.5.13
发布 :7.el7_2
大小 :211 k
源 :installed
来自源:base
简介 : Development tools for programs to manipulate PNG image format files
网址 :http://www.libpng.org/pub/png/
协议 : zlib

通过 yum info 命令查看已安装的软件选项。如果看到上面的结果说明安装成功。

gflags的错误安装

为了把他安装好,搞得我神魂颠倒,忘乎所以。真的是太多坑。最后通过 issue 得到了自己想要的答案。下面将给大家重现一下我的错误过程。

记得文章开头让大家设置的 epel 源吗?我用 yum search gflags 找到如下信息

1
2
3
4
5
6
7
8
9
10
[root@play ~]# yum search gflags
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.nethub.com.hk
* epel: ftp.jaist.ac.jp
* extras: mirrors.btte.net
* updates: mirror.lzu.edu.cn
======================================================= N/S matched: gflags =======================================================
gflags-devel.x86_64 : Development files for gflags
gflags.x86_64 : Library for commandline flag processing

因此,直接使用 yum install gflags gflags-devel

然后去编译 Guetzli ,出现下面的错误信息:

1
2
3
4
5
6
7
8
9
10
11
12
==== Building guetzli (release) ==== 
guetzli.cc <命令行>:0:18: 错误:‘google’不是一个命名空间名 guetzli/guetzli.cc:33:17: 附注:in expansion of macro ‘GFLAGS_NAMESPACE’ using namespace GFLAGS_NAMESPACE;
^ guetzli/guetzli.cc:33:33: 错误:expected namespace-name before ‘;’ token using namespace GFLAGS_NAMESPACE;
^ guetzli/guetzli.cc: 在函数‘int main(int, char**)’中: guetzli/guetzli.cc:194:55: 错误:‘SetUsageMessage’在此作用域中尚未声明 "guetzli [flags] input_filename output_filename");
^ guetzli/guetzli.cc:194:55: 附注:建议的替代: In file included from guetzli/guetzli.cc:24:0: /usr/include/gflags/gflags.h:331:29: 附注: ‘gflags::SetUsageMessage’ extern GFLAGS_DLL_DECL void SetUsageMessage(const std::string& usage);
^ guetzli/guetzli.cc:195:43: 错误:‘ParseCommandLineFlags’在此作用域中尚未声明 ParseCommandLineFlags(&argc, &argv, true);
^ guetzli/guetzli.cc:195:43: 附注:建议的替代: In file included from guetzli/guetzli.cc:24:0: /usr/include/gflags/gflags.h:346:31: 附注: ‘gflags::ParseCommandLineFlags’ extern GFLAGS_DLL_DECL uint32 ParseCommandLineFlags(int argc, char** argv, bool remove_flags);
^ guetzli/guetzli.cc:198:31: 错误:‘ShowUsageWithFlags’在此作用域中尚未声明 ShowUsageWithFlags(argv[0]);
^ guetzli/guetzli.cc:198:31: 附注:建议的替代: In file included from guetzli/guetzli.cc:24:0: /usr/include/gflags/gflags.h:175:29: 附注: ‘gflags::ShowUsageWithFlags’ extern GFLAGS_DLL_DECL void ShowUsageWithFlags(const char *argv0);
// what --help does
^ make[1]: *** [obj/Release/guetzli.o] 错误 1
make: *** [guetzli] 错误 2

错误中出现了多处 xx在此作用域中尚未声明 对应的 c++ 文件刚好是:gflags.h

因此我猜测,可能是版本不对。通过 yum info gflags 确认一下当前的版本信息

1
2
3
4
5
6
7
8
9
名称    :gflags
架构 :x86_64
版本 :2.1.1
发布 :6.el7
大小 :72 k
源 :epel/x86_64
简介 : Library for commandline flag processing
网址 :http://code.google.com/p/gflags
协议 : BSD

是2.1版本。因为我mac上的 Guetzli 是可以运行的,因此查看一下mac上的 gflags 版本,得到如下:

1
2
gflags: stable 2.2.0 (bottled)
Library for processing command-line flags

ok,两者之间差了一个小版本。在 gflags github 查看发布的 release 版本。看到了最新的 2.2.0 版本。然后看到其对应的描述:

This release adds support for use of the gflags library as external dependency not only in projects using CMake, but also Bazel, or pkg-config.

我想,不管怎么样,先试试呗。

正确安装 gflags 新版本

由于 epel 还没有最新的源,只能自己动手编译了。官方编译指南

ps: Debian/Ubuntu Linux 可以直接使用 apt-get 来安装,但是好像有些版本也会出现版本问题,也可以动手自己编译最新的版本

由于 gflags 需要 ccmake 编译。所以需要先安装 cmake (安装 cmake 后,就可以使用 ccmake)

1
yum -y install cmake

好,搞定 cmake ,接下来开始 gflags

1
2
3
4
5
[root@play ~]# wget https://github.com/gflags/gflags/archive/v2.2.0.tar.gz
[root@play ~]# tar -zxvf v2.2.0.tar.gz
[root@play ~]# cd gflags
[root@play ~]# mkdir build && cd build
[root@play ~]# ccmake ..

这时会有一个界面,用来生成 configuration files,这里需要特别说明一下,建议大家进入界面后,一直按c,选项不明白的都不要设置,知道出现一个g 的时候,请 press g,截个图
image

接着来

1
2
[root@play ~]# make
[root@play ~]# make install

ok,现在安装好了gflags,如果编译过程中还有其他错误,应该是少了一些支持的 lib,根据错误去查一查。

Guetzli安装

前面都是为了安装它而做的准备工作,接下来就来完成它的编译,so easy

1
2
[root@play ~]# git clone https://github.com/google/guetzli.git
[root@play ~]# make

然后会在目录中生成一个文件

guetzli/bin/Release/guetzli

将命令移动到自己的命令环境中,就可以使用了。

1
2
3
4
5
6
7
8
9
10
11
[root@play ~]# guetzli
guetzli: Guetzli JPEG compressor. Usage:
guetzli [flags] input_filename output_filename

xxxxxx

Flags from guetzli/guetzli.cc:
-quality (Visual quality to aim for, expressed as a JPEG quality value.)
type: double default: 95
-verbose (Print a verbose trace of all attempts to standard output.)
type: bool default: false

然后大家可是试一试压缩效果。后续会有文章介绍它的压缩效果,以及php中使用它。请持续关注。

我要吐槽

当我费劲心思解决这个问题后,作者 robryk 在issue 中告诉我,最新的代码已经把 gflags 依赖给移除了。因为出现相关问题的人太多了。详情

如果直接下载的是 guetzli 当前最新的代码(git clone https://github.com/google/guetzli.git)。

将不再需要 gflags 包了。只需要用 yum install libpng-devel 就好

然后编译 guetzli ,进入目录,直接 make 就可得到正确结果。