php_imagick实现图片剪切、旋转、锐化、减色或增加特效

一个可以供PHP调用ImageMagick功能的PHP扩展。使用这个扩展可以使PHP具备和ImageMagick相同的功能。

ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过185种基本格式的图片文件,包括流行的TIFF, JPEG, GIF, PNG, PDF以及PhotoCD等格式。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。

php_imagick是PHP对图片处理的一个扩展包,可以完成对图片改变大小、旋转、锐化、减色或增加特效等操作。

一、windows下安装Imagick扩展:

1、下载 ImageMagick并安装

http://image_magick.veidrodis.com/image_magick/binaries/ImageMagick-6.6.2-10-Q16-windows-dll.exe

2、下载php_imagick.dll

http://valokuva.org/outside-blog-content/imagick-windows-builds/php53/imagick-2.3.0-dev/vc9_nts/php_imagick.dll

如果你用的是线程安全的php,请下载

http://valokuva.org/outside-blog-content/imagick- windows-builds/php53/imagick-2.3.0-dev/vc9_zts/php_imagick.dll

3、设置

在php.ini中添加

extension=php_imagick.dll ,重启web server

二、linux下安装Imagick扩展:

1.yum安装ImageMagick

yum install ImageMagick ImageMagick-devel

2.测试是否安装成功

convert -version

3.安装imagick扩展

01.wget http://pecl.php.net/get/imagick-3.1.0RC2.tgz02.tar xzvf imagick-3.1.0RC2.tgz03.cd imagick-3.1.0RC204.phpize05../configure06.make07.make install

4.编辑php.ini文件,在文件末尾添加如下代码

extension=imagick.so

5. 重新启动apache服务器

service httpd restart

三、案例

1. 边框处理

  1. //by www.phpfensi.com
  2. header('Content-type: image/jpeg');
  3. $image = new Imagick('test.jpg');
  4. $color=new ImagickPixel();
  5. $color->setColor("rgb(220,220,220)");
  6. $image->borderImage($color,5,4);
  7. $image->blurImage(5,5,imagick::CHANNEL_GREEN);
  8. echo $image;

我们先来看个简单的实例

php_imagick程序示例

1.创建一个缩略图并显示出来

  1. <?php
  2. header('Content-type: image/jpeg');
  3. $image = new Imagick('image.jpg');
  4. // If 0 is provided as a width or height parameter,// aspect ratio is maintained
  5. $image->thumbnailImage(100, 0);
  6. echo $image;
  7. ?>

2.创建一个目录下的缩略图,并保存

  1. <?php
  2. $images = new Imagick(glob('images/*.JPG'));
  3. foreach($images as $image) {
  4. // Providing 0 forces thumbnailImage to maintain aspect ratio
  5. $image->thumbnailImage(1024,0);
  6. }
  7. $images->writeImages();
  8. ?>

3.缩略GIF动画图片

  1. <?php
  2. /* Create a new imagick object and read in GIF */
  3. $im = new Imagick("example.gif");
  4. /* Resize all frames */
  5. foreach ($im as $frame) {
  6. /* 50x50 frames */
  7. $frame->thumbnailImage(50, 50);
  8. /* Set the virtual canvas to correct size */
  9. $frame->setImagePage(50, 50, 0, 0);
  10. }/* Notice writeImages instead of writeImage */
  11. $im->writeImages("example_small.gif", true);
  12. ?>

现在我们进入正题吧,

例1

裁切/生成缩略图/添加水印, 自动检测和处理 GIF

调用方式:

  1. include 'imagick.class.php';
  2. $image = new lib_image_imagick();
  3. $image->open('a.gif');
  4. $image->resize_to(100, 100, 'scale_fill');
  5. $image->add_text('1024i.com', 10, 20);
  6. $image->add_watermark('1024i.gif', 10, 50);
  7. $image->save_to('x.gif');

imagick.class.php

  1. <?php
  2. class lib_image_imagick
  3. {
  4. private $image = null;
  5. private $type = null;
  6. // 构造函数
  7. public function __construct(){}
  8. // 析构函数
  9. public function __destruct()
  10. {
  11. if($this->image!==null) $this->image->destroy();
  12. }
  13. // 载入图像
  14. public function open($path)
  15. {
  16. $this->image = new Imagick( $path );
  17. if($this->image)
  18. {
  19. $this->type = strtolower($this->image->getImageFormat());
  20. }
  21. return $this->image;
  22. }
  23. public function crop($x=0, $y=0, $width=null, $height=null)
  24. {
  25. if($width==null) $width = $this->image->getImageWidth()-$x;
  26. if($height==null) $height = $this->image->getImageHeight()-$y;
  27. if($width<=0 || $height<=0) return;
  28. if($this->type=='gif')
  29. {
  30. $image = $this->image;
  31. $canvas = new Imagick();
  32. $images = $image->coalesceImages();
  33. foreach($images as $frame){
  34. $img = new Imagick();
  35. $img->readImageBlob($frame);
  36. $img->cropImage($width, $height, $x, $y);
  37. $canvas->addImage( $img );
  38. $canvas->setImageDelay( $img->getImageDelay() );
  39. $canvas->setImagePage($width, $height, 0, 0);
  40. }
  41. $image->destroy();
  42. $this->image = $canvas;
  43. }
  44. else
  45. {
  46. $this->image->cropImage($width, $height, $x, $y);
  47. }
  48. }
  49. /*
  50. * 更改图像大小
  51. $fit: 适应大小方式
  52. 'force': 把图片强制变形成 $width X $height 大小
  53. 'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
  54. 'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明))
  55. 其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
  56. $fit = 'force','scale','scale_fill' 时: 输出完整图像
  57. $fit = 图像方位值 时, 输出指定位置部分图像
  58. 字母与图像的对应关系如下:
  59. north_west north north_east
  60. west center east
  61. south_west south south_east
  62. */
  63. public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255,255,255,0) )
  64. {
  65. switch($fit)
  66. {
  67. case 'force':
  68. if($this->type=='gif')
  69. {
  70. $image = $this->image;
  71. $canvas = new Imagick();
  72. $images = $image->coalesceImages();
  73. foreach($images as $frame){
  74. $img = new Imagick();
  75. $img->readImageBlob($frame);
  76. $img->thumbnailImage( $width, $height, false );
  77. $canvas->addImage( $img );
  78. $canvas->setImageDelay( $img->getImageDelay() );
  79. }
  80. $image->destroy();
  81. $this->image = $canvas;
  82. }
  83. else
  84. {
  85. $this->image->thumbnailImage( $width, $height, false );
  86. }
  87. break;
  88. case 'scale':
  89. if($this->type=='gif')
  90. {
  91. $image = $this->image;
  92. $images = $image->coalesceImages();
  93. $canvas = new Imagick();
  94. foreach($images as $frame){
  95. $img = new Imagick();
  96. $img->readImageBlob($frame);
  97. $img->thumbnailImage( $width, $height, true );
  98. $canvas->addImage( $img );
  99. $canvas->setImageDelay( $img->getImageDelay() );
  100. }
  101. $image->destroy();
  102. $this->image = $canvas;
  103. }
  104. else
  105. {
  106. $this->image->thumbnailImage( $width, $height, true );
  107. }
  108. break;
  109. case 'scale_fill':
  110. $size = $this->image->getImagePage();
  111. $src_width = $size['width'];
  112. $src_height = $size['height'];
  113. $x = 0;
  114. $y = 0;
  115. $dst_width = $width;
  116. $dst_height = $height;
  117. if($src_width*$height > $src_height*$width)
  118. {
  119. $dst_height = intval($width*$src_height/$src_width);
  120. $y = intval( ($height-$dst_height)/2 );
  121. }
  122. else
  123. {
  124. $dst_width = intval($height*$src_width/$src_height);
  125. $x = intval( ($width-$dst_width)/2 );
  126. }
  127. $image = $this->image;
  128. $canvas = new Imagick();
  129. $color = 'rgba('.$fill_color[0].','.$fill_color[1].','.$fill_color[2].','.$fill_color[3].')';
  130. if($this->type=='gif')
  131. {
  132. $images = $image->coalesceImages();
  133. foreach($images as $frame)
  134. {
  135. $frame->thumbnailImage( $width, $height, true );
  136. $draw = new ImagickDraw();
  137. $draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
  138. $img = new Imagick();
  139. $img->newImage($width, $height, $color, 'gif');
  140. $img->drawImage($draw);
  141. $canvas->addImage( $img );
  142. $canvas->setImageDelay( $img->getImageDelay() );
  143. $canvas->setImagePage($width, $height, 0, 0);
  144. }
  145. }
  146. else
  147. {
  148. $image->thumbnailImage( $width, $height, true );
  149. $draw = new ImagickDraw();
  150. $draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
  151. $canvas->newImage($width, $height, $color, $this->get_type() );
  152. $canvas->drawImage($draw);
  153. $canvas->setImagePage($width, $height, 0, 0);
  154. }
  155. $image->destroy();
  156. $this->image = $canvas;
  157. break;
  158. default:
  159. $size = $this->image->getImagePage();
  160. $src_width = $size['width'];
  161. $src_height = $size['height'];
  162. $crop_x = 0;
  163. $crop_y = 0;
  164. $crop_w = $src_width;
  165. $crop_h = $src_height;
  166. if($src_width*$height > $src_height*$width)
  167. {
  168. $crop_w = intval($src_height*$width/$height);
  169. }
  170. else
  171. {
  172. $crop_h = intval($src_width*$height/$width);
  173. }
  174. switch($fit)
  175. {
  176. case 'north_west':
  177. $crop_x = 0;
  178. $crop_y = 0;
  179. break;
  180. case 'north':
  181. $crop_x = intval( ($src_width-$crop_w)/2 );
  182. $crop_y = 0;
  183. break;
  184. case 'north_east':
  185. $crop_x = $src_width-$crop_w;
  186. $crop_y = 0;
  187. break;
  188. case 'west':
  189. $crop_x = 0;
  190. $crop_y = intval( ($src_height-$crop_h)/2 );
  191. break;
  192. case 'center':
  193. $crop_x = intval( ($src_width-$crop_w)/2 );
  194. $crop_y = intval( ($src_height-$crop_h)/2 );
  195. break;
  196. case 'east':
  197. $crop_x = $src_width-$crop_w;
  198. $crop_y = intval( ($src_height-$crop_h)/2 );
  199. break;
  200. case 'south_west':
  201. $crop_x = 0;
  202. $crop_y = $src_height-$crop_h;
  203. break;
  204. case 'south':
  205. $crop_x = intval( ($src_width-$crop_w)/2 );
  206. $crop_y = $src_height-$crop_h;
  207. break;
  208. case 'south_east':
  209. $crop_x = $src_width-$crop_w;
  210. $crop_y = $src_height-$crop_h;
  211. break;
  212. default:
  213. $crop_x = intval( ($src_width-$crop_w)/2 );
  214. $crop_y = intval( ($src_height-$crop_h)/2 );
  215. }
  216. $image = $this->image;
  217. $canvas = new Imagick();
  218. if($this->type=='gif')
  219. {
  220. $images = $image->coalesceImages();
  221. foreach($images as $frame){
  222. $img = new Imagick();
  223. $img->readImageBlob($frame);
  224. $img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  225. $img->thumbnailImage( $width, $height, true );
  226. $canvas->addImage( $img );
  227. $canvas->setImageDelay( $img->getImageDelay() );
  228. $canvas->setImagePage($width, $height, 0, 0);
  229. }
  230. }
  231. else
  232. {
  233. $image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
  234. $image->thumbnailImage( $width, $height, true );
  235. $canvas->addImage( $image );
  236. $canvas->setImagePage($width, $height, 0, 0);
  237. }
  238. $image->destroy();
  239. $this->image = $canvas;
  240. }
  241. }
  242. // 添加水印图片
  243. public function add_watermark($path, $x = 0, $y = 0)
  244. {
  245. $watermark = new Imagick($path);
  246. $draw = new ImagickDraw();
  247. $draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
  248. if($this->type=='gif')
  249. {
  250. $image = $this->image;
  251. $canvas = new Imagick();
  252. $images = $image->coalesceImages();
  253. foreach($image as $frame)
  254. {
  255. $img = new Imagick();
  256. $img->readImageBlob($frame);
  257. $img->drawImage($draw);
  258. $canvas->addImage( $img );
  259. $canvas->setImageDelay( $img->getImageDelay() );
  260. }
  261. $image->destroy();
  262. $this->image = $canvas;
  263. }
  264. else
  265. {
  266. $this->image->drawImage($draw);
  267. }
  268. }
  269. // 添加水印文字
  270. public function add_text($text, $x = 0 , $y = 0, $angle=0, $style=array())
  271. {
  272. $draw = new ImagickDraw();
  273. if(isset($style['font'])) $draw->setFont($style['font']);
  274. if(isset($style['font_size'])) $draw->setFontSize($style['font_size']);
  275. if(isset($style['fill_color'])) $draw->setFillColor($style['fill_color']);
  276. if(isset($style['under_color'])) $draw->setTextUnderColor($style['under_color']);
  277. if($this->type=='gif')
  278. {
  279. foreach($this->image as $frame)
  280. {
  281. $frame->annotateImage($draw, $x, $y, $angle, $text);
  282. }
  283. }
  284. else
  285. {
  286. $this->image->annotateImage($draw, $x, $y, $angle, $text);
  287. }
  288. }
  289. // 保存到指定路径
  290. public function save_to( $path )
  291. {
  292. if($this->type=='gif')
  293. {
  294. $this->image->writeImages($path, true);
  295. }
  296. else
  297. {
  298. $this->image->writeImage($path);
  299. }
  300. }
  301. // 输出图像
  302. public function output($header = true)
  303. {
  304. if($header) header('Content-type: '.$this->type);
  305. echo $this->image->getImagesBlob();
  306. }
  307. public function get_width()
  308. {
  309. $size = $this->image->getImagePage();
  310. return $size['width'];
  311. }
  312. public function get_height()
  313. {
  314. $size = $this->image->getImagePage();
  315. return $size['height'];
  316. }
  317. // 设置图像类型, 默认与源类型一致
  318. public function set_type( $type='png' )
  319. {
  320. $this->type = $type;
  321. $this->image->setImageFormat( $type );
  322. }
  323. // 获取源图像类型
  324. public function get_type()
  325. {
  326. return $this->type;
  327. }
  328. // 当前对象是否为图片
  329. public function is_image()
  330. {
  331. if( $this->image )
  332. return true;
  333. else
  334. return false;
  335. }
  336. public function thumbnail($width = 100, $height = 100, $fit = true){ $this->image->thumbnailImage( $width, $height, $fit );} // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片
  337. /*
  338. 添加一个边框
  339. $width: 左右边框宽度
  340. $height: 上下边框宽度
  341. $color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'...
  342. */
  343. public function border($width, $height, $color='rgb(220, 220, 220)')
  344. {
  345. $color=new ImagickPixel();
  346. $color->setColor($color);
  347. $this->image->borderImage($color, $width, $height);
  348. }
  349. public function blur($radius, $sigma){$this->image->blurImage($radius, $sigma);} // 模糊
  350. public function gaussian_blur($radius, $sigma){$this->image->gaussianBlurImage($radius, $sigma);} // 高斯模糊
  351. public function motion_blur($radius, $sigma, $angle){$this->image->motionBlurImage($radius, $sigma, $angle);} // 运动模糊
  352. public function radial_blur($radius){$this->image->radialBlurImage($radius);} // 径向模糊
  353. public function add_noise($type=null){$this->image->addNoiseImage($type==null?imagick::NOISE_IMPULSE:$type);} // 添加噪点
  354. public function level($black_point, $gamma, $white_point){$this->image->levelImage($black_point, $gamma, $white_point);} // 调整色阶
  355. public function modulate($brightness, $saturation, $hue){$this->image->modulateImage($brightness, $saturation, $hue);} // 调整亮度、饱和度、色调
  356. public function charcoal($radius, $sigma){$this->image->charcoalImage($radius, $sigma);} // 素描
  357. public function oil_paint($radius){$this->image->oilPaintImage($radius);} // 油画效果
  358. public function flop(){$this->image->flopImage();} // 水平翻转
  359. public function flip(){$this->image->flipImage();} // 垂直翻转
  360. }