imagick是一個PHP的擴展,用ImageMagick提供的API來進行圖片的創建與修改,不過這些操作已經包裝到擴展imagick中去了,最終調用的是ImageMagick提供的API
ImageMagick是一套軟件系列,主要用於圖片的創建、編輯以及轉換等,詳細的解釋見ImageMagick的官方網站http://www.imagemagick.org/,ImageMagick與GD的性能要高很多,如果是在處理大量的圖片時更加能體現ImageMagick的性能。
英文原文介紹如下:
imagick is a native php extension to create and modify images using the ImageMagick API.
ImageMagick is a software suite to create, edit, and compose bitmap images.. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF.
著名的圖片服務提供商Flickr使用的是ImageMagick,還有Yupoo、手機之家使用的也是ImageMagick。
1.安裝ImageMagick
這里主要說說Linux下的安裝,Windows下的安裝就不說了,Windows下的安裝相對簡單一些,參考http://www.imagemagick.org/script/install-source.php#windows
由於安裝imagick擴展時需要依賴ImageMagick的函數庫,因此必須要先安裝ImageMagick
從地址ftp://ftp.imagemagick.org/pub/ImageMagick/可以找到ImageMagick的地址
[root@CentOS_Test_Server software]# wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.5.3-10.tar.gz
--19:26:09-- ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.5.3-10.tar.gz
=> `ImageMagick-6.5.3-10.tar.gz'
正在解析主機 ftp.imagemagick.org... 74.63.13.227
Connecting to ftp.imagemagick.org|74.63.13.227|:21... 已連接。
正在以 anonymous 登錄 ... 登錄成功!
==> SYST ... 完成。 ==> PWD ... 完成。
==> TYPE I ... 完成。 ==> CWD /pub/ImageMagick ... 完成。
==> SIZE ImageMagick-6.5.3-10.tar.gz ... 11151919
==> PASV ... 完成。 ==> RETR ImageMagick-6.5.3-10.tar.gz ... 完成。
長度:11151919 (11M)
100%[=====================================================================================>] 11,151,919 33.4K/s in 3m 9s
19:29:42 (57.7 KB/s) - `ImageMagick-6.5.3-10.tar.gz' saved [11151919]
tar zxvf ImageMagick-6.5.3-10.tar.gz
cd ImageMagick-6.5.3-10
./configure --prefix=/usr/local/imagemagick
make
make install
ImageMagick安裝完成以后的目錄結構如下:
[root@CentOS_Test_Server imagemagick]# pwd
/usr/local/imagemagick
[root@CentOS_Test_Server imagemagick]# ll
總計 32
drwxr-xr-x 2 root root 4096 07-21 19:59 bin
drwxr-xr-x 3 root root 4096 07-21 20:02 include
drwxr-xr-x 4 root root 4096 07-21 20:07 lib
drwxr-xr-x 5 root root 4096 07-21 20:07 share
bin目錄下的這些命令都可以通過命令行方式來操作圖片
[root@CentOS_Test_Server imagemagick]# ll bin/
總計 356
-rwxr-xr-x 1 root root 24261 07-21 19:59 animate
-rwxr-xr-x 1 root root 24711 07-21 19:59 compare
-rwxr-xr-x 1 root root 24273 07-21 19:59 composite
-rwxr-xr-x 1 root root 24261 07-21 19:59 conjure
-rwxr-xr-x 1 root root 24261 07-21 19:59 convert
-rwxr-xr-x 1 root root 24261 07-21 19:59 display
-rwxr-xr-x 1 root root 24717 07-21 19:59 identify
-rwxr-xr-x 1 root root 24259 07-21 19:59 import
-rwxr-xr-x 1 root root 1402 07-21 19:59 Magick-config
-rwxr-xr-x 1 root root 1458 07-21 19:59 Magick++-config
-rwxr-xr-x 1 root root 1620 07-21 19:59 MagickCore-config
-rwxr-xr-x 1 root root 1428 07-21 19:59 MagickWand-config
-rwxr-xr-x 1 root root 24261 07-21 19:59 mogrify
-rwxr-xr-x 1 root root 24261 07-21 19:59 montage
-rwxr-xr-x 1 root root 24259 07-21 19:59 stream
-rwxr-xr-x 1 root root 1410 07-21 19:59 Wand-config
[root@CentOS_Test_Server imagemagick]# ll include/
總計 8
drwxr-xr-x 5 root root 4096 07-21 20:07 ImageMagick
[root@CentOS_Test_Server imagemagick]# ll include/ImageMagick/
總計 32
drwxr-xr-x 2 root root 4096 07-21 20:07 magick
drwxr-xr-x 2 root root 4096 07-21 20:07 Magick++
-rw-r--r-- 1 root root 419 07-21 20:07 Magick++.h
drwxr-xr-x 2 root root 4096 07-21 20:07 wand
[root@CentOS_Test_Server imagemagick]# ll lib/
總計 17884
drwxr-xr-x 4 root root 4096 07-21 20:02 ImageMagick-6.5.3
-rw-r--r-- 1 root root 3123344 07-21 19:59 libMagick++.a
-rw-r--r-- 1 root root 5225066 07-21 19:59 libMagickCore.a
-rwxr-xr-x 1 root root 1036 07-21 19:59 libMagickCore.la
lrwxrwxrwx 1 root root 22 07-21 19:59 libMagickCore.so -> libMagickCore.so.2.0.0
lrwxrwxrwx 1 root root 22 07-21 19:59 libMagickCore.so.2 -> libMagickCore.so.2.0.0
-rwxr-xr-x 1 root root 3681379 07-21 19:59 libMagickCore.so.2.0.0
-rwxr-xr-x 1 root root 1089 07-21 19:59 libMagick++.la
lrwxrwxrwx 1 root root 20 07-21 19:59 libMagick++.so -> libMagick++.so.2.0.0
lrwxrwxrwx 1 root root 20 07-21 19:59 libMagick++.so.2 -> libMagick++.so.2.0.0
-rwxr-xr-x 1 root root 2060411 07-21 19:59 libMagick++.so.2.0.0
-rw-r--r-- 1 root root 2360930 07-21 19:59 libMagickWand.a
-rwxr-xr-x 1 root root 1080 07-21 19:59 libMagickWand.la
lrwxrwxrwx 1 root root 22 07-21 19:59 libMagickWand.so -> libMagickWand.so.2.0.0
lrwxrwxrwx 1 root root 22 07-21 19:59 libMagickWand.so.2 -> libMagickWand.so.2.0.0
-rwxr-xr-x 1 root root 1727376 07-21 19:59 libMagickWand.so.2.0.0
drwxr-xr-x 2 root root 4096 07-21 20:07 pkgconfig
通過命令man ImageMagick可以查看ImageMagick手冊的內容,特別要注意ImageMagick中的大小寫,不要寫錯了
2.安裝PHP的擴展imagick
安裝imagick擴展時需要PHP >= 5.1.3,ImageMagick >= 6.2.4
從http://pecl.php.net/package/imagick找到imagick的最新的stable版本
[root@CentOS_Test_Server software]# wget http://pecl.php.net/get/imagick-2.2.2.tgz
--23:08:04-- http://pecl.php.net/get/imagick-2.2.2.tgz
正在解析主機 pecl.php.net... 216.92.131.66
Connecting to pecl.php.net|216.92.131.66|:80... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:77212 (75K) [application/octet-stream]
Saving to: `imagick-2.2.2.tgz.1'
100%[=====================================================================================>] 77,212 35.1K/s in 2.1s
23:08:08 (35.1 KB/s) - `imagick-2.2.2.tgz.1' saved [77212/77212]
cd imagick-2.2.2
用tar zxvf解壓.tgz文件時報錯,網上說的也是這么解壓,還是不行啊,不知道解壓報錯跟什么有關
[root@CentOS_Test_Server software]# tar zxvf imagick-2.2.2.tgz
package.xml
imagick-2.2.2/examples/polygon.php
imagick-2.2.2/examples/captcha.php
imagick-2.2.2/examples/thumbnail.php
imagick-2.2.2/examples/watermark.php
imagick-2.2.2/config.m4
imagick-2.2.2/config.w32
imagick-2.2.2/CREDITS
imagick-2.2.2/imagick.c
imagick-2.2.2/imagick_class.c
gzip: stdin: invalid compressed data--format violated
tar: 歸檔文件中異常的 EOF
tar: 歸檔文件中異常的 EOF
tar: 錯誤不可恢復:現在退出
phpize是一個shell腳本,主要是用來進行編譯環境的准備,執行以后會生成一些新的文件,為配置、編譯及安裝作好准備
[root@CentOS_Test_Server imagick-2.2.2]# /usr/local/webserver/php/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
執行phpize命令以前目錄imagick-2-2-2中的內容
[root@CentOS_Test_Server imagick-2.2.2]# ll
總計 260
-rw-r--r-- 1 root root 4479 02-07 06:11 config.m4
-rw-r--r-- 1 root root 2254 02-07 06:11 config.w32
-rw-r--r-- 1 root root 39 02-07 06:11 CREDITS
drwxr-xr-x 2 root root 4096 07-21 23:09 examples
-rw-r--r-- 1 root root 94719 02-07 06:11 imagick.c
-rw-r--r-- 1 root root 111616 07-21 23:09 imagick_class.c
執行phpize命令以后目錄imagick-2-2-2中的內容
[root@CentOS_Test_Server imagick-2.2.2]# ll
總計 1504
-rw-r--r-- 1 root root 74808 07-21 23:12 acinclude.m4
-rw-r--r-- 1 root root 295888 07-21 23:12 aclocal.m4
drwxr-xr-x 2 root root 4096 07-21 23:12 autom4te.cache
drwxr-xr-x 2 root root 4096 07-21 23:12 build
-rwxr-xr-x 1 root root 43499 07-21 23:12 config.guess
-rw-r--r-- 1 root root 1684 07-21 23:12 config.h.in
-rw-r--r-- 1 root root 4479 02-07 06:11 config.m4
-rwxr-xr-x 1 root root 31743 07-21 23:12 config.sub
-rwxr-xr-x 1 root root 453751 07-21 23:12 configure
-rw-r--r-- 1 root root 4646 07-21 23:12 configure.in
-rw-r--r-- 1 root root 2254 02-07 06:11 config.w32
-rw-r--r-- 1 root root 39 02-07 06:11 CREDITS
drwxr-xr-x 2 root root 4096 07-21 23:09 examples
-rw-r--r-- 1 root root 94719 02-07 06:11 imagick.c
-rw-r--r-- 1 root root 111616 07-21 23:09 imagick_class.c
-rw-r--r-- 1 root root 0 07-21 23:12 install-sh
-rw-r--r-- 1 root root 186760 07-21 23:12 ltmain.sh
-rw-r--r-- 1 root root 5694 07-21 23:12 Makefile.global
-rw-r--r-- 1 root root 0 07-21 23:12 missing
-rw-r--r-- 1 root root 0 07-21 23:12 mkinstalldirs
-rw-r--r-- 1 root root 65036 07-21 23:12 run-tests.php
讓我們來看看imagick的擴展的配置選項
[root@CentOS_Test_Server imagick-2.2.2]# ./configure --help | more
`configure' configures this package to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--datadir=DIR read-only architecture-independent data [PREFIX/share]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--infodir=DIR info documentation [PREFIX/info]
--mandir=DIR man documentation [PREFIX/man]
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
--target=TARGET configure for building compilers for TARGET [HOST]
Optional Features:
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-shared=PKGS build shared libraries default=yes
--enable-static=PKGS build static libraries default=yes
--enable-fast-install=PKGS optimize for fast installation default=yes
--disable-libtool-lock avoid locking (might break parallel builds)
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-libdir=NAME Look for libraries in .../NAME rather than .../lib
--with-php-config=PATH Path to php-config php-config
--with-imagick=DIR Enables the imagick extension. DIR is the prefix to Imagemagick installation directory.
--with-imagick-gm=DIR GraphicsMagick backend. NO LONGER SUPPORTED!
--with-gnu-ld assume the C compiler uses GNU ld default=no
--with-pic try to use only PIC/non-PIC objects default=use both
--with-tags=TAGS include additional configurations automatic
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
CPPFLAGS C/C++ preprocessor flags, e.g. -I<include dir> if you have
headers in a nonstandard directory <include dir>
CPP C preprocessor
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-imagick=/usr/local/imagemagick
編譯過程中如果報錯,則通過ln -s把ImageMagick的so及la文件鏈接到/usr/lib目錄下,如果不報錯,則不需要做鏈接,我原來是由於安裝包不對,導致用tar zxvf解壓的時候文件解出來的不全,例如一些.h文件都沒有解壓出來,所以編譯當然會報錯了,因此我才加了下面這些鏈接,后來編譯還是報錯。
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickCore.so /usr/lib/libMagickCore.so
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickCore.so.2 /usr/lib/libMagickCore.so.2
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickCore.la /usr/lib/libMagickCore.la
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagick++.so /usr/lib/libMagick++.so
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagick++.so.2 /usr/lib/libMagick++.so.2
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagick++.la /usr/lib/libMagick++.la
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickWand.so /usr/lib/libMagickWand.so
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickWand.so.2 /usr/lib/libMagickWand.so.2
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickWand.la /usr/lib/libMagickWand.la
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickCore.so.2.0.0 /usr/lib/libMagickCore.so.2.0.0
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagick++.so.2.0.0 /usr/lib/libMagick++.so.2.0.0
[root@CentOS_Test_Server lib]# ln -s /usr/local/imagemagick/lib/libMagickWand.so.2.0.0 /usr/lib/libMagickWand.so.2.0.0
make
小記編譯imagick擴展遇到的郁悶的問題
由於原來在目錄/home/software下已經有文件imagick-2.2.2.tgz,這個文件是在vista下面下載后,通過Vmware共享給CentOS的,后來我通過wget又下載了一個,很巧的是也同樣還是這個版本,文件名一模一樣,因此新下載的imagick擴展自動命名為imagick-2.2.2.tgz.1,wget的下載日志“23:08:08 (35.1 KB/s) - `imagick-2.2.2.tgz.1' saved [77212/77212]”,解壓的時候解的仍然是vista下面下載的那個文件,因此導致解壓的時候報錯,解壓出來的文件不全,因此導致編譯報錯,可能是與安裝包的格式有關吧,參考http://linux.chinaunix.net/bbs/thread-1026205-1-1.html,與這個有點類似吧,通過wget下載的安裝包應該是不會出現這個問題的,然后rm imagick-2.2.2.tgz,mv imagick-2.2.2.tgz.1 imagick-2.2.2.tgz,tar zxvf imagick-2.2.2.tgz,解壓后正常,說明是安裝包的問題,然后后來的一切就都正常了。
make install也就是把編譯生成的so文件拷貝到php的擴展目錄下
[root@CentOS_Test_Server imagick-2.2.2]# make install
/bin/sh /home/software/imagick-2.2.2/libtool --mode=install cp ./imagick.la /home/software/imagick-2.2.2/modules
cp ./.libs/imagick.so /home/software/imagick-2.2.2/modules/imagick.so
cp ./.libs/imagick.lai /home/software/imagick-2.2.2/modules/imagick.la
PATH="$PATH:/sbin" ldconfig -n /home/software/imagick-2.2.2/modules
----------------------------------------------------------------------
Libraries have been installed in:
/home/software/imagick-2.2.2/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
查看so文件是否已經生成,發現imagick.so文件已經在php的擴展目錄下面了,證明已經安裝成功
[root@CentOS_Test_Server imagick-2.2.2]# ll /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/
總計 3040
-rwxr-xr-x 1 root root 172209 05-17 19:07 db4.so
-rwxr-xr-x 1 root root 517843 04-28 20:55 eaccelerator.so
-rwxr-xr-x 1 root root 748762 07-22 03:30 imagick.so
-rwxr-xr-x 1 root root 185627 04-28 20:38 memcache.so
-rwxr-xr-x 1 root root 118580 04-28 22:23 pdo_mysql.so
-rwxr-xr-x 1 root root 1308827 07-08 19:10 xapian.so
3.相關配置
vi /usr/local/webserver/php/etc/php.ini,加入如下的一行
extension = "imagick.so"
然后執行php -m | grep imagick,發現已經出現imagick模塊了,證明imagick已經生效了
[root@CentOS_Test_Server imagick-2.2.2]# php -m | grep imagick
imagick
上面只是命令行生效了,為了讓web服務器也生效,必須要重啟php-fpm(我的環境是Nginx+fastcgi方式執行的php)
[root@CentOS_Test_Server imagick-2.2.2]# /usr/local/webserver/php/sbin/php-fpm restart
Shutting down php_fpm . done
Starting php_fpm done
vi index.php,輸入如下的內容
<?php
phpinfo()
?>
在瀏覽器執行index.php即可看到模塊imagick已經生效
4.測試
PHP的imagick安裝以后,提供4個PHP的圖片處理類,分別是Imagick、ImagickDraw、ImagickPixel、ImagickPixelIterator,可以參考PHP的英文手冊(一般指的是chm格式的手冊,可以下載到本地查閱)或者在線手冊(http://cn.php.net/imagick)
可以用兩種方式進行調用,一種方式是直接調用/usr/local/imagemagick/bin目錄下的命令,第二種方式是調用PHP的imagick擴展提供的api
測試一,通過php擴展進行調用
vi /home/htdocs/www/imagick_test1.php,輸入如下的代碼
<?php
header('Content-type: image/jpeg');
$image = new Imagick('old_s.jpg');
// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);
echo $image;
?>
然后在瀏覽中執行,即可看到一張100px × 80px的圖片,old_s是大S的意思,也就是我是用大S的照片做的測試,在這里就不截圖了,其它的例子大家看手冊基本就會用了
另外可以在上述文件中的第一行加入判斷模塊imagick已經安裝或加載的代碼,extension_loaded('imagick') or die('imagick not loaded');
測試二,通過命令行進行調用
進入到圖片所在目錄
cd /home/htdocs/www
/usr/local/imagemagick/bin/convert old_s.jpg -resize 50% old_s.png
原來的圖片old_s.jpg是800*640,轉換后生成的圖片old_s.png是400*320,剛好減少一半
每個命令的選項少的有幾十個,多的則有上百個選項,非常復雜,功能非常的強大,參考http://www.imagemagick.org/script/command-line-tools.php
另外,Discuz采用了ImageMagick及GD兩種方式來處理圖片,Discuz7.0版本有個文件Discuz_7.0.0_SC_GBK/upload/include/image.class.php,基中的方法Thumb_IM及Watermark_IM使用的就是ImageMagick,大家可以參考一下Discuz的代碼。
BTW:我寫這篇博客是用的筆記本電腦的電池,因為有一段時間沒有用筆記本電腦的電池了,以后每月盡量充放電一次,以保持電池的容量,電池大概用了二個小時零10分鍾吧,還算可以
參考:
ImageMagick百度百科:http://baike.baidu.com/view/1109708.htm
ImageMagick中文站:http://www.imagemagick.com.cn/
ImageMagick英文站:http://www.imagemagick.org/
imagemagick安裝英文文檔:http://www.imagemagick.org/script/install-source.php
ImageMagick的ftp下載地址:ftp://ftp.imagemagick.org/pub/ImageMagick/
ImageMagick的api: http://www.imagemagick.org/script/api.php
ImageMagick的命令行參考文檔:http://www.imagemagick.org/script/command-line-tools.php
ImageMagick的PHP手冊:http://cn.php.net/imagick
ImageMagick的PHP擴展:http://pecl.php.net/package/imagick
