linux下安装php的imagick扩展模块(附php升级脚本)

时间:2021-09-16 10:38:56

imagick是一个PHP的扩展,是一套软件系列,用ImageMagick提供的API来进行图片的创建与修改,不过这些操作已经包装到扩展imagick中去了,最终调用的是ImageMagick提供的API
ImageMagick主要用于图片的创建、编辑以及转换等,ImageMagick与GD的性能要高很多,如果是在处理大量的图片时更加能体现ImageMagick的性能。

下面介绍下安装php的imagick扩展模块的方法:

(1)下载软件
下载ImageMagick.tar.gz: http://pan.baidu.com/s/1qYha9GS (提取密码:gy63)
下载imagick-3.1.0RC1.tgz: http://pan.baidu.com/s/1hsHiEUg (提取密码:dqu2)

一、安装php的imagick扩展模块第一种方法:(pcel安装imagick)【推荐第一种方法】

(2)安装ImageMagick

[root@dev software]# tar -xzvf ImageMagick.tar.gz
[root@dev software]# cd ImageMagick-7.0.2-0
[root@dev ImageMagick-7.0.2-0]# ./configure --prefix=/usr/local/imagemagick
[root@dev ImageMagick-7.0.2-0]# make && make install

[root@dev ImageMagick-7.0.2-0]# /Data/app/php5.6.26/bin/pecl install imagick

................
Build process completed successfully
Installing '/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/imagick.so'
Installing '/Data/app/php5.6.26/include/php/ext/imagick/php_imagick_shared.h'
install ok: channel://pecl.php.net/imagick-3.4.3RC1
configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini

产生的imagick.so文件拷贝到/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226下
在php.ini文件里添加imagick.so
然后重启php加载imagick模块即可。
使用/Data/app/php5.6.26/bin/php -m 命令查看加载的模块

二、安装php的imagick扩展模块第二种方法:(编译安装imagick)
(3)安装Imagick
注:安装该扩展不要求安装ImageMagick
[root@dev software]# tar -xzvf imagick-3.1.0RC1
[root@dev software]# cd imagick-3.1.0RC1
[root@dev imagick-3.1.0RC1]# /Data/app/php5.6.26/bin/phpize         #这里,之前安装的php路径是/Data/app/php5.6.26,使用phpize生成configure编译文件
[root@dev imagick-3.1.0RC1]# ./configure --with-php-config=/Data/app/php5.6.26/bin/php-config --with-imagick=/usr/local/imagemagick
-------------------------------------------------------
报错:
checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h

解决办法:

查看imagick解压目录下的config.m4文件,发现ImageMagick在7.0.2高版本后的目录结构发生了变化所导致编译失败的。
查看config.m4的第55,56行内容:
[root@dev imagick-3.1.0RC1]# vim config.m4
.......
if test -r $WAND_DIR/include/ImageMagick/wand/MagickWand.h; then
AC_MSG_RESULT(found in $WAND_DIR/include/ImageMagick/wand/MagickWand.h)
.......

对比ImageMagick安装目录结构:
[root@dev include]# pwd
/usr/local/imagemagick/include
[root@dev include]# ls
ImageMagick-7
[root@dev include]# ls ImageMagick-7/
Magick++ MagickCore Magick++.h MagickWand

发现ImageMagick安装后目录结构和上面imagick的config.m4文件里的不一样
需要做下软链接:
[root@dev include]# ln -s ImageMagick-7 ImageMagick
[root@dev include]# ls
ImageMagick ImageMagick-7
[root@dev include]# cd ImageMagick
[root@dev ImageMagick]# ls
Magick++ MagickCore Magick++.h MagickWand
[root@dev ImageMagick]# ln -s MagickWand wand
[root@dev ImageMagick]# ls
Magick++ MagickCore Magick++.h MagickWand wand

这样,再次编译imagick就成功了!
[root@dev imagick-3.1.0RC1]# ./configure --with-php-config=/Data/app/php5.6.26/bin/php-config --with-imagick=/usr/local/imagemagick
.........
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

-------------------------------------------------------
[root@dev imagick-3.1.0RC1]# make && make install

---------------------------------------------------------------------------
报错:
make: *** [imagick_class.lo] 错误 1

解决:
原因是没有找到pkgconfig路径

[root@dev imagick-3.1.0RC1]# find /usr -name pkgconfig
/usr/lib64/pkgconfig
/usr/share/pkgconfig
/usr/local/lib/pkgconfig
/usr/local/imagemagick/lib/pkgconfig
/usr/local/include/libmemcached/lib/pkgconfig
/usr/local/libmemcached/lib/pkgconfig

[root@dev imagick-3.1.0RC1]# export PKG_CONFIG_PATH=/usr/local/imagemagick/lib/pkgconfig

然后再次make && make install

完整的流程是:
[root@dev imagick-3.1.0RC1]# /Data/app/php5.6.26/bin/phpize
[root@dev imagick-3.1.0RC1]# export PKG_CONFIG_PATH=/usr/local/imagemagick/lib/pkgconfig
[root@dev imagick-3.1.0RC1]# ./configure --with-php-config=/Data/app/php5.6.26/bin/php-config --with-imagick=/usr/local/imagemagick
[root@dev imagick-3.1.0RC1]# make && make install
----------------------------------------------------------------------------

make install执行结果显示:
installing shared extensions: /Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/
Installing header files: /Data/app/php5.6.26/include/php/

生成imagick.so到/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/

手动将imagick.so添加到php.ini文件里,
[root@dev etc]# pwd
/Data/app/php5.6.26/etc
[root@dev etc]# vim php.ini
............
extension="/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/imagick.so"

然后重启php和nginx即可!

[root@dev etc]# /Data/app/php5.6.26/bin/php -m
.....
imagick

----------------------------------------------

---------------------------php升级:由php5.5.1升级到php5.6.26-------------------------
由于线上服务器最初部署的php环境版本是5.5.1,后续应开发需求,将其升级到5.6.26版本

下面附上自己的升级脚本:

[root@huanqiu_web1 software]# pwd
/software/software

[root@huanqiu_web1 software]# ls                             #下载升级需要的软件
ImageMagick.tar.gz    install_php.sh   memcached-2.2.0.tgz    php-5.6.26.tar.gz    php-wkhtmltox-master.zip

[root@huanqiu_web1 software]# cat install_php.sh               #升级脚本(下面编译时带的mysql参数,mysql安装后可以不启动,这里编译需要跟上mysql参数)
#!/bin/bash
cd /software/software
tar -xvf php-5.6.26.tar.gz
cd php-5.6.26
./configure --prefix=/Data/app/php5.6.26 --with-curl --enable-mbstring --with-mysqli=/Data/app/mysql5.6.25/bin/mysql_config --with-mysql=/Data/app/mysql5.6.25/ --disable-rpath --enable-inline-optimization --with-pcre-regex --with-config-file-path=/Data/app/php5.6.26/etc/ --with-config-file-scan-dir=/Data/app/php5.6.26/etc/php.d --with-gd --enable-soap --with-pdo-mysql=/Data/app/mysql5.6.25/  --with-freetype-dir --with-png-dir --with-mcrypt --with-zlib --with-jpeg-dir --with-iconv=/usr/local/lib/ --enable-fpm

make && make install

###extension####
cd /software/software
tar -xvf ImageMagick.tar.gz
cd ImageMagick-7.0.2-0
./configure
make
make install
/Data/app/php5.6.26/bin/pecl install imagick

cd /software/software
tar -xvf memcached-2.2.0.tgz
cd memcached-2.2.0
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cd /software/software
unzip php-wkhtmltox-master.zip
cd php-wkhtmltox-master
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cd /software/software/php-5.6.26/ext/sockets
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cd /software/software/php-5.6.26/ext/bcmath
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cd /software/software/php-5.6.26/ext/gettext
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cd /software/software/php-5.6.26/ext/openssl
mv config0.m4 config.m4
/Data/app/php5.6.26/bin/phpize
./configure --with-php-config=/Data/app/php5.6.26/bin/php-config
make && make install

cp /Data/app/php5.5.1/etc/php-fpm.conf /Data/app/php5.6.26/etc/
cp /Data/app/php5.5.1/etc/php.ini /Data/app/php5.6.26/etc/

sed -i 's#/Data/app/php5.5.1/lib/php/extensions/no-debug-non-zts-20121212/#/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/#g' /Data/app/php5.6.26/etc/php.ini

sed  -i '/memcached.so/i extension="/Data/app/php5.6.26/lib/php/extensions/no-debug-non-zts-20131226/imagick.so"' /Data/app/php5.6.26/etc/php.ini