Image.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // | ThinkImage.class.php 2013-03-05
  12. // +----------------------------------------------------------------------
  13. namespace Think;
  14. /**
  15. * 图片处理驱动类,可配置图片处理库
  16. * 目前支持GD库和imagick
  17. * @author 麦当苗儿 <zuojiazi@vip.qq.com>
  18. */
  19. class Image
  20. {
  21. /* 驱动相关常量定义 */
  22. const IMAGE_GD = 1; //常量,标识GD库类型
  23. const IMAGE_IMAGICK = 2; //常量,标识imagick库类型
  24. /* 缩略图相关常量定义 */
  25. const IMAGE_THUMB_SCALE = 1; //常量,标识缩略图等比例缩放类型
  26. const IMAGE_THUMB_FILLED = 2; //常量,标识缩略图缩放后填充类型
  27. const IMAGE_THUMB_CENTER = 3; //常量,标识缩略图居中裁剪类型
  28. const IMAGE_THUMB_NORTHWEST = 4; //常量,标识缩略图左上角裁剪类型
  29. const IMAGE_THUMB_SOUTHEAST = 5; //常量,标识缩略图右下角裁剪类型
  30. const IMAGE_THUMB_FIXED = 6; //常量,标识缩略图固定尺寸缩放类型
  31. /* 水印相关常量定义 */
  32. const IMAGE_WATER_NORTHWEST = 1; //常量,标识左上角水印
  33. const IMAGE_WATER_NORTH = 2; //常量,标识上居中水印
  34. const IMAGE_WATER_NORTHEAST = 3; //常量,标识右上角水印
  35. const IMAGE_WATER_WEST = 4; //常量,标识左居中水印
  36. const IMAGE_WATER_CENTER = 5; //常量,标识居中水印
  37. const IMAGE_WATER_EAST = 6; //常量,标识右居中水印
  38. const IMAGE_WATER_SOUTHWEST = 7; //常量,标识左下角水印
  39. const IMAGE_WATER_SOUTH = 8; //常量,标识下居中水印
  40. const IMAGE_WATER_SOUTHEAST = 9; //常量,标识右下角水印
  41. /**
  42. * 图片资源
  43. * @var resource
  44. */
  45. private $img;
  46. /**
  47. * 构造方法,用于实例化一个图片处理对象
  48. * @param string $type 要使用的类库,默认使用GD库
  49. */
  50. public function __construct($type = self::IMAGE_GD, $imgname = null)
  51. {
  52. /* 判断调用库的类型 */
  53. switch ($type) {
  54. case self::IMAGE_GD:
  55. $class = 'Gd';
  56. break;
  57. case self::IMAGE_IMAGICK:
  58. $class = 'Imagick';
  59. break;
  60. default:
  61. E('不支持的图片处理库类型');
  62. }
  63. /* 引入处理库,实例化图片处理对象 */
  64. $class = "Think\\Image\\Driver\\{$class}";
  65. $this->img = new $class($imgname);
  66. }
  67. /**
  68. * 打开一幅图像
  69. * @param string $imgname 图片路径
  70. * @return Object 当前图片处理库对象
  71. */
  72. public function open($imgname)
  73. {
  74. $this->img->open($imgname);
  75. return $this;
  76. }
  77. /**
  78. * 保存图片
  79. * @param string $imgname 图片保存名称
  80. * @param string $type 图片类型
  81. * @param integer $quality 图像质量
  82. * @param boolean $interlace 是否对JPEG类型图片设置隔行扫描
  83. * @return Object 当前图片处理库对象
  84. */
  85. public function save($imgname, $type = null, $quality = 80, $interlace = true)
  86. {
  87. $this->img->save($imgname, $type, $quality, $interlace);
  88. return $this;
  89. }
  90. /**
  91. * 返回图片宽度
  92. * @return integer 图片宽度
  93. */
  94. public function width()
  95. {
  96. return $this->img->width();
  97. }
  98. /**
  99. * 返回图片高度
  100. * @return integer 图片高度
  101. */
  102. public function height()
  103. {
  104. return $this->img->height();
  105. }
  106. /**
  107. * 返回图像类型
  108. * @return string 图片类型
  109. */
  110. public function type()
  111. {
  112. return $this->img->type();
  113. }
  114. /**
  115. * 返回图像MIME类型
  116. * @return string 图像MIME类型
  117. */
  118. public function mime()
  119. {
  120. return $this->img->mime();
  121. }
  122. /**
  123. * 返回图像尺寸数组 0 - 图片宽度,1 - 图片高度
  124. * @return array 图片尺寸
  125. */
  126. public function size()
  127. {
  128. return $this->img->size();
  129. }
  130. /**
  131. * 裁剪图片
  132. * @param integer $w 裁剪区域宽度
  133. * @param integer $h 裁剪区域高度
  134. * @param integer $x 裁剪区域x坐标
  135. * @param integer $y 裁剪区域y坐标
  136. * @param integer $width 图片保存宽度
  137. * @param integer $height 图片保存高度
  138. * @return Object 当前图片处理库对象
  139. */
  140. public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)
  141. {
  142. $this->img->crop($w, $h, $x, $y, $width, $height);
  143. return $this;
  144. }
  145. /**
  146. * 生成缩略图
  147. * @param integer $width 缩略图最大宽度
  148. * @param integer $height 缩略图最大高度
  149. * @param integer $type 缩略图裁剪类型
  150. * @return Object 当前图片处理库对象
  151. */
  152. public function thumb($width, $height, $type = self::IMAGE_THUMB_SCALE)
  153. {
  154. $this->img->thumb($width, $height, $type);
  155. return $this;
  156. }
  157. /**
  158. * 添加水印
  159. * @param string $source 水印图片路径
  160. * @param integer $locate 水印位置
  161. * @param integer $alpha 水印透明度
  162. * @return Object 当前图片处理库对象
  163. */
  164. public function water($source, $locate = self::IMAGE_WATER_SOUTHEAST, $alpha = 80)
  165. {
  166. $this->img->water($source, $locate, $alpha);
  167. return $this;
  168. }
  169. /**
  170. * 图像添加文字
  171. * @param string $text 添加的文字
  172. * @param string $font 字体路径
  173. * @param integer $size 字号
  174. * @param string $color 文字颜色
  175. * @param integer $locate 文字写入位置
  176. * @param integer $offset 文字相对当前位置的偏移量
  177. * @param integer $angle 文字倾斜角度
  178. * @return Object 当前图片处理库对象
  179. */
  180. public function text($text, $font, $size, $color = '#00000000',
  181. $locate = self::IMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0) {
  182. $this->img->text($text, $font, $size, $color, $locate, $offset, $angle);
  183. return $this;
  184. }
  185. }