imageChajian.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. gd2库图片处理,添加水印
  4. */
  5. class imageChajian extends Chajian
  6. {
  7. public $ext; //图片类型
  8. public $img; //图片对象
  9. public $mime; //图片对象
  10. public $path; //图片地址
  11. public $w = 0;
  12. public $h = 0;
  13. public $bool = false;
  14. public $size = 0;
  15. public $whbili = 0;//高和宽的比例
  16. protected function initChajian()
  17. {
  18. $this->bool = function_exists('ImageCreate');
  19. }
  20. /**
  21. 创建图片对象$rotate旋转角度
  22. */
  23. public function createimg($path,$rotate=0)
  24. {
  25. if(!$this->bool)return false;
  26. $this->bool = false;
  27. if(!file_exists($path))return false;
  28. $this->ext = $this->getext($path);
  29. $this->path = $path;
  30. $img = $this->createimgobj($path);
  31. $this->img = $img;
  32. if(!$img)return false;
  33. //判断是否旋转
  34. if($rotate != 0 && $rotate<360 && $rotate>-360){
  35. $white = $this->color('#ffffff',$img);
  36. $img = imagerotate($img, $rotate, $white);//旋转
  37. }
  38. $this->w = imagesx($this->img);
  39. $this->h = imagesy($this->img);
  40. $this->size = ceil(filesize($this->path)/1024);
  41. $this->whbili = $this->w/$this->h;
  42. $this->bool = true;
  43. }
  44. /**
  45. 获取图片对象
  46. */
  47. public function createimgobj($path)
  48. {
  49. $ext = $this->getmime($path);
  50. $img = false;
  51. switch ($ext){
  52. case 'image/gif':
  53. $img=imagecreatefromgif($path);
  54. break;
  55. case 'image/png':
  56. $img=imagecreatefrompng($path);
  57. break;
  58. default:
  59. $img=imagecreatefromjpeg($path);
  60. break;
  61. }
  62. return $img;
  63. }
  64. public function conver($opath, $npath)
  65. {
  66. if(!file_exists($opath))return;
  67. $img = $this->createimgobj($opath);
  68. $this->saveas($npath, $img);
  69. }
  70. /**
  71. 获取图片的格式
  72. */
  73. public function getext($img_name)
  74. {
  75. $type = strtolower(substr($img_name,strrpos($img_name,'.')+1));
  76. return $type;
  77. }
  78. public function getmime($img_name)
  79. {
  80. $mime = '';
  81. if(file_exists($img_name)){
  82. $fileobj = getimagesize($img_name);
  83. $mime = strtolower($fileobj['mime']);
  84. $this->mime = $mime;
  85. }
  86. return $mime;
  87. }
  88. /**
  89. 添加文字水印
  90. $str 添加文字
  91. */
  92. public function addwater($str,$color='#000000',$size=20,$align='rb')
  93. {
  94. if(!$this->bool)return;
  95. $font = '../fonts/FZZHYJW.TTF'; //方正稚艺简体
  96. $lw = strlen($str)*($size/2);
  97. $lh = $size*0.5;
  98. $color = $this->color($color,$this->img);
  99. $x = 2;
  100. $y = 2;
  101. switch($align){
  102. case 'rb'://右下角
  103. $x = $this->w - $lw-2;
  104. $y = $this->h - $lh-2;
  105. break;
  106. case 'tr'://右上角
  107. $x = $this->w - $lw-2;
  108. break;
  109. case 'lb'://左下角
  110. $y = $this->h - $lh-2;
  111. break;
  112. case 'cn'://居中
  113. $x = ($this->w - $lw) * 0.5;
  114. $y = ($this->h - $lh) * 0.5;
  115. break;
  116. }
  117. imagettftext($this->img, $size,0, $x, $y, $color, $font, $str);
  118. $sapath = str_replace('.'.$this->ext.'', '_water.'.$this->ext.'', $this->path);
  119. $sapath = $this->path;
  120. $this->saveas($sapath, $this->img);//另存为
  121. }
  122. /**
  123. 添加图片水印
  124. @params $align 图片位置
  125. */
  126. public function imgwater($imgpath,$align='rb')
  127. {
  128. if(!$this->bool || !file_exists($imgpath))return;
  129. list($lw, $lh) = getimagesize($imgpath);
  130. $logoimg = $this->createimgobj($imgpath);
  131. $x = 2;
  132. $y = 2;
  133. switch($align){
  134. case 'rb'://右下角
  135. $x = $this->w - $lw-2;
  136. $y = $this->h - $lh-2;
  137. break;
  138. case 'tr'://右上角
  139. $x = $this->w - $lw-2;
  140. break;
  141. case 'lb'://左下角
  142. $y = $this->h - $lh-2;
  143. break;
  144. case 'cn'://居中
  145. $x = ($this->w - $lw) * 0.5;
  146. $y = ($this->h - $lh) * 0.5;
  147. break;
  148. }
  149. imagecopymerge($this->img, $logoimg, $x ,$y ,0 ,0 ,$lw ,$lh, 100);
  150. $this->saveas($this->path, $this->img);
  151. }
  152. /**
  153. 创建颜色
  154. */
  155. public function color($color,$img)
  156. {
  157. if(!empty($color)&&(strlen($color)==7)){
  158. $r=hexdec(substr($color,1,2));
  159. $g=hexdec(substr($color,3,2));
  160. $b=hexdec(substr($color,5));
  161. }else{
  162. $r=$g=$b='00';
  163. }
  164. return imagecolorallocate($img, $r, $g, $b);
  165. }
  166. public function colorTorgb($color)
  167. {
  168. if(!empty($color)&&(strlen($color)==7)){
  169. $r=hexdec(substr($color,1,2));
  170. $g=hexdec(substr($color,3,2));
  171. $b=hexdec(substr($color,5));
  172. }else{
  173. $r=$g=$b='00';
  174. }
  175. return array($r, $g, $b);
  176. }
  177. public function colorToHsl($color)
  178. {
  179. $rgba = $this->colorTorgb($color);
  180. $r = floatval($rgba[0])/255;
  181. $g = floatval($rgba[1])/255;
  182. $b = floatval($rgba[2])/255;
  183. $rgb = array($r, $g, $b);
  184. $max = max($rgb);
  185. $min = min($rgb);
  186. $diff = $max - $min;
  187. if ($max == $min) {
  188. $h = 0;
  189. } else if ($max == $r && $g >= $b) {
  190. $h = 60 * (($g - $b) / $diff);
  191. } else if ($max == $r && $g < $b) {
  192. $h = 60 * (($g - $b) / $diff) + 360;
  193. } else if ($max == $g) {
  194. $h = 60 * (($b - $r) / $diff) + 120;
  195. } else if ($max == $b) {
  196. $h = 60 * (($r - $g) / $diff) + 240;
  197. }
  198. $l = ($max + $min) / 2;
  199. if ($l == 0 || $max == $min) {
  200. $s = 0;
  201. } else if (0 < $l && $l <= 0.5) {
  202. $s = $diff / (2 * $l);
  203. } else if ($l > 0.5) {
  204. $s = $diff / (2 - 2 * $l);
  205. }
  206. return array($h, round($s*100,2), round($l*100,2));
  207. }
  208. /**
  209. 注销图片
  210. */
  211. private function destroy()
  212. {
  213. imagedestroy($this->img);
  214. }
  215. /**
  216. 另存图片
  217. */
  218. private function saveas($spath,$img)
  219. {
  220. $ext = $this->getmime($spath);
  221. $this->saveass($ext, $img, $spath);
  222. }
  223. private function saveass($ext,$img, $spath)
  224. {
  225. switch($ext){
  226. case 'image/gif':
  227. imagegif($img,$spath);
  228. break;
  229. case 'image/png':
  230. imagepng($img,$spath);
  231. break;
  232. case 'image/bmp':
  233. imagewbmp($img,$spath);
  234. break;
  235. default:
  236. imagejpeg($img,$spath,80);
  237. break;
  238. }
  239. }
  240. /**
  241. * 压缩图片
  242. */
  243. public function compress($w,$h)
  244. {
  245. if(!$this->bool)return '';
  246. $bili = 1;
  247. $nh = $h;
  248. $nw = $w;
  249. if($w < $this->w){
  250. $bili = $w / $this->w;
  251. $nh = $bili * $this->h;
  252. }
  253. if($h>0 && $nh > $h){
  254. $bili = $h / $this->h;
  255. $nh = $h;
  256. $nw = $bili * $this->w;
  257. }
  258. if($bili==1)return '';
  259. $yspath = str_replace('.'.$this->ext.'', 'y.'.$this->ext.'', $this->path);
  260. return $this->thumbnail($nw, $nh, 0, $yspath);
  261. }
  262. /**
  263. 图片缩略图
  264. @param $w 宽
  265. @param $h 高
  266. */
  267. public function thumbnail($w,$h,$lx=0, $sapath='')
  268. {
  269. if(!$this->bool)return '';
  270. list($mw, $mh, $bili) = $this->imgwh($w,$h);
  271. $tmpimg = imagecreatetruecolor($w,$h);
  272. imagefill($tmpimg,0,0,$this->color('#ffffff',$tmpimg));
  273. $tx = 0;
  274. $ty = 0;
  275. //开始截的位置
  276. $sx = 0;
  277. $sy = 0;
  278. if($w > $mw){
  279. if($lx==1){//整图缩略可以看到白边
  280. $tx = ($w-$mw)/2;
  281. }else if($lx == 0){//可能去掉看不到的
  282. $mw = $w;
  283. $mh = $mw/$this->whbili;
  284. $nbl= $mh/$this->h;
  285. $sy = ($mh-$h)/2/$nbl; //当前缩放比例
  286. }
  287. }
  288. if($h > $mh){
  289. if($lx==1){
  290. $ty = ($h-$mh)/2;
  291. }else if($lx == 0){
  292. $mh = $h;
  293. $mw = $mh*$this->whbili;
  294. $nbl= $mw/$this->w;
  295. $sx = ($mw-$w)/2/$nbl; //当前缩放比例
  296. }
  297. }
  298. //imagecopyresized
  299. imagecopyresampled($tmpimg, $this->img, $tx,$ty, $sx,$sy, $mw,$mh,$this->w,$this->h);//生成缩略图
  300. //$sapath = str_replace('.'.$this->ext.'', '_thumb'.$w.'x'.$h.'.'.$this->ext.'', $this->path);
  301. if($sapath=='')$sapath = str_replace('.'.$this->ext.'', '_s.'.$this->ext.'', $this->path);
  302. $this->saveass($this->mime ,$tmpimg, $sapath);//保存图片
  303. return $sapath;
  304. }
  305. /**
  306. 图片显示宽高
  307. */
  308. public function imgwh($mw,$mh)
  309. {
  310. $w = $this->w;
  311. $h = $this->h;
  312. $bili=1;
  313. if($w>$mw){
  314. $bili=($mw/$w);
  315. $h=$bili*$h;
  316. $w=$mw;
  317. }
  318. if($h>$mh){
  319. $bili=($mh/$this->h);
  320. $w=$bili*$this->w;
  321. $h=$mh;
  322. }
  323. return array($w,$h,$bili);
  324. }
  325. }
粤ICP备19079148号