Gd.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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> <https://www.nanodreamtech.com>
  10. // +----------------------------------------------------------------------
  11. // | ImageGd.class.php 2013-03-05
  12. // +----------------------------------------------------------------------
  13. namespace Think\Image\Driver;
  14. use Think\Image;
  15. class Gd{
  16. /**
  17. * 图像资源对象
  18. * @var resource
  19. */
  20. private $img;
  21. /**
  22. * 图像信息,包括width,height,type,mime,size
  23. * @var array
  24. */
  25. private $info;
  26. /**
  27. * 构造方法,可用于打开一张图像
  28. * @param string $imgname 图像路径
  29. */
  30. public function __construct($imgname = null) {
  31. $imgname && $this->open($imgname);
  32. }
  33. /**
  34. * 打开一张图像
  35. * @param string $imgname 图像路径
  36. */
  37. public function open($imgname){
  38. //检测图像文件
  39. if(!is_file($imgname)) E('不存在的图像文件');
  40. //获取图像信息
  41. $info = getimagesize($imgname);
  42. //检测图像合法性
  43. if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
  44. E('非法图像文件');
  45. }
  46. //设置图像信息
  47. $this->info = array(
  48. 'width' => $info[0],
  49. 'height' => $info[1],
  50. 'type' => image_type_to_extension($info[2], false),
  51. 'mime' => $info['mime'],
  52. );
  53. //销毁已存在的图像
  54. empty($this->img) || imagedestroy($this->img);
  55. //打开图像
  56. if('gif' == $this->info['type']){
  57. $class = 'Think\\Image\\Driver\\GIF';
  58. $this->gif = new $class($imgname);
  59. $this->img = imagecreatefromstring($this->gif->image());
  60. } else {
  61. $fun = "imagecreatefrom{$this->info['type']}";
  62. $this->img = $fun($imgname);
  63. }
  64. }
  65. /**
  66. * 保存图像
  67. * @param string $imgname 图像保存名称
  68. * @param string $type 图像类型
  69. * @param integer $quality 图像质量
  70. * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
  71. */
  72. public function save($imgname, $type = null, $quality=80,$interlace = true){
  73. if(empty($this->img)) E('没有可以被保存的图像资源');
  74. //自动获取图像类型
  75. if(is_null($type)){
  76. $type = $this->info['type'];
  77. } else {
  78. $type = strtolower($type);
  79. }
  80. //保存图像
  81. if('jpeg' == $type || 'jpg' == $type){
  82. //JPEG图像设置隔行扫描
  83. imageinterlace($this->img, $interlace);
  84. imagejpeg($this->img, $imgname,$quality);
  85. }elseif('gif' == $type && !empty($this->gif)){
  86. $this->gif->save($imgname);
  87. }else{
  88. $fun = 'image'.$type;
  89. $fun($this->img, $imgname);
  90. }
  91. }
  92. /**
  93. * 返回图像宽度
  94. * @return integer 图像宽度
  95. */
  96. public function width(){
  97. if(empty($this->img)) E('没有指定图像资源');
  98. return $this->info['width'];
  99. }
  100. /**
  101. * 返回图像高度
  102. * @return integer 图像高度
  103. */
  104. public function height(){
  105. if(empty($this->img)) E('没有指定图像资源');
  106. return $this->info['height'];
  107. }
  108. /**
  109. * 返回图像类型
  110. * @return string 图像类型
  111. */
  112. public function type(){
  113. if(empty($this->img)) E('没有指定图像资源');
  114. return $this->info['type'];
  115. }
  116. /**
  117. * 返回图像MIME类型
  118. * @return string 图像MIME类型
  119. */
  120. public function mime(){
  121. if(empty($this->img)) E('没有指定图像资源');
  122. return $this->info['mime'];
  123. }
  124. /**
  125. * 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
  126. * @return array 图像尺寸
  127. */
  128. public function size(){
  129. if(empty($this->img)) E('没有指定图像资源');
  130. return array($this->info['width'], $this->info['height']);
  131. }
  132. /**
  133. * 裁剪图像
  134. * @param integer $w 裁剪区域宽度
  135. * @param integer $h 裁剪区域高度
  136. * @param integer $x 裁剪区域x坐标
  137. * @param integer $y 裁剪区域y坐标
  138. * @param integer $width 图像保存宽度
  139. * @param integer $height 图像保存高度
  140. */
  141. public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
  142. if(empty($this->img)) E('没有可以被裁剪的图像资源');
  143. //设置保存尺寸
  144. empty($width) && $width = $w;
  145. empty($height) && $height = $h;
  146. do {
  147. //创建新图像
  148. $img = imagecreatetruecolor($width, $height);
  149. // 调整默认颜色
  150. $color = imagecolorallocate($img, 255, 255, 255);
  151. imagefill($img, 0, 0, $color);
  152. //裁剪
  153. imagecopyresampled($img, $this->img, 0, 0, $x, $y, $width, $height, $w, $h);
  154. imagedestroy($this->img); //销毁原图
  155. //设置新图像
  156. $this->img = $img;
  157. } while(!empty($this->gif) && $this->gifNext());
  158. $this->info['width'] = $width;
  159. $this->info['height'] = $height;
  160. }
  161. /**
  162. * 生成缩略图
  163. * @param integer $width 缩略图最大宽度
  164. * @param integer $height 缩略图最大高度
  165. * @param integer $type 缩略图裁剪类型
  166. */
  167. public function thumb($width, $height, $type = Image::IMAGE_THUMB_SCALE){
  168. if(empty($this->img)) E('没有可以被缩略的图像资源');
  169. //原图宽度和高度
  170. $w = $this->info['width'];
  171. $h = $this->info['height'];
  172. /* 计算缩略图生成的必要参数 */
  173. switch ($type) {
  174. /* 等比例缩放 */
  175. case Image::IMAGE_THUMB_SCALE:
  176. //原图尺寸小于缩略图尺寸则不进行缩略
  177. if($w < $width && $h < $height) return;
  178. //计算缩放比例
  179. $scale = min($width/$w, $height/$h);
  180. //设置缩略图的坐标及宽度和高度
  181. $x = $y = 0;
  182. $width = $w * $scale;
  183. $height = $h * $scale;
  184. break;
  185. /* 居中裁剪 */
  186. case Image::IMAGE_THUMB_CENTER:
  187. //计算缩放比例
  188. $scale = max($width/$w, $height/$h);
  189. //设置缩略图的坐标及宽度和高度
  190. $w = $width/$scale;
  191. $h = $height/$scale;
  192. $x = ($this->info['width'] - $w)/2;
  193. $y = ($this->info['height'] - $h)/2;
  194. break;
  195. /* 左上角裁剪 */
  196. case Image::IMAGE_THUMB_NORTHWEST:
  197. //计算缩放比例
  198. $scale = max($width/$w, $height/$h);
  199. //设置缩略图的坐标及宽度和高度
  200. $x = $y = 0;
  201. $w = $width/$scale;
  202. $h = $height/$scale;
  203. break;
  204. /* 右下角裁剪 */
  205. case Image::IMAGE_THUMB_SOUTHEAST:
  206. //计算缩放比例
  207. $scale = max($width/$w, $height/$h);
  208. //设置缩略图的坐标及宽度和高度
  209. $w = $width/$scale;
  210. $h = $height/$scale;
  211. $x = $this->info['width'] - $w;
  212. $y = $this->info['height'] - $h;
  213. break;
  214. /* 填充 */
  215. case Image::IMAGE_THUMB_FILLED:
  216. //计算缩放比例
  217. if($w < $width && $h < $height){
  218. $scale = 1;
  219. } else {
  220. $scale = min($width/$w, $height/$h);
  221. }
  222. //设置缩略图的坐标及宽度和高度
  223. $neww = $w * $scale;
  224. $newh = $h * $scale;
  225. $posx = ($width - $w * $scale)/2;
  226. $posy = ($height - $h * $scale)/2;
  227. do{
  228. //创建新图像
  229. $img = imagecreatetruecolor($width, $height);
  230. // 调整默认颜色
  231. $color = imagecolorallocate($img, 255, 255, 255);
  232. imagefill($img, 0, 0, $color);
  233. //裁剪
  234. imagecopyresampled($img, $this->img, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
  235. imagedestroy($this->img); //销毁原图
  236. $this->img = $img;
  237. } while(!empty($this->gif) && $this->gifNext());
  238. $this->info['width'] = $width;
  239. $this->info['height'] = $height;
  240. return;
  241. /* 固定 */
  242. case Image::IMAGE_THUMB_FIXED:
  243. $x = $y = 0;
  244. break;
  245. default:
  246. E('不支持的缩略图裁剪类型');
  247. }
  248. /* 裁剪图像 */
  249. $this->crop($w, $h, $x, $y, $width, $height);
  250. }
  251. /**
  252. * 添加水印
  253. * @param string $source 水印图片路径
  254. * @param integer $locate 水印位置
  255. * @param integer $alpha 水印透明度
  256. */
  257. public function water($source, $locate = Image::IMAGE_WATER_SOUTHEAST,$alpha=80){
  258. //资源检测
  259. if(empty($this->img)) E('没有可以被添加水印的图像资源');
  260. if(!is_file($source)) E('水印图像不存在');
  261. //获取水印图像信息
  262. $info = getimagesize($source);
  263. if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
  264. E('非法水印文件');
  265. }
  266. //创建水印图像资源
  267. $fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  268. $water = $fun($source);
  269. //设定水印图像的混色模式
  270. imagealphablending($water, true);
  271. /* 设定水印位置 */
  272. switch ($locate) {
  273. /* 右下角水印 */
  274. case Image::IMAGE_WATER_SOUTHEAST:
  275. $x = $this->info['width'] - $info[0];
  276. $y = $this->info['height'] - $info[1];
  277. break;
  278. /* 左下角水印 */
  279. case Image::IMAGE_WATER_SOUTHWEST:
  280. $x = 0;
  281. $y = $this->info['height'] - $info[1];
  282. break;
  283. /* 左上角水印 */
  284. case Image::IMAGE_WATER_NORTHWEST:
  285. $x = $y = 0;
  286. break;
  287. /* 右上角水印 */
  288. case Image::IMAGE_WATER_NORTHEAST:
  289. $x = $this->info['width'] - $info[0];
  290. $y = 0;
  291. break;
  292. /* 居中水印 */
  293. case Image::IMAGE_WATER_CENTER:
  294. $x = ($this->info['width'] - $info[0])/2;
  295. $y = ($this->info['height'] - $info[1])/2;
  296. break;
  297. /* 下居中水印 */
  298. case Image::IMAGE_WATER_SOUTH:
  299. $x = ($this->info['width'] - $info[0])/2;
  300. $y = $this->info['height'] - $info[1];
  301. break;
  302. /* 右居中水印 */
  303. case Image::IMAGE_WATER_EAST:
  304. $x = $this->info['width'] - $info[0];
  305. $y = ($this->info['height'] - $info[1])/2;
  306. break;
  307. /* 上居中水印 */
  308. case Image::IMAGE_WATER_NORTH:
  309. $x = ($this->info['width'] - $info[0])/2;
  310. $y = 0;
  311. break;
  312. /* 左居中水印 */
  313. case Image::IMAGE_WATER_WEST:
  314. $x = 0;
  315. $y = ($this->info['height'] - $info[1])/2;
  316. break;
  317. default:
  318. /* 自定义水印坐标 */
  319. if(is_array($locate)){
  320. list($x, $y) = $locate;
  321. } else {
  322. E('不支持的水印位置类型');
  323. }
  324. }
  325. do{
  326. //添加水印
  327. $src = imagecreatetruecolor($info[0], $info[1]);
  328. // 调整默认颜色
  329. $color = imagecolorallocate($src, 255, 255, 255);
  330. imagefill($src, 0, 0, $color);
  331. imagecopy($src, $this->img, 0, 0, $x, $y, $info[0], $info[1]);
  332. imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
  333. imagecopymerge($this->img, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
  334. //销毁零时图片资源
  335. imagedestroy($src);
  336. } while(!empty($this->gif) && $this->gifNext());
  337. //销毁水印资源
  338. imagedestroy($water);
  339. }
  340. /**
  341. * 图像添加文字
  342. * @param string $text 添加的文字
  343. * @param string $font 字体路径
  344. * @param integer $size 字号
  345. * @param string $color 文字颜色
  346. * @param integer $locate 文字写入位置
  347. * @param integer $offset 文字相对当前位置的偏移量
  348. * @param integer $angle 文字倾斜角度
  349. */
  350. public function text($text, $font, $size, $color = '#00000000',
  351. $locate = Image::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){
  352. //资源检测
  353. if(empty($this->img)) E('没有可以被写入文字的图像资源');
  354. if(!is_file($font)) E("不存在的字体文件:{$font}");
  355. //获取文字信息
  356. $info = imagettfbbox($size, $angle, $font, $text);
  357. $minx = min($info[0], $info[2], $info[4], $info[6]);
  358. $maxx = max($info[0], $info[2], $info[4], $info[6]);
  359. $miny = min($info[1], $info[3], $info[5], $info[7]);
  360. $maxy = max($info[1], $info[3], $info[5], $info[7]);
  361. /* 计算文字初始坐标和尺寸 */
  362. $x = $minx;
  363. $y = abs($miny);
  364. $w = $maxx - $minx;
  365. $h = $maxy - $miny;
  366. /* 设定文字位置 */
  367. switch ($locate) {
  368. /* 右下角文字 */
  369. case Image::IMAGE_WATER_SOUTHEAST:
  370. $x += $this->info['width'] - $w;
  371. $y += $this->info['height'] - $h;
  372. break;
  373. /* 左下角文字 */
  374. case Image::IMAGE_WATER_SOUTHWEST:
  375. $y += $this->info['height'] - $h;
  376. break;
  377. /* 左上角文字 */
  378. case Image::IMAGE_WATER_NORTHWEST:
  379. // 起始坐标即为左上角坐标,无需调整
  380. break;
  381. /* 右上角文字 */
  382. case Image::IMAGE_WATER_NORTHEAST:
  383. $x += $this->info['width'] - $w;
  384. break;
  385. /* 居中文字 */
  386. case Image::IMAGE_WATER_CENTER:
  387. $x += ($this->info['width'] - $w)/2;
  388. $y += ($this->info['height'] - $h)/2;
  389. break;
  390. /* 下居中文字 */
  391. case Image::IMAGE_WATER_SOUTH:
  392. $x += ($this->info['width'] - $w)/2;
  393. $y += $this->info['height'] - $h;
  394. break;
  395. /* 右居中文字 */
  396. case Image::IMAGE_WATER_EAST:
  397. $x += $this->info['width'] - $w;
  398. $y += ($this->info['height'] - $h)/2;
  399. break;
  400. /* 上居中文字 */
  401. case Image::IMAGE_WATER_NORTH:
  402. $x += ($this->info['width'] - $w)/2;
  403. break;
  404. /* 左居中文字 */
  405. case Image::IMAGE_WATER_WEST:
  406. $y += ($this->info['height'] - $h)/2;
  407. break;
  408. default:
  409. /* 自定义文字坐标 */
  410. if(is_array($locate)){
  411. list($posx, $posy) = $locate;
  412. $x += $posx;
  413. $y += $posy;
  414. } else {
  415. E('不支持的文字位置类型');
  416. }
  417. }
  418. /* 设置偏移量 */
  419. if(is_array($offset)){
  420. $offset = array_map('intval', $offset);
  421. list($ox, $oy) = $offset;
  422. } else{
  423. $offset = intval($offset);
  424. $ox = $oy = $offset;
  425. }
  426. /* 设置颜色 */
  427. if(is_string($color) && 0 === strpos($color, '#')){
  428. $color = str_split(substr($color, 1), 2);
  429. $color = array_map('hexdec', $color);
  430. if(empty($color[3]) || $color[3] > 127){
  431. $color[3] = 0;
  432. }
  433. } elseif (!is_array($color)) {
  434. E('错误的颜色值');
  435. }
  436. do{
  437. /* 写入文字 */
  438. $col = imagecolorallocatealpha($this->img, $color[0], $color[1], $color[2], $color[3]);
  439. imagettftext($this->img, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text);
  440. } while(!empty($this->gif) && $this->gifNext());
  441. }
  442. /* 切换到GIF的下一帧并保存当前帧,内部使用 */
  443. private function gifNext(){
  444. ob_start();
  445. ob_implicit_flush(0);
  446. imagegif($this->img);
  447. $img = ob_get_clean();
  448. $this->gif->image($img);
  449. $next = $this->gif->nextImage();
  450. if($next){
  451. $this->img = imagecreatefromstring($next);
  452. return $next;
  453. } else {
  454. $this->img = imagecreatefromstring($this->gif->image());
  455. return false;
  456. }
  457. }
  458. /**
  459. * 析构方法,用于销毁图像资源
  460. */
  461. public function __destruct() {
  462. empty($this->img) || imagedestroy($this->img);
  463. }
  464. }