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

这篇文章主要介绍了php_imagick实现图片剪切、旋转、锐化、减色或增加特效的方法,可实现通过调用ImageMagick功能的PHP扩展使PHP具备和ImageMagick相同的功能,最终实现强大的ImageMagick图形处理功能,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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. header('Content-type: image/jpeg');
  2. $image = new Imagick('test.jpg');
  3. $color=new ImagickPixel();
  4. $color->setColor("rgb(220,220,220)");
  5. $image->borderImage($color,5,4);
  6. $image->blurImage(5,5,imagick::CHANNEL_GREEN);
  7. 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. ?>

现在我们进入正题吧,示例:

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

希望本文所述对大家的PHP程序设计有所帮助。