CentOs7 中安裝 guetzli 詳細教程

CentOs7 中安裝 guetzli 教程,我的承諾,今天兌現。帶你上車

週六的時候收到 36kr 推送的一篇文章: Google開源新算法,可將JPEG文件縮小35%

測試了結果,驚的我目瞪口呆。如果非要我用一個詞形容的話:

牛逼

太牛逼

先來教大家如何在 CentOs7 中安裝,項目的說明文檔沒有很好的說明如何完成 redhat 系列的 linux 如何安裝。我是踩了不少坑才完成的。

這裏邊最簡單的安裝就是 mac 上了,直接通過 brew install guetzli 就ok。ubuntu 上的安裝也是滿多坑,如果出了問題,也可以借鑑本文。

Guetzli 項目地址


首先爲自己的系統設置epel源

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

上面這一步並不是必須的。我只是爲了講解後面的錯誤。

在安裝 Guetzli 之前,需要先安裝:
- libpng link
- gflags link

libpng的安裝

先說 libpng 的安裝。該庫是一款C語言編寫的比較底層的讀寫PNG文件的跨平臺的庫。

如果搞不懂c語言,你也不用管他,簡單說就是 guetzli 內部會使用它,所以需要先把它安裝好。否則 guetzli 沒辦法讀取png圖片(並且也不能編譯通過)

這個庫可以直接通過yum來進行安裝。安裝出來的是 1.5.13 。當前最新的版本是 1.6.29
由於我的源裏邊沒有最新版,所以這裏安裝的版本就是:1.5.13

安裝命令:

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.

如果沒有安裝依賴成功,請自行手動安裝。命令

yum install libpng

檢查是否安裝成功:

yum info libpng libpng-devel

看到如下內容

已安裝的軟件包
名稱    :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 找到如下信息

[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 ,出現下面的錯誤信息:

==== 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 確認一下當前的版本信息

名稱    :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 版本,得到如下:

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)

yum -y install cmake

好,搞定 cmake ,接下來開始 gflags

[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

接着來

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

ok,現在安裝好了gflags,如果編譯過程中還有其他錯誤,應該是少了一些支持的 lib,根據錯誤去查一查。

Guetzli安裝

前面都是爲了安裝它而做的準備工作,接下來就來完成它的編譯,so easy

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

然後會在目錄中生成一個文件

guetzli/bin/Release/guetzli

將命令移動到自己的命令環境中,就可以使用了。

[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 依賴給移除了。因爲出現相關問題的人太多了。詳情
- https://github.com/google/guetzli/pull/97
- https://github.com/google/guetzli/issues/89

覺得本文不錯,歡迎打賞:

image

發佈了113 篇原創文章 · 獲贊 153 · 訪問量 62萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章