imageChajian.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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=5,$align='lt')
  93. {
  94. if(!$this->bool)return;
  95. $font = 'arial.ttf';
  96. $lw = strlen($str)*($size);
  97. $lh = $size*1.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. imagestring($this->img, $size, $x, $y, $str, $color);
  119. $sapath = str_replace('.'.$this->ext.'', '_water.'.$this->ext.'', $this->path);
  120. $sapath = $this->path;
  121. $this->saveas($sapath, $this->img);//另存为
  122. }
  123. /**
  124. 添加图片水印
  125. @params $align 图片位置
  126. */
  127. public function imgwater($imgpath,$align='rb')
  128. {
  129. if(!$this->bool || !file_exists($imgpath))return;
  130. list($lw, $lh) = getimagesize($imgpath);
  131. $logoimg = $this->createimgobj($imgpath);
  132. $x = 2;
  133. $y = 2;
  134. switch($align){
  135. case 'rb'://右下角
  136. $x = $this->w - $lw-2;
  137. $y = $this->h - $lh-2;
  138. break;
  139. case 'tr'://右上角
  140. $x = $this->w - $lw-2;
  141. break;
  142. case 'lb'://左下角
  143. $y = $this->h - $lh-2;
  144. break;
  145. case 'cn'://居中
  146. $x = ($this->w - $lw) * 0.5;
  147. $y = ($this->h - $lh) * 0.5;
  148. break;
  149. }
  150. imagecopymerge($this->img, $logoimg, $x ,$y ,0 ,0 ,$lw ,$lh, 100);
  151. $this->saveas($this->path, $this->img);
  152. }
  153. /**
  154. 创建颜色
  155. */
  156. public function color($color,$img)
  157. {
  158. if(!empty($color)&&(strlen($color)==7)){
  159. $r=hexdec(substr($color,1,2));
  160. $g=hexdec(substr($color,3,2));
  161. $b=hexdec(substr($color,5));
  162. }else{
  163. $r=$g=$b='00';
  164. }
  165. return imagecolorallocate($img, $r, $g, $b);
  166. }
  167. public function colorTorgb($color)
  168. {
  169. if(!empty($color)&&(strlen($color)==7)){
  170. $r=hexdec(substr($color,1,2));
  171. $g=hexdec(substr($color,3,2));
  172. $b=hexdec(substr($color,5));
  173. }else{
  174. $r=$g=$b='00';
  175. }
  176. return array($r, $g, $b);
  177. }
  178. public function colorToHsl($color)
  179. {
  180. $rgba = $this->colorTorgb($color);
  181. $r = floatval($rgba[0])/255;
  182. $g = floatval($rgba[1])/255;
  183. $b = floatval($rgba[2])/255;
  184. $rgb = array($r, $g, $b);
  185. $max = max($rgb);
  186. $min = min($rgb);
  187. $diff = $max - $min;
  188. if ($max == $min) {
  189. $h = 0;
  190. } else if ($max == $r && $g >= $b) {
  191. $h = 60 * (($g - $b) / $diff);
  192. } else if ($max == $r && $g < $b) {
  193. $h = 60 * (($g - $b) / $diff) + 360;
  194. } else if ($max == $g) {
  195. $h = 60 * (($b - $r) / $diff) + 120;
  196. } else if ($max == $b) {
  197. $h = 60 * (($r - $g) / $diff) + 240;
  198. }
  199. $l = ($max + $min) / 2;
  200. if ($l == 0 || $max == $min) {
  201. $s = 0;
  202. } else if (0 < $l && $l <= 0.5) {
  203. $s = $diff / (2 * $l);
  204. } else if ($l > 0.5) {
  205. $s = $diff / (2 - 2 * $l);
  206. }
  207. return array($h, round($s*100,2), round($l*100,2));
  208. }
  209. /**
  210. 注销图片
  211. */
  212. private function destroy()
  213. {
  214. imagedestroy($this->img);
  215. }
  216. /**
  217. 另存图片
  218. */
  219. private function saveas($spath,$img)
  220. {
  221. $ext = $this->getmime($spath);
  222. $this->saveass($ext, $img, $spath);
  223. }
  224. private function saveass($ext,$img, $spath)
  225. {
  226. switch($ext){
  227. case 'image/gif':
  228. imagegif($img,$spath);
  229. break;
  230. case 'image/png':
  231. imagepng($img,$spath);
  232. break;
  233. case 'image/bmp':
  234. imagewbmp($img,$spath);
  235. break;
  236. default:
  237. imagejpeg($img,$spath,80);
  238. break;
  239. }
  240. }
  241. /**
  242. * 压缩图片
  243. */
  244. public function compress($w,$h)
  245. {
  246. if(!$this->bool)return '';
  247. $bili = 1;
  248. $nh = $h;
  249. $nw = $w;
  250. if($w < $this->w){
  251. $bili = $w / $this->w;
  252. $nh = $bili * $this->h;
  253. }
  254. if($h>0 && $nh > $h){
  255. $bili = $h / $this->h;
  256. $nh = $h;
  257. $nw = $bili * $this->w;
  258. }
  259. if($bili==1)return '';
  260. $yspath = str_replace('.'.$this->ext.'', 'y.'.$this->ext.'', $this->path);
  261. return $this->thumbnail($nw, $nh, 0, $yspath);
  262. }
  263. /**
  264. 图片缩略图
  265. @param $w 宽
  266. @param $h 高
  267. */
  268. public function thumbnail($w,$h,$lx=0, $sapath='')
  269. {
  270. if(!$this->bool)return '';
  271. list($mw, $mh, $bili) = $this->imgwh($w,$h);
  272. $tmpimg = imagecreatetruecolor($w,$h);
  273. imagefill($tmpimg,0,0,$this->color('#ffffff',$tmpimg));
  274. $tx = 0;
  275. $ty = 0;
  276. //开始截的位置
  277. $sx = 0;
  278. $sy = 0;
  279. if($w > $mw){
  280. if($lx==1){//整图缩略可以看到白边
  281. $tx = ($w-$mw)/2;
  282. }else if($lx == 0){//可能去掉看不到的
  283. $mw = $w;
  284. $mh = $mw/$this->whbili;
  285. $nbl= $mh/$this->h;
  286. $sy = ($mh-$h)/2/$nbl; //当前缩放比例
  287. }
  288. }
  289. if($h > $mh){
  290. if($lx==1){
  291. $ty = ($h-$mh)/2;
  292. }else if($lx == 0){
  293. $mh = $h;
  294. $mw = $mh*$this->whbili;
  295. $nbl= $mw/$this->w;
  296. $sx = ($mw-$w)/2/$nbl; //当前缩放比例
  297. }
  298. }
  299. //imagecopyresized
  300. imagecopyresampled($tmpimg, $this->img, $tx,$ty, $sx,$sy, $mw,$mh,$this->w,$this->h);//生成缩略图
  301. //$sapath = str_replace('.'.$this->ext.'', '_thumb'.$w.'x'.$h.'.'.$this->ext.'', $this->path);
  302. if($sapath=='')$sapath = str_replace('.'.$this->ext.'', '_s.'.$this->ext.'', $this->path);
  303. $this->saveass($this->mime ,$tmpimg, $sapath);//保存图片
  304. return $sapath;
  305. }
  306. /**
  307. 图片显示宽高
  308. */
  309. public function imgwh($mw,$mh)
  310. {
  311. $w = $this->w;
  312. $h = $this->h;
  313. $bili=1;
  314. if($w>$mw){
  315. $bili=($mw/$w);
  316. $h=$bili*$h;
  317. $w=$mw;
  318. }
  319. if($h>$mh){
  320. $bili=($mh/$this->h);
  321. $w=$bili*$this->w;
  322. $h=$mh;
  323. }
  324. return array($w,$h,$bili);
  325. }
  326. }
粤ICP备19079148号