PHP Imagick完美实现图片裁切、生成缩略图、添加水印

这篇文章主要介绍了PHP Imagick完美实现图片裁切、生成缩略图、添加水印的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

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