Php – Compile pHash on Centos + PHP extension

libraryPHP

I'm trying to compile pHash 0.9.5 on Centos 6.3 x86_64
Here is the detail of the steps I've followed:

$ 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

Then once pHash is installed I've tried to install the php extension like this:

$ 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
$

So apparently pHash has been installed correctly checking for pHash in default path... found in /usr/local but pHash.h is not found.

pHash.h is located in /usr/local/include. I'm trying to debug the configure script to see how come this file is not located but for now I'm not really successful.

If anyone have an idea I would much appreciate. Thanks.

Maxime

UPDATE:

Apparently according the config.log: it's caused by CImg.h library so I'm wondering if I have to register somewhere this header file…

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

UPDATE2:

ok, so I've run cpp -Wp,-v to see which default paths was loaded by the pre-compiler:

$ 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.

So /usr/local/include is part of the default path so I've copied CImg.h in this folder and try to run the ./configure again. Now I have an issue with audiophash.h which apparently is a known issue.

I'll try to fix it and come back here to hopefully answer my question.

Best Answer

ok so here is the complete answer:

Download required libraries

$ 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

Compile required library for pHash. We need to enable the audio and image support because of the PHP extension.

$ 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

Compile pHash library

$ 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"

Edit pHash.cpp

I had to edit pHash.cpp to fix a compilation error. I think you only need to do it if you are using 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

Optional - If you want to store the hash in the database (SQL):

You need to edit the pHash.cpp to modify the return value to the hash.

By default ph_dct_imagehash($file) return a *ulong64 (pointer) we need to retrieve a string to store it in the database.

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