Gd.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. // | ImageGd.class.php 2013-03-05
  12. // +----------------------------------------------------------------------
  13. namespace Think\Image\Driver;
  14. use Think\Image;
  15. class Gd
  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语句,否则如果是http外网图片时不判断
  43. if (substr($imgname, 0, 4) != 'http' && !is_file($imgname)) {
  44. E('不存在的图像文件');
  45. }
  46. //获取图像信息
  47. $info = getimagesize($imgname);
  48. //检测图像合法性
  49. if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
  50. E('非法图像文件');
  51. }
  52. //设置图像信息
  53. $this->info = array(
  54. 'width' => $info[0],
  55. 'height' => $info[1],
  56. 'type' => image_type_to_extension($info[2], false),
  57. 'mime' => $info['mime'],
  58. );
  59. //销毁已存在的图像
  60. empty($this->img) || imagedestroy($this->img);
  61. //打开图像
  62. if ('gif' == $this->info['type']) {
  63. $class = 'Think\\Image\\Driver\\GIF';
  64. $this->gif = new $class($imgname);
  65. $this->img = imagecreatefromstring($this->gif->image());
  66. } else {
  67. $fun = "imagecreatefrom{$this->info['type']}";
  68. $this->img = $fun($imgname);
  69. }
  70. }
  71. /**
  72. * 保存图像
  73. * @param string $imgname 图像保存名称
  74. * @param string $type 图像类型
  75. * @param integer $quality 图像质量
  76. * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
  77. */
  78. public function save($imgname, $type = null, $quality = 80, $interlace = true)
  79. {
  80. if (empty($this->img)) {
  81. E('没有可以被保存的图像资源');
  82. }
  83. //自动获取图像类型
  84. if (is_null($type)) {
  85. $type = $this->info['type'];
  86. } else {
  87. $type = strtolower($type);
  88. }
  89. //保存图像
  90. if ('jpeg' == $type || 'jpg' == $type) {
  91. //JPEG图像设置隔行扫描
  92. imageinterlace($this->img, $interlace);
  93. imagejpeg($this->img, $imgname, $quality);
  94. } elseif ('gif' == $type && !empty($this->gif)) {
  95. $this->gif->save($imgname);
  96. } elseif ('png' == $type) {
  97. //设定保存完整的 alpha 通道信息
  98. imagesavealpha($this->img, true);
  99. //ImagePNG生成图像的质量范围从0到9的
  100. imagepng($this->img, $imgname, $quality / 10);
  101. } else {
  102. $fun = 'image' . $type;
  103. $fun($this->img, $imgname);
  104. }
  105. }
  106. /**
  107. * 返回图像宽度
  108. * @return integer 图像宽度
  109. */
  110. public function width()
  111. {
  112. if (empty($this->img)) {
  113. E('没有指定图像资源');
  114. }
  115. return $this->info['width'];
  116. }
  117. /**
  118. * 返回图像高度
  119. * @return integer 图像高度
  120. */
  121. public function height()
  122. {
  123. if (empty($this->img)) {
  124. E('没有指定图像资源');
  125. }
  126. return $this->info['height'];
  127. }
  128. /**
  129. * 返回图像类型
  130. * @return string 图像类型
  131. */
  132. public function type()
  133. {
  134. if (empty($this->img)) {
  135. E('没有指定图像资源');
  136. }
  137. return $this->info['type'];
  138. }
  139. /**
  140. * 返回图像MIME类型
  141. * @return string 图像MIME类型
  142. */
  143. public function mime()
  144. {
  145. if (empty($this->img)) {
  146. E('没有指定图像资源');
  147. }
  148. return $this->info['mime'];
  149. }
  150. /**
  151. * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
  152. * @return array 图像尺寸
  153. */
  154. public function size()
  155. {
  156. if (empty($this->img)) {
  157. E('没有指定图像资源');
  158. }
  159. return array($this->info['width'], $this->info['height']);
  160. }
  161. /**
  162. * 裁剪图像
  163. * @param integer $w 裁剪区域宽度
  164. * @param integer $h 裁剪区域高度
  165. * @param integer $x 裁剪区域x坐标
  166. * @param integer $y 裁剪区域y坐标
  167. * @param integer $width 图像保存宽度
  168. * @param integer $height 图像保存高度
  169. */
  170. public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
  171. {
  172. if (empty($this->img)) {
  173. E('没有可以被裁剪的图像资源');
  174. }
  175. //设置保存尺寸
  176. empty($width) && $width = $w;
  177. empty($height) && $height = $h;
  178. do {
  179. //创建新图像
  180. $img = imagecreatetruecolor($width, $height);
  181. // 调整默认颜色
  182. $color = imagecolorallocate($img, 255, 255, 255);
  183. imagefill($img, 0, 0, $color);
  184. //取消默认的混色模式(优化原来生成的png图片为非透明的BUG)
  185. if ('png' == $this->info['type']) {
  186. imagealphablending($img, false);
  187. }
  188. //裁剪
  189. imagecopyresampled($img, $this->img, 0, 0, $x, $y, $width, $height, $w, $h);
  190. imagedestroy($this->img); //销毁原图
  191. //设置新图像
  192. $this->img = $img;
  193. } while (!empty($this->gif) && $this->gifNext());
  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. do {
  269. //创建新图像
  270. $img = imagecreatetruecolor($width, $height);
  271. // 调整默认颜色
  272. $color = imagecolorallocate($img, 255, 255, 255);
  273. imagefill($img, 0, 0, $color);
  274. //裁剪
  275. imagecopyresampled($img, $this->img, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
  276. imagedestroy($this->img); //销毁原图
  277. $this->img = $img;
  278. } while (!empty($this->gif) && $this->gifNext());
  279. $this->info['width'] = $width;
  280. $this->info['height'] = $height;
  281. return;
  282. /* 固定 */
  283. case Image::IMAGE_THUMB_FIXED:
  284. $x = $y = 0;
  285. break;
  286. default:
  287. E('不支持的缩略图裁剪类型');
  288. }
  289. /* 裁剪图像 */
  290. $this->crop($w, $h, $x, $y, $width, $height);
  291. }
  292. /**
  293. * 添加水印
  294. * @param string $source 水印图片路径
  295. * @param integer $locate 水印位置
  296. * @param integer $alpha 水印透明度
  297. */
  298. public function water($source, $locate = Image::IMAGE_WATER_SOUTHEAST, $alpha = 80)
  299. {
  300. //资源检测
  301. if (empty($this->img)) {
  302. E('没有可以被添加水印的图像资源');
  303. }
  304. if (!is_file($source)) {
  305. E('水印图像不存在');
  306. }
  307. //获取水印图像信息
  308. $info = getimagesize($source);
  309. if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
  310. E('非法水印文件');
  311. }
  312. //创建水印图像资源
  313. $fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  314. $water = $fun($source);
  315. //设定水印图像的混色模式
  316. imagealphablending($water, true);
  317. /* 设定水印位置 */
  318. switch ($locate) {
  319. /* 右下角水印 */
  320. case Image::IMAGE_WATER_SOUTHEAST:
  321. $x = $this->info['width'] - $info[0];
  322. $y = $this->info['height'] - $info[1];
  323. break;
  324. /* 左下角水印 */
  325. case Image::IMAGE_WATER_SOUTHWEST:
  326. $x = 0;
  327. $y = $this->info['height'] - $info[1];
  328. break;
  329. /* 左上角水印 */
  330. case Image::IMAGE_WATER_NORTHWEST:
  331. $x = $y = 0;
  332. break;
  333. /* 右上角水印 */
  334. case Image::IMAGE_WATER_NORTHEAST:
  335. $x = $this->info['width'] - $info[0];
  336. $y = 0;
  337. break;
  338. /* 居中水印 */
  339. case Image::IMAGE_WATER_CENTER:
  340. $x = ($this->info['width'] - $info[0]) / 2;
  341. $y = ($this->info['height'] - $info[1]) / 2;
  342. break;
  343. /* 下居中水印 */
  344. case Image::IMAGE_WATER_SOUTH:
  345. $x = ($this->info['width'] - $info[0]) / 2;
  346. $y = $this->info['height'] - $info[1];
  347. break;
  348. /* 右居中水印 */
  349. case Image::IMAGE_WATER_EAST:
  350. $x = $this->info['width'] - $info[0];
  351. $y = ($this->info['height'] - $info[1]) / 2;
  352. break;
  353. /* 上居中水印 */
  354. case Image::IMAGE_WATER_NORTH:
  355. $x = ($this->info['width'] - $info[0]) / 2;
  356. $y = 0;
  357. break;
  358. /* 左居中水印 */
  359. case Image::IMAGE_WATER_WEST:
  360. $x = 0;
  361. $y = ($this->info['height'] - $info[1]) / 2;
  362. break;
  363. default:
  364. /* 自定义水印坐标 */
  365. if (is_array($locate)) {
  366. list($x, $y) = $locate;
  367. } else {
  368. E('不支持的水印位置类型');
  369. }
  370. }
  371. do {
  372. //添加水印
  373. $src = imagecreatetruecolor($info[0], $info[1]);
  374. // 调整默认颜色
  375. $color = imagecolorallocate($src, 255, 255, 255);
  376. imagefill($src, 0, 0, $color);
  377. imagecopy($src, $this->img, 0, 0, $x, $y, $info[0], $info[1]);
  378. imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
  379. imagecopymerge($this->img, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
  380. //销毁零时图片资源
  381. imagedestroy($src);
  382. } while (!empty($this->gif) && $this->gifNext());
  383. //销毁水印资源
  384. imagedestroy($water);
  385. }
  386. /**
  387. * 图像添加文字
  388. * @param string $text 添加的文字
  389. * @param string $font 字体路径
  390. * @param integer $size 字号
  391. * @param string $color 文字颜色
  392. * @param integer $locate 文字写入位置
  393. * @param integer $offset 文字相对当前位置的偏移量
  394. * @param integer $angle 文字倾斜角度
  395. */
  396. public function text($text, $font, $size, $color = '#00000000',
  397. $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0) {
  398. //资源检测
  399. if (empty($this->img)) {
  400. E('没有可以被写入文字的图像资源');
  401. }
  402. if (!is_file($font)) {
  403. E("不存在的字体文件:{$font}");
  404. }
  405. //获取文字信息
  406. $info = imagettfbbox($size, $angle, $font, $text);
  407. $minx = min($info[0], $info[2], $info[4], $info[6]);
  408. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  409. $miny = min($info[1], $info[3], $info[5], $info[7]);
  410. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  411. /* 计算文字初始坐标和尺寸 */
  412. $x = $minx;
  413. $y = abs($miny);
  414. $w = $maxx - $minx;
  415. $h = $maxy - $miny;
  416. /* 设定文字位置 */
  417. switch ($locate) {
  418. /* 右下角文字 */
  419. case Image::IMAGE_WATER_SOUTHEAST:
  420. $x += $this->info['width'] - $w;
  421. $y += $this->info['height'] - $h;
  422. break;
  423. /* 左下角文字 */
  424. case Image::IMAGE_WATER_SOUTHWEST:
  425. $y += $this->info['height'] - $h;
  426. break;
  427. /* 左上角文字 */
  428. case Image::IMAGE_WATER_NORTHWEST:
  429. // 起始坐标即为左上角坐标,无需调整
  430. break;
  431. /* 右上角文字 */
  432. case Image::IMAGE_WATER_NORTHEAST:
  433. $x += $this->info['width'] - $w;
  434. break;
  435. /* 居中文字 */
  436. case Image::IMAGE_WATER_CENTER:
  437. $x += ($this->info['width'] - $w) / 2;
  438. $y += ($this->info['height'] - $h) / 2;
  439. break;
  440. /* 下居中文字 */
  441. case Image::IMAGE_WATER_SOUTH:
  442. $x += ($this->info['width'] - $w) / 2;
  443. $y += $this->info['height'] - $h;
  444. break;
  445. /* 右居中文字 */
  446. case Image::IMAGE_WATER_EAST:
  447. $x += $this->info['width'] - $w;
  448. $y += ($this->info['height'] - $h) / 2;
  449. break;
  450. /* 上居中文字 */
  451. case Image::IMAGE_WATER_NORTH:
  452. $x += ($this->info['width'] - $w) / 2;
  453. break;
  454. /* 左居中文字 */
  455. case Image::IMAGE_WATER_WEST:
  456. $y += ($this->info['height'] - $h) / 2;
  457. break;
  458. default:
  459. /* 自定义文字坐标 */
  460. if (is_array($locate)) {
  461. list($posx, $posy) = $locate;
  462. $x += $posx;
  463. $y += $posy;
  464. } else {
  465. E('不支持的文字位置类型');
  466. }
  467. }
  468. /* 设置偏移量 */
  469. if (is_array($offset)) {
  470. $offset = array_map('intval', $offset);
  471. list($ox, $oy) = $offset;
  472. } else {
  473. $offset = intval($offset);
  474. $ox = $oy = $offset;
  475. }
  476. /* 设置颜色 */
  477. if (is_string($color) && 0 === strpos($color, '#')) {
  478. $color = str_split(substr($color, 1), 2);
  479. $color = array_map('hexdec', $color);
  480. if (empty($color[3]) || $color[3] > 127) {
  481. $color[3] = 0;
  482. }
  483. } elseif (!is_array($color)) {
  484. E('错误的颜色值');
  485. }
  486. do {
  487. /* 写入文字 */
  488. $col = imagecolorallocatealpha($this->img, $color[0], $color[1], $color[2], $color[3]);
  489. imagettftext($this->img, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text);
  490. } while (!empty($this->gif) && $this->gifNext());
  491. }
  492. /* 切换到GIF的下一帧并保存当前帧,内部使用 */
  493. private function gifNext()
  494. {
  495. ob_start();
  496. ob_implicit_flush(0);
  497. imagegif($this->img);
  498. $img = ob_get_clean();
  499. $this->gif->image($img);
  500. $next = $this->gif->nextImage();
  501. if ($next) {
  502. $this->img = imagecreatefromstring($next);
  503. return $next;
  504. } else {
  505. $this->img = imagecreatefromstring($this->gif->image());
  506. return false;
  507. }
  508. }
  509. /**
  510. * 析构方法,用于销毁图像资源
  511. */
  512. public function __destruct()
  513. {
  514. empty($this->img) || imagedestroy($this->img);
  515. }
  516. }