關於 ImageMagic 和 imagick 的介紹,見《圖片處理神器ImageMagick以及PHP的imagick擴展》 和 《Ubuntu下安裝ImageMagick和MagicWand For PHP》,安裝和代碼也都參考自這幾篇文章,同時記錄下了自己的安裝過程以及自己在安裝過程中遇到的問題。
說明:ImageMagic 的 PHP 擴展可以用 imagick 和 MagicWand for PHP,這里安裝使用 imagick。
安裝環境:Ubuntu 13.10 (GNU/Linux 3.11.0-12-generic i686)
Nginx 版本:1.2.7
Nginx 安裝路徑:/usr/local/nginx
Nginx 網站根目錄:/home/wwwroot
PHP 版本:5.3.27
PHP 框架:ThinkPHP 3.2.2
PHP 安裝路徑:/usr/local/php
PHP 配置文件 php.ini 路徑:/usr/local/php/etc/php.ini
(一)安裝 ImageMagic (6.9.1-6)
下載地址:http://www.imagemagick.org/download/ImageMagick-6.9.1-6.tar.gz
① 下載安裝包 ImageMagick-6.9.1-6.tar.gz 並通過 ftp 上傳到服務器環境,可以通過命令:
find / -name *.tar.gz
找到之前安裝包的存放路徑,cd 進入該目錄:
cd /home/***/lnmp/lnmp1.0-full/
解壓壓縮包:
tar -zxvf ImageMagick-6.9.1-6.tar.gz
cd 進入解壓之后的目錄:
cd ImageMagick-6.9.1-6/
② 配置:
./configure --enable-shared --enable-lzw --without-perl --with-modules
③ 編譯和安裝:
make && make install
④ 測試 ImageMagic 是否安裝成功:
convert -version
如果執行該命令后報錯:
/usr/local/bin/convert: error while loading shared libraries: libMagickCore-6.Q16.so.2: cannot open shared object file: No such file or directory
則需要執行 ldconfig(ldconfig命令的用途, 主要是在默認搜尋目錄( /lib和/usr/lib ) 以及動態庫配置文件 /etc/ld.so.conf 內所列的目錄下, 搜索出可共享的動態鏈接庫( 格式如lib*.so* ), 進而創建出動態裝入程序( ld.so )所需的連接和緩存文件. 緩存文件默認為/etc/ld.so.cache, 此文件保存已排好序的動態鏈接庫名字列表. ):
root@***:/home/***/lnmp/lnmp1.0-full/ImageMagick-6.9.1-6# ldconfig
此時再使用 convert -version,就可以顯示版本信息了,也就說明安裝成功了:
root@***:/home/***/lnmp/lnmp1.0-full/ImageMagick-6.9.1-6# /usr/local/bin/convert -version Version: ImageMagick 6.9.1-6 Q16 i686 2016-06-15 http://www.imagemagick.org Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC License: http://www.imagemagick.org/script/license.php Features: Cipher DPC Modules OpenMP Delegates (built-in): bzlib freetype jng jpeg ltdl png xml zlib
(參考:《undefined reference to `inflateReset2'》)
(二). 安裝 imagick
主要參考《Linux下php安裝imagick》
① 下載、拷貝、安裝 imagick
下載地址:http://pecl.php.net/get/imagick-3.0.1.tgz
root@***:/home/***/lnmp/lnmp1.0-full# tar -zxvf imagick-3.0.1.tgz
解壓之后,進入解壓后的目錄:
root@***:/home/***/lnmp/lnmp1.0-full# cd imagick-3.0.1/
② 用 phpize 生成 configure 配置文件
root@***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# /usr/local/php/bin/phpize
如果報錯:checking for MagickWand.h header file... configure: error: Cannot locate header file MagickWand.h,則:
ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick
(參考: 《安裝imagick時Cannot locate header file MagickWand.h錯誤的解決》)
③ 配置:
root@***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# ./configure --with-php-config=/usr/local/php/bin/php-config --with-imagick=/usr/local/imagemagick
④ 編譯與安裝
make && make install
make install 成功后顯示:
root@***:/home/***/lnmp/lnmp1.0-full/imagick-3.0.1# make install Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ Installing header files: /usr/local/php/include/php/
⑤ 配置 php,讓 php 支持 imagick
vi /usr/local/php/etc/php.ini #編輯配置文件,在最后一行添加以下內容 extension="imagick.so"
⑥ 重啟 php-fpm:
ps aux | grep php-fpm kill -QUIT 1219 /usr/local/php/sbin/php-fpm
(參考:《php修改php.ini重啟nginx php.ini設置不生效》)
⑦ 重啟 Nginx:
nginx -s reload
⑧ 測試 imagick 是否添加成功:
在 php 文件中輸入:
<?php
phpinfo();
如果在 info 頁有如下內容,則說明添加成功:
(三)使用 imagick 減小上傳圖片的品質(80%)
工具類:
Imgicktool.class.php:
<?php /* * 圖片壓縮類 重新封裝了Imagick */ namespace Home\Common; class Imgicktool{ //Imagick對象實例 public $obj = null; public function __construct() { //判斷是否加載了該擴展 if(!extension_loaded('Imagick')) { return false; } $this->obj = new \Imagick(); } /* * png2jpg轉換圖片格式 * * @param string src_img 源圖片路徑 * @param string dest_img 要生成的圖片的路徑 * @return boolean 轉換成共返回true 否則false */ public function png2jpg($src_img,$dest_img) { if(!is_object($this->obj)) { return false; } try { $this->obj->readImage($src_img); if($this->obj->writeImage($dest_img)) { $this->destory(); return $dest_img; } return false; } catch (ImagickException $e) { return false; } } /* * 去掉圖片的profile信息 * * @param string src_img 源圖片路徑 * @return string src_img 圖片名稱 否則返回false */ public function strip_profile($src_img,$dest_img = '') { if(!is_object($this->obj)) { return false; } try { $dest_img = empty($dest_img) ? $src_img : $dest_img; $this->obj->readImage($src_img); $this->obj->stripImage (); if($this->obj->writeImage ($dest_img)) { $this->destory(); return $src_img; } return false; } catch (ImagickException $e) { return false; } } /* * 設置jpg圖片質量 * * @param string src_img 源圖片路徑 * @param string dest_img 要生成的圖片的路徑 * @return boolean 轉換成共返回true 否則false */ public function set_quality($src_img,$quality = 70,$dest_img = '') { if(!is_object($this->obj)) { echo 'error1'; return false; } try { $dest_img = empty($dest_img) ? $src_img : $dest_img; $this->obj->readImage($src_img); $this->obj->setImageCompression(Imagick::COMPRESSION_JPEG); $this->obj->setImageCompressionQuality($quality); if($this->obj->writeImage($dest_img)) { $this->destory(); return $dest_img; } echo 'error2'; return false; } catch (ImagickException $e) { echo 'error3'; return false; } } /* * 圖片瘦身 * * @param string src_img 源圖片路徑 * @param int quality 設置圖片壓縮質量 * @param string dest_img 要生成的圖片的路徑 * @return boolean 轉換成共返回true 否則false */ public function slimming($src_img,$quality = 60,$dest_img = '') { if(!is_object($this->obj)) { return false; } try { $dest_img = empty($dest_img) ? $src_img : $dest_img; $this->obj->readImage($src_img); $this->obj->setImageFormat('jpeg'); $this->obj->setImageCompression(\Imagick::COMPRESSION_JPEG); //將圖片的質量降低到原來的60% $quality = $this->obj->getImageCompressionQuality() * $quality / 100; $this->obj->setImageCompressionQuality($quality); $this->obj->stripImage(); if($this->obj->writeImage($dest_img)) { $this->destory(); return $dest_img; } return false; } catch (ImagickException $e) { return false; } } /* * 生成縮略圖 * * @param string src_img 源圖片路徑 * @param int quality 設置圖片壓縮質量 * @param string dest_img 要生成的圖片的路徑 * @return boolean 轉換成共返回true 否則false */ public function thump($src_img,$width = 250,$height = '') { if(!is_object($this->obj)) { return false; } try { $file_info = pathinfo($src_img); //生成縮略圖名稱 $file_name = substr($file_info['basename'],0,strrpos($file_info['basename'],'.')); $dest_img = $file_info['dirname'] . '/' . $file_name . '_thump.' . $file_info['extension']; $this->obj->readImage($src_img); //計算要獲得縮略圖的高度 $img_width = $this->obj->getImageWidth(); $img_height = $this->obj->getImageHeight(); $dest_height = $img_height * ($width / $img_width); $this->obj->resizeImage($width, $dest_height, Imagick::FILTER_CATROM, 1, false); //生成圖片 if($this->obj->writeImage($dest_img)) { $this->destory(); return $dest_img; } return false; } catch (ImagickException $e) { return false; } } /* * 釋放資源 * */ function destory() { if(is_object($this->obj)) { $this->obj->clear(); $this->obj->destroy(); } } }
測試文件 ImgController.class.php:
<?php namespace Home\Controller; use Home\Common\Imgicktool; use Think\Controller; class ImgController extends Controller{ function info(){ phpinfo(); } function quality(){ $srcFile = APP_PATH.'/test/test.jpg'; $destFile = APP_PATH.'/test/test.jpg'; $qua = new Imgicktool(); $qua->slimming($srcFile, 80, $destFile); } }
參考:
1.《Ubuntu下安裝ImageMagick和MagicWand For PHP》
2.《undefined reference to `inflateReset2'》
3.《php修改php.ini重啟nginx php.ini設置不生效》
5.《安裝imagick時Cannot locate header file MagickWand.h錯誤的解決》
7. Centos 安裝 ImageMagic 時,make 報錯,參考《紅帽安裝ImageMagick出錯》
8. PHP 5.4的不能安裝 imagick-3.0.1《linux下安裝ImageMagick-6.5.1-2 老是編譯有錯誤,哪位高手知道怎么解決嗎》,這是在正式環境下 (CentOS6)下遇到的問題,imagick 各個版本下載地址:http://pecl.php.net/package/imagick/