Image.class.php 6.9 KB

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