在 Centos + PHP 扩展上编译 pHash

在 Centos + PHP 扩展上编译 pHash

我正在尝试在 Centos 6.3 x86_64 上编译 pHash 0.9.5 以下是我所遵循的步骤的详细信息:

$ yum install unzip
$ wget https://downloads.sourceforge.net/project/cimg/CImg-1.5.4.zip
$ unzip CImg-1.5.4.zip
$
$ wget http://phash.org/releases/pHash-0.9.5.tar.gz
$ tar xvf pHash-0.9.5.tar.gz
$
$ cp CImg-1.5.4/CImg.h pHash-0.9.5/src/
$
$ cd pHash-0.9.5
$ ./configure --enable-video-hash=no --enable-audio-hash=no
$ make
$ make install

然后,一旦安装了 pHash,我就会尝试像这样安装 php 扩展:

$ cd bindings/php
$ phpize
$ ./configure 
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib64/php/modules
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking whether pHash is available... yes, shared
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking for pHash in default path... found in /usr/local
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking pHash.h usability... no
checking pHash.h presence... no
checking for pHash.h... no
configure: error: 'pHash.h' header not found
$

因此显然 pHash 已正确安装checking for pHash in default path... found in /usr/localpHash.h未被找到。

pHash.h位于/usr/local/include。我正在尝试调试配置脚本以查看为什么找不到此文件,但目前我还没有成功。

如果有人有想法我将非常感激。谢谢。

马克西姆

更新:

显然,根据config.log:它是由 CImg.h 库引起的,所以我想知道我是否必须在某处注册这个头文件......

configure:5136: checking pHash.h usability
configure:5153: g++ -c -g -O2  -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -DHAVE_PHASH conftest.cpp >&5
In file included from conftest.cpp:51:
/usr/local/include/pHash.h:50:18: error: CImg.h: No such file or directory
In file included from conftest.cpp:51:
/usr/local/include/pHash.h:51: error: 'cimg_library' is not a namespace-name
/usr/local/include/pHash.h:51: error: expected namespace-name before ';' token
In file included from conftest.cpp:51:
/usr/local/include/pHash.h:144: error: expected ';' before '<' token
/usr/local/include/pHash.h:211: error: expected ',' or '...' before '<' token
/usr/local/include/pHash.h:250: error: expected ',' or '...' before '<' token
/usr/local/include/pHash.h:273: error: expected ',' or '...' before '<' token
In file included from conftest.cpp:51:
/usr/local/include/pHash.h:292: error: expected initializer before '<' token

更新2:

好的,我运行了一下,cpp -Wp,-v看看预编译器加载了哪些默认路径:

$ cpp -Wp,-v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/include
 /usr/include
End of search list.

因此/usr/local/include是默认路径的一部分,所以我已复制CImg.h到此文件夹中并尝试./configure再次运行。现在我遇到了一个问题audiophash.h,显然这是一个已知问题。

我会尝试修复它并回到这里希望能回答我的问题。

答案1

好的,这是完整的答案:

下载所需的库

$ cd ~/download
$ yum install libsndfile-devel unzip
$ wget https://downloads.sourceforge.net/project/cimg/CImg-1.5.4.zip
$ wget http://phash.org/releases/pHash-0.9.5.tar.gz
$ wget http://www.mega-nerd.com/SRC/libsamplerate-0.1.8.tar.gz
$ wget http://sourceforge.net/projects/mpg123/files/mpg123/1.15.1/mpg123-1.15.1.tar.bz2/download

编译 pHash 所需的库。由于 PHP 扩展,我们需要启用音频和图像支持。

$ tar xvf libsamplerate-0.1.8.tar.gz
$ cd libsamplerate-0.1.8
$ ./configure
$ make && make install
$
$ tar xvf mpg123-1.15.1
$ cd mpg123-1.15.1
$ ./configure
$ make && make install
We need to copy CImg.h in the /user/local/include folder
$ unzip CImg-1.5.4.zip
$ cp CImg-1.5.4/CImg.h /usr/local/include

编译pHash库

$ tar xvf pHash-0.9.5.tar.gz
$ cd pHash-0.9.5
$ ./configure --enable-video-hash=no
$ make && make install
Compile the PHP extension
$ cd bindings/php
$ phpize
$ ./configure LIBS="-lpthread"

编辑pHash.cpp

我必须编辑 pHash.cpp 来修复编译错误。我认为只有在使用 php >=5.3 时才需要这样做

$ vi /root/download/pHash-0.9.5/bindings/php/pHash.cpp
# line 106, replace "function_entry" by "zend_function_entry"
# save and close
Finish the compilation
$ make
$ make test
$ make install
# finally enable pHash in the php.ini adding extension=pHash.so
# restart apache

可选-如果您想将哈希存储在数据库中(SQL):

您需要编辑 pHash.cpp 来修改哈希的返回值。

默认情况下ph_dct_imagehash($file)返回一个*ulong64(指针),我们需要检索一个字符串以将其存储在数据库中。

看:https://github.com/lucidix/phash/commit/5be2d454c932152e9b2395e21f97a008c6bd8766

答案2

我知道这有点离题,但我在 Ubuntu 12.04 LTS amd64 上安装 pHash + php 扩展时遇到了困难,因此我也分享了我的安装日志:

$ sudo apt-get install make libsndfile-dev checkinstall
$ sudo apt-get install cimg-dev libjpeg62 libfftw3-3 imagemagick graphicsmagick

$ wget http://downloads.sourceforge.net/project/libpng/libpng15/1.5.18/libpng-1.5.18.tar.gz
$ tar xvf libpng-1.5.18.tar.gz
$ cd libpng-1.5.18
$ ./configure
$ make check
$ make install

cimg 有很多依赖项,如果你需要操作位图以外的其他格式(假设你不想自己处理转换),这些依赖项是必需的:
libjpeg62 => jpeg 支持
libpng15 => png 支持
libfftw3-3 => 傅里叶变换
imagemagick => 用于格式转换
graphicsmagick => imagemagick fork,必需

$ sudo apt-get install libsamplerate0-dev libmpg123-dev

这两个库是音频哈希处理所必需的。php 绑定目前需要 pHash 库启用音频哈希处理才能进行编译(请参阅http://lists.phash.org/htdig.cgi/phash-support-phash.org/2012-December/000139.html

$ cd
$ wget http://phash.org/releases/pHash-0.9.6.tar.gz
$ tar xvf pHash-0.9.6.tar.gz
$ cd pHash-0.9.6
$ ./configure --enable-openmp=yes --enable-video-hash=no LIBS='-lpthread'
$ make
$ sudo checkinstall --pkgname=phash --pkgversion="1:$(date +%Y%m%d%H%M)-0.9.6" --backup=no \
  --deldoc=yes --fstrans=no --default

我使用 checkinstall 创建和管理 phash 包,但您也可以使用 make install。LIBS='-lpthread' 告诉 configure 脚本使用 lpthread 库(否则它不会在我的计算机上编译)

由于一个模糊的 API 版本问题,使用我的 php 版本(5.3)进行编译时,附带的 php 绑定将无法工作,因此我改用了这个源代码:

$ cd
$ git clone --depth=1 http://github.com/Alexis2004/php-phash
$ cd php-phash
$ pear install CodeGen_PECL
$ ./compile.sh
$ make test
$ make install

您现在要做的就是将“extension=pHash.so”添加到您的 php.ini 文件中,就可以开始了!

答案3

@maxwell2022 实现未涵盖 libpng 和 ffmpeg。如果没有这些依赖项,PHP 绑定对我来说就不起作用。

这是我的安装日志:

yum install libsndfile-devel -y

cd
wget http://www.mega-nerd.com/SRC/libsamplerate-0.1.8.tar.gz
tar xvf libsamplerate-0.1.8.tar.gz
cd libsamplerate-0.1.8
./configure
make && make install

cd
wget http://sourceforge.net/projects/mpg123/files/mpg123/1.15.4/mpg123-1.15.4.tar.bz2/download
tar xvf mpg123-1.15.4.tar.bz2
cd mpg123-1.15.4
./configure
make && make install

cd
wget http://sourceforge.net/projects/libpng/files/libpng15/1.5.17/libpng-1.5.17.tar.gz/download
tar xvf libpng-1.5.17.tar.gz
cd libpng-1.5.17
./configure
make && make install

cd
wget https://downloads.sourceforge.net/project/cimg/CImg-1.5.6.zip
unzip CImg-1.5.6.zip
cp CImg-1.5.6/CImg.h /usr/local/include

su -c 'yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/el/updates/6/i386/rpmfusion-free-release-6-1.noarch.rpm http://download1.rpmfusion.org/nonfree/el/updates/6/i386/rpmfusion-nonfree-release-6-1.noarch.rpm'

yum install ffmpeg ffmpeg-devel libpng-devel -y

ln -s /usr/include/ffmpeg/libavutil /usr/include/libavutil
ln -s /usr/include/ffmpeg/libavformat /usr/include/libavformat
ln -s /usr/include/ffmpeg/libavcodec /usr/include/libavcodec
ln -s /usr/include/ffmpeg/libswscale /usr/include/libswscale

cd
wget http://phash.org/releases/pHash-0.9.6.tar.gz
tar xvf pHash-0.9.6.tar.gz
cd pHash-0.9.6

./configure
make && make install

cd bindings/php
phpize
./configure
make
make install

echo 'extension=pHash.so' > /etc/php.d/phash.ini

如果您需要重新开始...请快速清理。

cd ~/libsamplerate-0.1.8 && make uninstall
cd && rm -fr libsamplerat*

cd ~/mpg123-1.15.4 && make uninstall
cd && rm -fr mpg123-1.15.*

cd ~/libpng-1.5.17 && make uninstall
cd && rm -fr libpng-1.5.*

cd ~/pHash-0.9.6 && make uninstall
cd && rm -fr pHash*

rm -fr ~/CImg-1.5.6
rm -f CImg*
rm -f /usr/local/include/CImg.h

相关内容