Image.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace app\common\library;
  3. use app\common\library\gif\Gif;
  4. class Image
  5. {
  6. /* 缩略图相关常量定义 */
  7. const THUMB_SCALING = 1; //常量,标识缩略图等比例缩放类型
  8. const THUMB_FILLED = 2; //常量,标识缩略图缩放后填充类型
  9. const THUMB_CENTER = 3; //常量,标识缩略图居中裁剪类型
  10. const THUMB_NORTHWEST = 4; //常量,标识缩略图左上角裁剪类型
  11. const THUMB_SOUTHEAST = 5; //常量,标识缩略图右下角裁剪类型
  12. const THUMB_FIXED = 6; //常量,标识缩略图固定尺寸缩放类型
  13. /* 水印相关常量定义 */
  14. const WATER_NORTHWEST = 1; //常量,标识左上角水印
  15. const WATER_NORTH = 2; //常量,标识上居中水印
  16. const WATER_NORTHEAST = 3; //常量,标识右上角水印
  17. const WATER_WEST = 4; //常量,标识左居中水印
  18. const WATER_CENTER = 5; //常量,标识居中水印
  19. const WATER_EAST = 6; //常量,标识右居中水印
  20. const WATER_SOUTHWEST = 7; //常量,标识左下角水印
  21. const WATER_SOUTH = 8; //常量,标识下居中水印
  22. const WATER_SOUTHEAST = 9; //常量,标识右下角水印
  23. /* 翻转相关常量定义 */
  24. const FLIP_X = 1; //X轴翻转
  25. const FLIP_Y = 2; //Y轴翻转
  26. /**
  27. * 图像资源对象
  28. *
  29. * @var resource
  30. */
  31. protected $im;
  32. /** @var Gif */
  33. protected $gif;
  34. /**
  35. * 图像信息,包括 width, height, type, mime, size
  36. *
  37. * @var array
  38. */
  39. protected $info;
  40. protected function __construct(\SplFileInfo $file)
  41. {
  42. //获取图像信息
  43. $info = @getimagesize($file->getPathname());
  44. //检测图像合法性
  45. if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
  46. throw new \RuntimeException('Illegal image file');
  47. }
  48. //设置图像信息
  49. $this->info = [
  50. 'width' => $info[0],
  51. 'height' => $info[1],
  52. 'type' => image_type_to_extension($info[2], false),
  53. 'mime' => $info['mime'],
  54. ];
  55. //打开图像
  56. if ('gif' == $this->info['type']) {
  57. $this->gif = new Gif($file->getPathname());
  58. $this->im = @imagecreatefromstring($this->gif->image());
  59. } else {
  60. $fun = "imagecreatefrom{$this->info['type']}";
  61. $this->im = @$fun($file->getPathname());
  62. }
  63. if (empty($this->im)) {
  64. throw new \RuntimeException('Failed to create image resources!');
  65. }
  66. }
  67. /**
  68. * 打开一个图片文件
  69. * @param \SplFileInfo|string $file
  70. * @return Image
  71. */
  72. public static function open($file)
  73. {
  74. if (is_string($file)) {
  75. $file = new \SplFileInfo($file);
  76. }
  77. if (!$file->isFile()) {
  78. throw new \RuntimeException('image file not exist');
  79. }
  80. return new self($file);
  81. }
  82. /**
  83. * 保存图像
  84. * @param string $pathname 图像保存路径名称
  85. * @param null|string $type 图像类型
  86. * @param int $quality 图像质量
  87. * @param bool $interlace 是否对JPEG类型图像设置隔行扫描
  88. * @return $this
  89. */
  90. public function save($pathname, $type = null, $quality = 80, $interlace = true)
  91. {
  92. //自动获取图像类型
  93. if (is_null($type)) {
  94. $type = $this->info['type'];
  95. } else {
  96. $type = strtolower($type);
  97. }
  98. //保存图像
  99. if ('jpeg' == $type || 'jpg' == $type) {
  100. //JPEG图像设置隔行扫描
  101. imageinterlace($this->im, $interlace);
  102. imagejpeg($this->im, $pathname, $quality);
  103. } elseif ('gif' == $type && !empty($this->gif)) {
  104. $this->gif->save($pathname);
  105. } elseif ('png' == $type) {
  106. //设定保存完整的 alpha 通道信息
  107. imagesavealpha($this->im, true);
  108. //ImagePNG生成图像的质量范围从0到9的
  109. imagepng($this->im, $pathname, min((int) ($quality / 10), 9));
  110. } else {
  111. $fun = 'image' . $type;
  112. $fun($this->im, $pathname);
  113. }
  114. return $this;
  115. }
  116. /**
  117. * 返回图像宽度
  118. * @return int 图像宽度
  119. */
  120. public function width()
  121. {
  122. return $this->info['width'];
  123. }
  124. /**
  125. * 返回图像高度
  126. * @return int 图像高度
  127. */
  128. public function height()
  129. {
  130. return $this->info['height'];
  131. }
  132. /**
  133. * 返回图像类型
  134. * @return string 图像类型
  135. */
  136. public function type()
  137. {
  138. return $this->info['type'];
  139. }
  140. /**
  141. * 返回图像MIME类型
  142. * @return string 图像MIME类型
  143. */
  144. public function mime()
  145. {
  146. return $this->info['mime'];
  147. }
  148. /**
  149. * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
  150. * @return array 图像尺寸
  151. */
  152. public function size()
  153. {
  154. return [$this->info['width'], $this->info['height']];
  155. }
  156. /**
  157. * 旋转图像
  158. * @param int $degrees 顺时针旋转的度数
  159. * @return $this
  160. */
  161. public function rotate($degrees = 90)
  162. {
  163. do {
  164. $img = imagerotate($this->im, -$degrees, imagecolorallocatealpha($this->im, 0, 0, 0, 127));
  165. imagedestroy($this->im);
  166. $this->im = $img;
  167. } while (!empty($this->gif) && $this->gifNext());
  168. $this->info['width'] = imagesx($this->im);
  169. $this->info['height'] = imagesy($this->im);
  170. return $this;
  171. }
  172. /**
  173. * 翻转图像
  174. * @param integer $direction 翻转轴,X或者Y
  175. * @return $this
  176. */
  177. public function flip($direction = self::FLIP_X)
  178. {
  179. //原图宽度和高度
  180. $w = $this->info['width'];
  181. $h = $this->info['height'];
  182. do {
  183. $img = imagecreatetruecolor($w, $h);
  184. switch ($direction) {
  185. case self::FLIP_X:
  186. for ($y = 0; $y < $h; $y++) {
  187. imagecopy($img, $this->im, 0, $h - $y - 1, 0, $y, $w, 1);
  188. }
  189. break;
  190. case self::FLIP_Y:
  191. for ($x = 0; $x < $w; $x++) {
  192. imagecopy($img, $this->im, $w - $x - 1, 0, $x, 0, 1, $h);
  193. }
  194. break;
  195. default:
  196. throw new \RuntimeException('不支持的翻转类型');
  197. }
  198. imagedestroy($this->im);
  199. $this->im = $img;
  200. } while (!empty($this->gif) && $this->gifNext());
  201. return $this;
  202. }
  203. /**
  204. * 裁剪图像
  205. *
  206. * @param integer $w 裁剪区域宽度
  207. * @param integer $h 裁剪区域高度
  208. * @param integer $x 裁剪区域x坐标
  209. * @param integer $y 裁剪区域y坐标
  210. * @param integer $width 图像保存宽度
  211. * @param integer $height 图像保存高度
  212. *
  213. * @return $this
  214. */
  215. public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
  216. {
  217. //设置保存尺寸
  218. empty($width) && $width = $w;
  219. empty($height) && $height = $h;
  220. do {
  221. //创建新图像
  222. $img = imagecreatetruecolor($width, $height);
  223. // 调整默认颜色
  224. $color = imagecolorallocate($img, 255, 255, 255);
  225. imagefill($img, 0, 0, $color);
  226. //裁剪
  227. imagecopyresampled($img, $this->im, 0, 0, (int)$x, (int)$y, (int)$width, (int)$height, (int)$w, (int)$h);
  228. imagedestroy($this->im); //销毁原图
  229. //设置新图像
  230. $this->im = $img;
  231. } while (!empty($this->gif) && $this->gifNext());
  232. $this->info['width'] = (int) $width;
  233. $this->info['height'] = (int) $height;
  234. return $this;
  235. }
  236. /**
  237. * 生成缩略图
  238. *
  239. * @param integer $width 缩略图最大宽度
  240. * @param integer $height 缩略图最大高度
  241. * @param int $type 缩略图裁剪类型
  242. *
  243. * @return $this
  244. */
  245. public function thumb($width, $height, $type = self::THUMB_SCALING)
  246. {
  247. //原图宽度和高度
  248. $w = $this->info['width'];
  249. $h = $this->info['height'];
  250. /* 计算缩略图生成的必要参数 */
  251. switch ($type) {
  252. /* 等比例缩放 */
  253. case self::THUMB_SCALING:
  254. //原图尺寸小于缩略图尺寸则不进行缩略
  255. if ($w < $width && $h < $height) {
  256. return $this;
  257. }
  258. //计算缩放比例
  259. $scale = min($width / $w, $height / $h);
  260. //设置缩略图的坐标及宽度和高度
  261. $x = $y = 0;
  262. $width = $w * $scale;
  263. $height = $h * $scale;
  264. break;
  265. /* 居中裁剪 */
  266. case self::THUMB_CENTER:
  267. //计算缩放比例
  268. $scale = max($width / $w, $height / $h);
  269. //设置缩略图的坐标及宽度和高度
  270. $w = $width / $scale;
  271. $h = $height / $scale;
  272. $x = ($this->info['width'] - $w) / 2;
  273. $y = ($this->info['height'] - $h) / 2;
  274. break;
  275. /* 左上角裁剪 */
  276. case self::THUMB_NORTHWEST:
  277. //计算缩放比例
  278. $scale = max($width / $w, $height / $h);
  279. //设置缩略图的坐标及宽度和高度
  280. $x = $y = 0;
  281. $w = $width / $scale;
  282. $h = $height / $scale;
  283. break;
  284. /* 右下角裁剪 */
  285. case self::THUMB_SOUTHEAST:
  286. //计算缩放比例
  287. $scale = max($width / $w, $height / $h);
  288. //设置缩略图的坐标及宽度和高度
  289. $w = $width / $scale;
  290. $h = $height / $scale;
  291. $x = $this->info['width'] - $w;
  292. $y = $this->info['height'] - $h;
  293. break;
  294. /* 填充 */
  295. case self::THUMB_FILLED:
  296. //计算缩放比例
  297. if ($w < $width && $h < $height) {
  298. $scale = 1;
  299. } else {
  300. $scale = min($width / $w, $height / $h);
  301. }
  302. //设置缩略图的坐标及宽度和高度
  303. $neww = $w * $scale;
  304. $newh = $h * $scale;
  305. $x = $this->info['width'] - $w;
  306. $y = $this->info['height'] - $h;
  307. $posx = ($width - $w * $scale) / 2;
  308. $posy = ($height - $h * $scale) / 2;
  309. do {
  310. //创建新图像
  311. $img = imagecreatetruecolor($width, $height);
  312. // 调整默认颜色
  313. $color = imagecolorallocate($img, 255, 255, 255);
  314. imagefill($img, 0, 0, $color);
  315. //裁剪
  316. imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
  317. imagedestroy($this->im); //销毁原图
  318. $this->im = $img;
  319. } while (!empty($this->gif) && $this->gifNext());
  320. $this->info['width'] = (int) $width;
  321. $this->info['height'] = (int) $height;
  322. return $this;
  323. /* 固定 */
  324. case self::THUMB_FIXED:
  325. $x = $y = 0;
  326. break;
  327. default:
  328. throw new \RuntimeException('不支持的缩略图裁剪类型');
  329. }
  330. /* 裁剪图像 */
  331. return $this->crop($w, $h, $x, $y, $width, $height);
  332. }
  333. /**
  334. * 添加水印
  335. *
  336. * @param string $source 水印图片路径
  337. * @param int $locate 水印位置
  338. * @param int $alpha 透明度
  339. * @param int $scale 水印缩放比例
  340. * @return $this
  341. */
  342. public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100,$scale=1)
  343. {
  344. if (!is_file($source)) {
  345. throw new \RuntimeException('水印图像不存在');
  346. }
  347. //获取水印图像信息
  348. $info = getimagesize($source);
  349. $water_width=intval($info[0]*$scale);
  350. $water_height=intval($info[1]*$scale);
  351. //设置水印宽高
  352. if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
  353. throw new \RuntimeException('非法水印文件');
  354. }
  355. //创建水印图像资源
  356. $fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  357. $water = $fun($source);
  358. //设定水印图像的混色模式
  359. imagealphablending($water, true);
  360. /* 设定水印位置 */
  361. switch ($locate) {
  362. /* 右下角水印 */
  363. case self::WATER_SOUTHEAST:
  364. $x = $this->info['width'] - $water_width;
  365. $y = $this->info['height'] - $water_height;
  366. break;
  367. /* 左下角水印 */
  368. case self::WATER_SOUTHWEST:
  369. $x = 0;
  370. $y = $this->info['height'] - $water_height;
  371. break;
  372. /* 左上角水印 */
  373. case self::WATER_NORTHWEST:
  374. $x = $y = 0;
  375. break;
  376. /* 右上角水印 */
  377. case self::WATER_NORTHEAST:
  378. $x = $this->info['width'] - $water_width;
  379. $y = 0;
  380. break;
  381. /* 居中水印 */
  382. case self::WATER_CENTER:
  383. $x = ($this->info['width'] - $water_width) / 2;
  384. $y = ($this->info['height'] - $water_height) / 2;
  385. break;
  386. /* 下居中水印 */
  387. case self::WATER_SOUTH:
  388. $x = ($this->info['width'] - $water_width) / 2;
  389. $y = $this->info['height'] - $water_height;
  390. break;
  391. /* 右居中水印 */
  392. case self::WATER_EAST:
  393. $x = $this->info['width'] - $water_width;
  394. $y = ($this->info['height'] - $water_height) / 2;
  395. break;
  396. /* 上居中水印 */
  397. case self::WATER_NORTH:
  398. $x = ($this->info['width'] - $water_width) / 2;
  399. $y = 0;
  400. break;
  401. /* 左居中水印 */
  402. case self::WATER_WEST:
  403. $x = 0;
  404. $y = ($this->info['height'] - $water_height) / 2;
  405. break;
  406. default:
  407. /* 自定义水印坐标 */
  408. if (is_array($locate)) {
  409. list($x, $y) = $locate;
  410. } else {
  411. throw new \RuntimeException('不支持的水印位置类型');
  412. }
  413. }
  414. do {
  415. $src = imagecreatetruecolor($water_width, $water_height);
  416. // 调整默认颜色
  417. $color = imagecolorallocate($src, 255, 255, 255);
  418. imagefill($src, 0, 0, $color);
  419. imagecopyresampled(
  420. $src,
  421. $water,
  422. 0, 0, 0, 0,
  423. $water_width, $water_height,
  424. $info[0], $info[1]
  425. );
  426. imagecopymerge($this->im, $src, intval($x), intval($y), 0, 0, $water_width, $water_height, $alpha);
  427. //销毁零时图片资源
  428. imagedestroy($src);
  429. } while (!empty($this->gif) && $this->gifNext());
  430. //销毁水印资源
  431. imagedestroy($water);
  432. return $this;
  433. }
  434. /**
  435. * 图像添加文字
  436. *
  437. * @param string $text 添加的文字
  438. * @param string $font 字体路径
  439. * @param integer $size 字号
  440. * @param string $color 文字颜色
  441. * @param int $locate 文字写入位置
  442. * @param integer $offset 文字相对当前位置的偏移量
  443. * @param integer $angle 文字倾斜角度
  444. *
  445. * @return $this
  446. * @throws \RuntimeException
  447. */
  448. public function text($text, $font, $size, $color = '#00000000',
  449. $locate = self::WATER_SOUTHEAST, $offset = 0, $angle = 0) {
  450. if (!is_file($font)) {
  451. throw new \RuntimeException("不存在的字体文件:{$font}");
  452. }
  453. //获取文字信息
  454. $info = imagettfbbox($size, $angle, $font, $text);
  455. $minx = min($info[0], $info[2], $info[4], $info[6]);
  456. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  457. $miny = min($info[1], $info[3], $info[5], $info[7]);
  458. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  459. /* 计算文字初始坐标和尺寸 */
  460. $x = $minx;
  461. $y = abs($miny);
  462. $w = $maxx - $minx;
  463. $h = $maxy - $miny;
  464. /* 设定文字位置 */
  465. switch ($locate) {
  466. /* 右下角文字 */
  467. case self::WATER_SOUTHEAST:
  468. $x += $this->info['width'] - $w;
  469. $y += $this->info['height'] - $h;
  470. break;
  471. /* 左下角文字 */
  472. case self::WATER_SOUTHWEST:
  473. $y += $this->info['height'] - $h;
  474. break;
  475. /* 左上角文字 */
  476. case self::WATER_NORTHWEST:
  477. // 起始坐标即为左上角坐标,无需调整
  478. break;
  479. /* 右上角文字 */
  480. case self::WATER_NORTHEAST:
  481. $x += $this->info['width'] - $w;
  482. break;
  483. /* 居中文字 */
  484. case self::WATER_CENTER:
  485. $x += ($this->info['width'] - $w) / 2;
  486. $y += ($this->info['height'] - $h) / 2;
  487. break;
  488. /* 下居中文字 */
  489. case self::WATER_SOUTH:
  490. $x += ($this->info['width'] - $w) / 2;
  491. $y += $this->info['height'] - $h;
  492. break;
  493. /* 右居中文字 */
  494. case self::WATER_EAST:
  495. $x += $this->info['width'] - $w;
  496. $y += ($this->info['height'] - $h) / 2;
  497. break;
  498. /* 上居中文字 */
  499. case self::WATER_NORTH:
  500. $x += ($this->info['width'] - $w) / 2;
  501. break;
  502. /* 左居中文字 */
  503. case self::WATER_WEST:
  504. $y += ($this->info['height'] - $h) / 2;
  505. break;
  506. default:
  507. /* 自定义文字坐标 */
  508. if (is_array($locate)) {
  509. list($posx, $posy) = $locate;
  510. $x += $posx;
  511. $y += $posy;
  512. } else {
  513. throw new \RuntimeException('不支持的文字位置类型');
  514. }
  515. }
  516. /* 设置偏移量 */
  517. if (is_array($offset)) {
  518. $offset = array_map('intval', $offset);
  519. list($ox, $oy) = $offset;
  520. } else {
  521. $offset = intval($offset);
  522. $ox = $oy = $offset;
  523. }
  524. /* 设置颜色 */
  525. if (is_string($color) && 0 === strpos($color, '#')) {
  526. $color = str_split(substr($color, 1), 2);
  527. $color = array_map('hexdec', $color);
  528. if (empty($color[3]) || $color[3] > 127) {
  529. $color[3] = 0;
  530. }
  531. } elseif (!is_array($color)) {
  532. throw new \RuntimeException('错误的颜色值');
  533. }
  534. do {
  535. /* 写入文字 */
  536. $col = imagecolorallocatealpha($this->im, $color[0], $color[1], $color[2], $color[3]);
  537. imagettftext($this->im, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text);
  538. } while (!empty($this->gif) && $this->gifNext());
  539. return $this;
  540. }
  541. /**
  542. * 切换到GIF的下一帧并保存当前帧
  543. */
  544. protected function gifNext()
  545. {
  546. ob_start();
  547. ob_implicit_flush(0);
  548. imagegif($this->im);
  549. $img = ob_get_clean();
  550. $this->gif->image($img);
  551. $next = $this->gif->nextImage();
  552. if ($next) {
  553. imagedestroy($this->im);
  554. $this->im = imagecreatefromstring($next);
  555. return $next;
  556. } else {
  557. imagedestroy($this->im);
  558. $this->im = imagecreatefromstring($this->gif->image());
  559. return false;
  560. }
  561. }
  562. /**
  563. * 析构方法,用于销毁图像资源
  564. */
  565. public function __destruct()
  566. {
  567. empty($this->im) || imagedestroy($this->im);
  568. }
  569. }