Imagick.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. // | ImageImagick.class.php 2013-03-06
  12. // +----------------------------------------------------------------------
  13. namespace Think\Image\Driver;
  14. use Think\Image;
  15. class Imagick
  16. {
  17. /**
  18. * 图像资源对象
  19. * @var resource
  20. */
  21. private $img;
  22. /**
  23. * 图像信息,包括width,height,type,mime,size
  24. * @var array
  25. */
  26. private $info;
  27. /**
  28. * 构造方法,可用于打开一张图像
  29. * @param string $imgname 图像路径
  30. */
  31. public function __construct($imgname = null)
  32. {
  33. $imgname && $this->open($imgname);
  34. }
  35. /**
  36. * 打开一张图像
  37. * @param string $imgname 图像路径
  38. */
  39. public function open($imgname)
  40. {
  41. //检测图像文件
  42. if (!is_file($imgname)) {
  43. E('不存在的图像文件');
  44. }
  45. //销毁已存在的图像
  46. empty($this->img) || $this->img->destroy();
  47. //载入图像
  48. $this->img = new \Imagick(realpath($imgname));
  49. //设置图像信息
  50. $this->info = array(
  51. 'width' => $this->img->getImageWidth(),
  52. 'height' => $this->img->getImageHeight(),
  53. 'type' => strtolower($this->img->getImageFormat()),
  54. 'mime' => $this->img->getImageMimeType(),
  55. );
  56. }
  57. /**
  58. * 保存图像
  59. * @param string $imgname 图像保存名称
  60. * @param string $type 图像类型
  61. * @param integer $quality JPEG图像质量
  62. * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
  63. */
  64. public function save($imgname, $type = null, $quality = 80, $interlace = true)
  65. {
  66. if (empty($this->img)) {
  67. E('没有可以被保存的图像资源');
  68. }
  69. //设置图片类型
  70. if (is_null($type)) {
  71. $type = $this->info['type'];
  72. } else {
  73. $type = strtolower($type);
  74. $this->img->setImageFormat($type);
  75. }
  76. //JPEG图像设置隔行扫描
  77. if ('jpeg' == $type || 'jpg' == $type) {
  78. $this->img->setImageInterlaceScheme(1);
  79. }
  80. // 设置图像质量
  81. $this->img->setImageCompressionQuality($quality);
  82. //去除图像配置信息
  83. $this->img->stripImage();
  84. //保存图像
  85. $imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径
  86. if ('gif' == $type) {
  87. $this->img->writeImages($imgname, true);
  88. } else {
  89. $this->img->writeImage($imgname);
  90. }
  91. }
  92. /**
  93. * 返回图像宽度
  94. * @return integer 图像宽度
  95. */
  96. public function width()
  97. {
  98. if (empty($this->img)) {
  99. E('没有指定图像资源');
  100. }
  101. return $this->info['width'];
  102. }
  103. /**
  104. * 返回图像高度
  105. * @return integer 图像高度
  106. */
  107. public function height()
  108. {
  109. if (empty($this->img)) {
  110. E('没有指定图像资源');
  111. }
  112. return $this->info['height'];
  113. }
  114. /**
  115. * 返回图像类型
  116. * @return string 图像类型
  117. */
  118. public function type()
  119. {
  120. if (empty($this->img)) {
  121. E('没有指定图像资源');
  122. }
  123. return $this->info['type'];
  124. }
  125. /**
  126. * 返回图像MIME类型
  127. * @return string 图像MIME类型
  128. */
  129. public function mime()
  130. {
  131. if (empty($this->img)) {
  132. E('没有指定图像资源');
  133. }
  134. return $this->info['mime'];
  135. }
  136. /**
  137. * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
  138. * @return array 图像尺寸
  139. */
  140. public function size()
  141. {
  142. if (empty($this->img)) {
  143. E('没有指定图像资源');
  144. }
  145. return array($this->info['width'], $this->info['height']);
  146. }
  147. /**
  148. * 裁剪图像
  149. * @param integer $w 裁剪区域宽度
  150. * @param integer $h 裁剪区域高度
  151. * @param integer $x 裁剪区域x坐标
  152. * @param integer $y 裁剪区域y坐标
  153. * @param integer $width 图像保存宽度
  154. * @param integer $height 图像保存高度
  155. */
  156. public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
  157. {
  158. if (empty($this->img)) {
  159. E('没有可以被裁剪的图像资源');
  160. }
  161. //设置保存尺寸
  162. empty($width) && $width = $w;
  163. empty($height) && $height = $h;
  164. //裁剪图片
  165. if ('gif' == $this->info['type']) {
  166. $img = $this->img->coalesceImages();
  167. $this->img->destroy(); //销毁原图
  168. //循环裁剪每一帧
  169. do {
  170. $this->_crop($w, $h, $x, $y, $width, $height, $img);
  171. } while ($img->nextImage());
  172. //压缩图片
  173. $this->img = $img->deconstructImages();
  174. $img->destroy(); //销毁零时图片
  175. } else {
  176. $this->_crop($w, $h, $x, $y, $width, $height);
  177. }
  178. }
  179. /* 裁剪图片,内部调用 */
  180. private function _crop($w, $h, $x, $y, $width, $height, $img = null)
  181. {
  182. is_null($img) && $img = $this->img;
  183. //裁剪
  184. $info = $this->info;
  185. if (0 != $x || 0 != $y || $w != $info['width'] || $h != $info['height']) {
  186. $img->cropImage($w, $h, $x, $y);
  187. $img->setImagePage($w, $h, 0, 0); //调整画布和图片一致
  188. }
  189. //调整大小
  190. if ($w != $width || $h != $height) {
  191. $img->sampleImage($width, $height);
  192. }
  193. //设置缓存尺寸
  194. $this->info['width'] = $width;
  195. $this->info['height'] = $height;
  196. }
  197. /**
  198. * 生成缩略图
  199. * @param integer $width 缩略图最大宽度
  200. * @param integer $height 缩略图最大高度
  201. * @param integer $type 缩略图裁剪类型
  202. */
  203. public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE)
  204. {
  205. if (empty($this->img)) {
  206. E('没有可以被缩略的图像资源');
  207. }
  208. //原图宽度和高度
  209. $w = $this->info['width'];
  210. $h = $this->info['height'];
  211. /* 计算缩略图生成的必要参数 */
  212. switch ($type) {
  213. /* 等比例缩放 */
  214. case Image::IMAGE_THUMB_SCALE:
  215. //原图尺寸小于缩略图尺寸则不进行缩略
  216. if ($w < $width && $h < $height) {
  217. return;
  218. }
  219. //计算缩放比例
  220. $scale = min($width / $w, $height / $h);
  221. //设置缩略图的坐标及宽度和高度
  222. $x = $y = 0;
  223. $width = $w * $scale;
  224. $height = $h * $scale;
  225. break;
  226. /* 居中裁剪 */
  227. case Image::IMAGE_THUMB_CENTER:
  228. //计算缩放比例
  229. $scale = max($width / $w, $height / $h);
  230. //设置缩略图的坐标及宽度和高度
  231. $w = $width / $scale;
  232. $h = $height / $scale;
  233. $x = ($this->info['width'] - $w) / 2;
  234. $y = ($this->info['height'] - $h) / 2;
  235. break;
  236. /* 左上角裁剪 */
  237. case Image::IMAGE_THUMB_NORTHWEST:
  238. //计算缩放比例
  239. $scale = max($width / $w, $height / $h);
  240. //设置缩略图的坐标及宽度和高度
  241. $x = $y = 0;
  242. $w = $width / $scale;
  243. $h = $height / $scale;
  244. break;
  245. /* 右下角裁剪 */
  246. case Image::IMAGE_THUMB_SOUTHEAST:
  247. //计算缩放比例
  248. $scale = max($width / $w, $height / $h);
  249. //设置缩略图的坐标及宽度和高度
  250. $w = $width / $scale;
  251. $h = $height / $scale;
  252. $x = $this->info['width'] - $w;
  253. $y = $this->info['height'] - $h;
  254. break;
  255. /* 填充 */
  256. case Image::IMAGE_THUMB_FILLED:
  257. //计算缩放比例
  258. if ($w < $width && $h < $height) {
  259. $scale = 1;
  260. } else {
  261. $scale = min($width / $w, $height / $h);
  262. }
  263. //设置缩略图的坐标及宽度和高度
  264. $neww = $w * $scale;
  265. $newh = $h * $scale;
  266. $posx = ($width - $w * $scale) / 2;
  267. $posy = ($height - $h * $scale) / 2;
  268. //创建一张新图像
  269. $newimg = new \Imagick();
  270. $newimg->newImage($width, $height, 'white', $this->info['type']);
  271. if ('gif' == $this->info['type']) {
  272. $imgs = $this->img->coalesceImages();
  273. $img = new \Imagick();
  274. $this->img->destroy(); //销毁原图
  275. //循环填充每一帧
  276. do {
  277. //填充图像
  278. $image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);
  279. $img->addImage($image);
  280. $img->setImageDelay($imgs->getImageDelay());
  281. $img->setImagePage($width, $height, 0, 0);
  282. $image->destroy(); //销毁零时图片
  283. } while ($imgs->nextImage());
  284. //压缩图片
  285. $this->img->destroy();
  286. $this->img = $img->deconstructImages();
  287. $imgs->destroy(); //销毁零时图片
  288. $img->destroy(); //销毁零时图片
  289. } else {
  290. //填充图像
  291. $img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
  292. //销毁原图
  293. $this->img->destroy();
  294. $this->img = $img;
  295. }
  296. //设置新图像属性
  297. $this->info['width'] = $width;
  298. $this->info['height'] = $height;
  299. return;
  300. /* 固定 */
  301. case Image::IMAGE_THUMB_FIXED:
  302. $x = $y = 0;
  303. break;
  304. default:
  305. E('不支持的缩略图裁剪类型');
  306. }
  307. /* 裁剪图像 */
  308. $this->crop($w, $h, $x, $y, $width, $height);
  309. }
  310. /* 填充指定图像,内部使用 */
  311. private function _fill($newimg, $posx, $posy, $neww, $newh, $img = null)
  312. {
  313. is_null($img) && $img = $this->img;
  314. /* 将指定图片绘入空白图片 */
  315. $draw = new \ImagickDraw();
  316. $draw->composite($img->getImageCompose(), $posx, $posy, $neww, $newh, $img);
  317. $image = $newimg->clone();
  318. $image->drawImage($draw);
  319. $draw->destroy();
  320. return $image;
  321. }
  322. /**
  323. * 添加水印
  324. * @param string $source 水印图片路径
  325. * @param integer $locate 水印位置
  326. * @param integer $alpha 水印透明度
  327. */
  328. public function water($source, $locate = Image::IMAGE_WATER_SOUTHEAST, $alpha = 80)
  329. {
  330. //资源检测
  331. if (empty($this->img)) {
  332. E('没有可以被添加水印的图像资源');
  333. }
  334. if (!is_file($source)) {
  335. E('水印图像不存在');
  336. }
  337. //创建水印图像资源
  338. $water = new \Imagick(realpath($source));
  339. $info = array($water->getImageWidth(), $water->getImageHeight());
  340. /* 设定水印位置 */
  341. switch ($locate) {
  342. /* 右下角水印 */
  343. case Image::IMAGE_WATER_SOUTHEAST:
  344. $x = $this->info['width'] - $info[0];
  345. $y = $this->info['height'] - $info[1];
  346. break;
  347. /* 左下角水印 */
  348. case Image::IMAGE_WATER_SOUTHWEST:
  349. $x = 0;
  350. $y = $this->info['height'] - $info[1];
  351. break;
  352. /* 左上角水印 */
  353. case Image::IMAGE_WATER_NORTHWEST:
  354. $x = $y = 0;
  355. break;
  356. /* 右上角水印 */
  357. case Image::IMAGE_WATER_NORTHEAST:
  358. $x = $this->info['width'] - $info[0];
  359. $y = 0;
  360. break;
  361. /* 居中水印 */
  362. case Image::IMAGE_WATER_CENTER:
  363. $x = ($this->info['width'] - $info[0]) / 2;
  364. $y = ($this->info['height'] - $info[1]) / 2;
  365. break;
  366. /* 下居中水印 */
  367. case Image::IMAGE_WATER_SOUTH:
  368. $x = ($this->info['width'] - $info[0]) / 2;
  369. $y = $this->info['height'] - $info[1];
  370. break;
  371. /* 右居中水印 */
  372. case Image::IMAGE_WATER_EAST:
  373. $x = $this->info['width'] - $info[0];
  374. $y = ($this->info['height'] - $info[1]) / 2;
  375. break;
  376. /* 上居中水印 */
  377. case Image::IMAGE_WATER_NORTH:
  378. $x = ($this->info['width'] - $info[0]) / 2;
  379. $y = 0;
  380. break;
  381. /* 左居中水印 */
  382. case Image::IMAGE_WATER_WEST:
  383. $x = 0;
  384. $y = ($this->info['height'] - $info[1]) / 2;
  385. break;
  386. default:
  387. /* 自定义水印坐标 */
  388. if (is_array($locate)) {
  389. list($x, $y) = $locate;
  390. } else {
  391. E('不支持的水印位置类型');
  392. }
  393. }
  394. //创建绘图资源
  395. $draw = new \ImagickDraw();
  396. $draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water);
  397. if ('gif' == $this->info['type']) {
  398. $img = $this->img->coalesceImages();
  399. $this->img->destroy(); //销毁原图
  400. do {
  401. //添加水印
  402. $img->drawImage($draw);
  403. } while ($img->nextImage());
  404. //压缩图片
  405. $this->img = $img->deconstructImages();
  406. $img->destroy(); //销毁零时图片
  407. } else {
  408. //添加水印
  409. $this->img->drawImage($draw);
  410. }
  411. //销毁水印资源
  412. $draw->destroy();
  413. $water->destroy();
  414. }
  415. /**
  416. * 图像添加文字
  417. * @param string $text 添加的文字
  418. * @param string $font 字体路径
  419. * @param integer $size 字号
  420. * @param string $color 文字颜色
  421. * @param integer $locate 文字写入位置
  422. * @param integer $offset 文字相对当前位置的偏移量
  423. * @param integer $angle 文字倾斜角度
  424. */
  425. public function text($text, $font, $size, $color = '#00000000',
  426. $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0) {
  427. //资源检测
  428. if (empty($this->img)) {
  429. E('没有可以被写入文字的图像资源');
  430. }
  431. if (!is_file($font)) {
  432. E("不存在的字体文件:{$font}");
  433. }
  434. //获取颜色和透明度
  435. if (is_array($color)) {
  436. $color = array_map('dechex', $color);
  437. foreach ($color as &$value) {
  438. $value = str_pad($value, 2, '0', STR_PAD_LEFT);
  439. }
  440. $color = '#' . implode('', $color);
  441. } elseif (!is_string($color) || 0 !== strpos($color, '#')) {
  442. E('错误的颜色值');
  443. }
  444. $col = substr($color, 0, 7);
  445. $alp = strlen($color) == 9 ? substr($color, -2) : 0;
  446. //获取文字信息
  447. $draw = new \ImagickDraw();
  448. $draw->setFont(realpath($font));
  449. $draw->setFontSize($size);
  450. $draw->setFillColor($col);
  451. $draw->setFillAlpha(1 - hexdec($alp) / 127);
  452. $draw->setTextAntialias(true);
  453. $draw->setStrokeAntialias(true);
  454. $metrics = $this->img->queryFontMetrics($draw, $text);
  455. /* 计算文字初始坐标和尺寸 */
  456. $x = 0;
  457. $y = $metrics['ascender'];
  458. $w = $metrics['textWidth'];
  459. $h = $metrics['textHeight'];
  460. /* 设定文字位置 */
  461. switch ($locate) {
  462. /* 右下角文字 */
  463. case Image::IMAGE_WATER_SOUTHEAST:
  464. $x += $this->info['width'] - $w;
  465. $y += $this->info['height'] - $h;
  466. break;
  467. /* 左下角文字 */
  468. case Image::IMAGE_WATER_SOUTHWEST:
  469. $y += $this->info['height'] - $h;
  470. break;
  471. /* 左上角文字 */
  472. case Image::IMAGE_WATER_NORTHWEST:
  473. // 起始坐标即为左上角坐标,无需调整
  474. break;
  475. /* 右上角文字 */
  476. case Image::IMAGE_WATER_NORTHEAST:
  477. $x += $this->info['width'] - $w;
  478. break;
  479. /* 居中文字 */
  480. case Image::IMAGE_WATER_CENTER:
  481. $x += ($this->info['width'] - $w) / 2;
  482. $y += ($this->info['height'] - $h) / 2;
  483. break;
  484. /* 下居中文字 */
  485. case Image::IMAGE_WATER_SOUTH:
  486. $x += ($this->info['width'] - $w) / 2;
  487. $y += $this->info['height'] - $h;
  488. break;
  489. /* 右居中文字 */
  490. case Image::IMAGE_WATER_EAST:
  491. $x += $this->info['width'] - $w;
  492. $y += ($this->info['height'] - $h) / 2;
  493. break;
  494. /* 上居中文字 */
  495. case Image::IMAGE_WATER_NORTH:
  496. $x += ($this->info['width'] - $w) / 2;
  497. break;
  498. /* 左居中文字 */
  499. case Image::IMAGE_WATER_WEST:
  500. $y += ($this->info['height'] - $h) / 2;
  501. break;
  502. default:
  503. /* 自定义文字坐标 */
  504. if (is_array($locate)) {
  505. list($posx, $posy) = $locate;
  506. $x += $posx;
  507. $y += $posy;
  508. } else {
  509. E('不支持的文字位置类型');
  510. }
  511. }
  512. /* 设置偏移量 */
  513. if (is_array($offset)) {
  514. $offset = array_map('intval', $offset);
  515. list($ox, $oy) = $offset;
  516. } else {
  517. $offset = intval($offset);
  518. $ox = $oy = $offset;
  519. }
  520. /* 写入文字 */
  521. if ('gif' == $this->info['type']) {
  522. $img = $this->img->coalesceImages();
  523. $this->img->destroy(); //销毁原图
  524. do {
  525. $img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
  526. } while ($img->nextImage());
  527. //压缩图片
  528. $this->img = $img->deconstructImages();
  529. $img->destroy(); //销毁零时图片
  530. } else {
  531. $this->img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
  532. }
  533. $draw->destroy();
  534. }
  535. /**
  536. * 析构方法,用于销毁图像资源
  537. */
  538. public function __destruct()
  539. {
  540. empty($this->img) || $this->img->destroy();
  541. }
  542. }