Image.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. <?php
  2. /*
  3. Name:FastPHP
  4. CopyRight: Mr.Fu 2019SR0832915
  5. */
  6. namespace image;
  7. /*
  8. 图片处理类
  9. Author:FastPHP
  10. */
  11. use think\Exception;
  12. class Image
  13. {
  14. //获取图片真实信息
  15. static function getImageInfo($imgfile)
  16. {
  17. $imageinfo=getimagesize($imgfile);
  18. $types=array(1=>'gif',2=>'jpg',3=>'png',4=>'swf',5=>'psd',6=>'bmp',7=>'tiff',8=>'tiff',9=>'jpc',10=>'jp2',11=>'jpx',12=>'jb2',13=>'swc',14=>'iff',15=>'wbmp',16=>'xbm',17=>'ico',18=>'webp');
  19. if($imageinfo){
  20. list($width, $height, $type)=$imageinfo;
  21. if($types[$type]){
  22. $arr['type']=$types[$type];
  23. $arr['width']=$width;
  24. $arr['height']=$height;
  25. return $arr;
  26. }else{
  27. return false;
  28. }
  29. }else{
  30. return false;
  31. }
  32. }
  33. //图片格式转换:支持源:jpg,png,gif,bmp,支持输出:jpg,png,gif,webp $src_clear 源文件是否清理
  34. static function convert($src,$dst='',$to_ext='jpg',$src_clear=false)
  35. {
  36. $info=self::getImageInfo($src);
  37. //源和目标格式相同不能转换
  38. if($info['type']==$to_ext){
  39. return $src;
  40. }
  41. switch(strtolower($info['type'])){
  42. case 'jpg':
  43. case 'jpeg':
  44. $img=imagecreatefromjpeg($src);
  45. break;
  46. case 'gif':
  47. $img=imagecreatefromgif($src);
  48. break;
  49. case 'png':
  50. $img=imagecreatefrompng($src);
  51. break;
  52. case 'bmp':
  53. $img=imagecreatefrombmp($src);
  54. break;
  55. case 'webp':
  56. $img=imagecreatefromwebp($src);
  57. break;
  58. default:
  59. break;
  60. }
  61. if($src_clear){
  62. \unlink($src);
  63. }
  64. //如果输出目录为空则组装输出路径
  65. if($dst==""){
  66. $file=pathinfo($src);
  67. $dst=$file['dirname'].DS.$file['filename'].'.'.$to_ext;
  68. }
  69. if($img){
  70. switch($to_ext){
  71. case 'jpg':
  72. case 'jpeg':
  73. imagejpeg($img,$dst);
  74. return $dst;
  75. case 'png':
  76. imagepng($img,$dst);
  77. return $dst;
  78. case 'gif';
  79. imagegif($img,$dst);
  80. return $dst;
  81. case 'webp':
  82. imagewebp($img,$dst);
  83. return $dst;
  84. default:
  85. break;
  86. }
  87. }
  88. return false;
  89. }
  90. //生成验证图片
  91. static function getAuthImage($text,$name,$im_x = 160,$im_y = 50) {
  92. $alpha = 126;// 0表示完全不透明,127表示完全透明
  93. // 创建画板
  94. $distortion_im = imagecreatetruecolor($im_x,$im_y);
  95. imagesavealpha($distortion_im,true);
  96. //生成随机颜色
  97. $tmpC0=mt_rand(100,255);
  98. $tmpC1=mt_rand(100,255);
  99. $tmpC2=mt_rand(100,255);
  100. $buttum_c = imagecolorallocatealpha($distortion_im,$tmpC0,$tmpC1,$tmpC2,$alpha);
  101. // 填充画板
  102. imagefill($distortion_im, 0, 0, $buttum_c);
  103. // 写入验证码文本
  104. $font = public_path().'static/font/en.ttf';
  105. for ($i=0;$i<strlen($text);$i++){
  106. $text_c = imagecolorallocatealpha($distortion_im, mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),0);
  107. $tmp =substr($text,$i,1);
  108. $array = array(-1,1);
  109. $p = array_rand($array);
  110. $an = $array[$p]*mt_rand(-15,15);//角度
  111. $size = 28;
  112. imagettftext($distortion_im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
  113. }
  114. // 创建一个随机颜色
  115. // $text_c = imagecolorallocatealpha($distortion_im, mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),0);
  116. // $distortion_im = imagecreatetruecolor ($im_x, $im_y);
  117. // imagefill($distortion_im, 16, 13, $buttum_c);
  118. // for ( $i=0; $i<$im_x; $i++) {
  119. // for ( $j=0; $j<$im_y; $j++) {
  120. // $rgb = imagecolorat($distortion_im, $i , $j);
  121. // if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) {
  122. // imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);
  123. // }
  124. // }
  125. // }
  126. //加入干扰象素;
  127. $count = 100;//干扰像素的数量
  128. for($i=0; $i<$count; $i++){
  129. $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  130. imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
  131. }
  132. //加入干扰线条
  133. $rand = mt_rand(5,30);
  134. $rand1 = mt_rand(15,25);
  135. $rand2 = mt_rand(5,10);
  136. for ($yy=$rand; $yy<=+$rand+2; $yy++){
  137. for ($px=-80;$px<=80;$px=$px+0.1){
  138. $x=$px/$rand1;
  139. if ($x!=0){
  140. $y=sin($x);
  141. }
  142. $py=$y*$rand2;
  143. imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
  144. }
  145. }
  146. ob_clean();
  147. $returnPath = 'uploads/validateImg/'.$name.'.png';
  148. $savePath = public_path().$returnPath;
  149. ImagePNG($distortion_im,$savePath);//以PNG格式将图像输出到浏览器或文件;
  150. ImageDestroy($distortion_im);//销毁一图像,释放与image关联的内存;
  151. // ImageDestroy($distortion_im);
  152. // echo ob_get_clean();
  153. return $returnPath;
  154. }
  155. /**
  156. * @title: php直接输出验证码图片
  157. * @desc: 描述
  158. * @param {string} {text} {} {验证码内容}
  159. * @param {string} {name} {} {验证码标识}
  160. * @param {int} {im_x} {} {宽度}
  161. * @param {int} {im_y} {} {高度}
  162. * @return {*}
  163. * @author: Rock
  164. * @method: POST
  165. * @Date: 2022-05-30 11:27:13
  166. */
  167. static function getAuthImage2($text,$im_x = 160,$im_y = 50)
  168. {
  169. $alpha = 126;// 0表示完全不透明,127表示完全透明
  170. // 创建画板
  171. $distortion_im = imagecreatetruecolor($im_x,$im_y);
  172. imagesavealpha($distortion_im,true);
  173. //生成随机颜色
  174. $tmpC0=mt_rand(100,255);
  175. $tmpC1=mt_rand(100,255);
  176. $tmpC2=mt_rand(100,255);
  177. $buttum_c = imagecolorallocatealpha($distortion_im,$tmpC0,$tmpC1,$tmpC2,$alpha);
  178. // 填充画板
  179. imagefill($distortion_im, 0, 0, $buttum_c);
  180. // 写入验证码文本
  181. $font = public_path().'static/font/en.ttf';
  182. // $font = "C:/DR_CODE/JDF/pubilc/static/font/en.ttf";
  183. for ($i=0;$i<strlen($text);$i++){
  184. $text_c = imagecolorallocatealpha($distortion_im, mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),0);
  185. $tmp =substr($text,$i,1);
  186. $array = array(-1,1);
  187. $p = array_rand($array);
  188. $an = $array[$p]*mt_rand(-15,15);//角度
  189. $size = 28;
  190. imagettftext($distortion_im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
  191. }
  192. //加入干扰象素;
  193. $count = 100;//干扰像素的数量
  194. for($i=0; $i<$count; $i++){
  195. $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  196. imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
  197. }
  198. //加入干扰线条
  199. $rand = mt_rand(5,30);
  200. $rand1 = mt_rand(15,25);
  201. $rand2 = mt_rand(5,10);
  202. for ($yy=$rand; $yy<=+$rand+2; $yy++){
  203. for ($px=-80;$px<=80;$px=$px+0.1){
  204. $x=$px/$rand1;
  205. if ($x!=0){
  206. $y=sin($x);
  207. }
  208. $py=$y*$rand2;
  209. imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
  210. }
  211. }
  212. ob_clean();
  213. imagepng($distortion_im);//以PNG格式将图像输出到浏览器或文件;
  214. $content = ob_get_clean();
  215. ImageDestroy($distortion_im);//销毁一图像,释放与image关联的内存;
  216. return $content;
  217. }
  218. //保存base64的图片数据
  219. static function SaveBase64Img($data){
  220. $dir = DS.'uploads'.DS.'images'.DS.date('Ymd',time()).DS;
  221. $savepath = ROOT_PATH.$dir;
  222. if(!file_exists($savepath)){
  223. mkdir($savepath,0777,true);
  224. }
  225. $filename = time().GetRandStr(10).'.jpg';
  226. $data = substr($data,22);
  227. $tmp = base64_decode($data);
  228. file_put_contents($savepath.DS.$filename,$tmp);
  229. return $dir.$filename;
  230. }
  231. /*
  232. 模糊化像素颜色,按照四舍五入的方式
  233. */
  234. static function Probably($int){
  235. $len=strlen($int);
  236. if($len==1){
  237. return 0;
  238. }elseif($len==2 or $len==3){
  239. return round($int/10)."0";
  240. }else{
  241. return false;
  242. }
  243. }
  244. /*
  245. 模糊化像素颜色,按照30的步长模糊,把255*255*255种颜色,模糊化:9*9*9的方式
  246. */
  247. static function Probably2($int){
  248. $ints=array(
  249. 0=>array(0,29),
  250. 30=>array(30,59),
  251. 60=>array(60,89),
  252. 90=>array(90,119),
  253. 120=>array(120,149),
  254. 150=>array(150,179),
  255. 180=>array(180,209),
  256. 210=>array(210,239),
  257. 240=>array(240,256),
  258. );
  259. foreach($ints as $k=>$v){
  260. $min=$v[0];
  261. $max=$v[1];
  262. if(($int>=$min)and($int<=$max)){
  263. return $k;
  264. }
  265. }
  266. }
  267. /*
  268. 获得指定图片的颜色列表
  269. */
  270. static function GetImgColorList($src_file,$samples=50,$top=20){
  271. //图片的等比缩放 因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源
  272. $src=imagecreatefromjpeg($src_file);
  273. //取得源图片的宽度和高度
  274. $size_src=getimagesize($src_file);
  275. $w=$size_src['0'];
  276. $h=$size_src['1'];
  277. $max=$samples;
  278. //指定缩放出来的最大的宽度(也有可能是高度) 根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
  279. if($w > $h){
  280. $w=$max;
  281. $h=$h*($max/$size_src['0']);
  282. }else{
  283. $h=$max;
  284. $w=$w*($max/$size_src['1']);
  285. }
  286. //声明一个$w宽,$h高的真彩图片资源
  287. $image=imagecreatetruecolor($w, $h);
  288. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  289. imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
  290. //告诉浏览器以图片形式解析
  291. ImageJpeg($image,RUNTIME_PATH.'colorindex.jpg',100);
  292. $sm_img=imagecreatefromjpeg(RUNTIME_PATH."colorindex.jpg");
  293. $sm_img_size=getimagesize(RUNTIME_PATH."colorindex.jpg");
  294. //扫描每个像素放到数组,将相近的颜色合并,生成新主要颜色。
  295. $x_num=$sm_img_size[0];
  296. $y_num=$sm_img_size[1];
  297. $Color_list=array();
  298. $i=0;
  299. for($x=0;$x<$x_num;$x++){
  300. for($y=0;$y<$y_num;$y++){
  301. $Color_index=ImageColorAt($sm_img,$x,$y);
  302. $c_arr=ImageColorsForIndex($sm_img,$Color_index);
  303. // 模糊化像素RGB值
  304. $r=self::Probably2($c_arr['red']);
  305. $g=self::Probably2($c_arr['green']);
  306. $b=self::Probably2($c_arr['blue']);
  307. $color_str=$r.','.$g.','.$b;
  308. $Color_list[$i]=$color_str;
  309. $i++;
  310. }
  311. }
  312. $arr=array_count_values($Color_list);
  313. arsort($arr);
  314. $step=1;
  315. $Color_Top=array();
  316. foreach($arr as $k=>$v){
  317. if($step<=$top){
  318. $Color_Top[]=$k;
  319. $step++;
  320. }
  321. }
  322. unlink(RUNTIME_PATH.'colorindex.jpg');
  323. //销毁资源
  324. imagedestroy($image);
  325. imagedestroy($src);
  326. if(is_array($Color_Top)){
  327. return $Color_Top;
  328. }else{
  329. return false;
  330. }
  331. }
  332. //图片的等比缩放 本函数的文件输出方式是选择操作,如果$out_file为空刚是覆盖式操作,支持jpg,png,gif,bmp
  333. static function ImageCompress($src_file,$out_file="",$max='1024',$quality='80'){
  334. //获取图片信息
  335. $type=getimagesize($src_file)['mime'];
  336. //识别图片类型
  337. if($type=='image/jpeg'){
  338. $src=imagecreatefromjpeg($src_file);
  339. }else if($type=='image/png'){
  340. $src=imagecreatefrompng($src_file);
  341. }else if($type=='image/gif'){
  342. $src=imagecreatefromgif($src_file);
  343. }else{
  344. return false;
  345. }
  346. $size_src=getimagesize($src_file);
  347. $w=$size_src['0'];
  348. $h=$size_src['1'];
  349. if(($w>$max)or($h>$max)){
  350. if($w > $h){
  351. $w=$max;
  352. $h=$h*($max/$size_src['0']);
  353. }else{
  354. $h=$max;
  355. $w=$w*($max/$size_src['1']);
  356. }
  357. }
  358. $image=imagecreatetruecolor($w, $h);
  359. imagesavealpha($image, true);
  360. $trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127);
  361. imagefill($image, 0, 0, $trans_colour);
  362. imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
  363. if($out_file==""){
  364. $out_file=$src_file;
  365. }
  366. if($type=='image/jpeg'){
  367. imagejpeg($image,$out_file,$quality);
  368. }else if($type=='image/png'){
  369. imagepng($image,$out_file);
  370. }else if($type=='image/gif'){
  371. imagegif($image,$out_file);
  372. }
  373. //销毁资源
  374. imagedestroy($image);
  375. imagedestroy($src);
  376. }
  377. //图片加水印函数
  378. //$min :图片最小尺寸打水印要求
  379. //$quality :图片保存质量级别
  380. //$fnt :图片水印字体
  381. static function watermark($img_path,$text="",$min=470,$quality=100){
  382. $font = PathDS(APP_PATH.'data'.DS.'font'.DS.'zh.ttf');
  383. $str2=$text;
  384. $str2_w=(strlen($text)+3)*8;
  385. $im=imagecreatefromjpeg($img_path);
  386. $image_info=getimagesize($img_path);
  387. $img_w=$image_info[0];
  388. $img_h=$image_info[1];
  389. //创建颜色,用于文字字体的白和阴影的黑
  390. $white=imagecolorallocate($im,255,255,255);
  391. $black1=imagecolorallocate($im,100,100,100);
  392. $black2=imagecolorallocate($im,223,223,223);
  393. if($img_w>=$min){
  394. $l2=$img_w-$str2_w;
  395. $t2=$img_h-15;
  396. imagettftext($im,18,0, $l2+1, $t2+1, $black1, $font, $str2);
  397. imagettftext($im,18,0, $l2, $t2, $white, $font, $str2);
  398. }elseif($img_h>=$min){
  399. $l2=$img_w-15;
  400. $t2=$str2_w;
  401. imagettftext($im,18,90, $l2+1, $t2+1, $black1, $font, $str2);
  402. imagettftext($im,18,90, $l2, $t2, $white, $font, $str2);
  403. }else{
  404. return false;
  405. }
  406. //将$im输出
  407. ImageJpeg($im,$img_path,$quality);
  408. //销毁$im对象
  409. ImageDestroy($im);
  410. }
  411. /*
  412. 多行自动文字水印(自动居中,换行)
  413. $temp:模板图片
  414. $outimg:输出路径
  415. $title:文字内容
  416. $color:十六进制的颜色
  417. $type:左右定位类型 0:居中 1:左对齐
  418. $top:顶部开始位置
  419. $line_size:每行文字数量
  420. $fontsize:字体大小
  421. $defaultCharWidth:单个字符宽度
  422. */
  423. static function ImageAddText($temp,$outimg,$title,$color,$type,$top,$line_size,$fontsize=20,$defaultCharWidth=26)
  424. {
  425. //字体文件
  426. $font = PathDS(APP_PATH.'data'.DS.'font'.DS.'zh.ttf');
  427. $temp_info=self::getImageInfo($temp);
  428. //加载模板
  429. $Img=imagecreatefromjpeg($temp);
  430. //把十六进制颜色转换成RGB
  431. $color=self::HexToRGB($color);
  432. //创建一个颜色
  433. $black=imagecolorallocate($Img,$color[0],$color[1],$color[2]);
  434. //文字顶部开始位置
  435. $s_top=$top;
  436. //每行文字高度
  437. $line_height=50;
  438. //每行文字数量
  439. $line_size=$line_size;
  440. //总文字数量
  441. $count=mb_strlen($title);
  442. //计算需要多少行显示
  443. $line_count=ceil($count/$line_size);
  444. //开始循环每行打印
  445. for($i=1;$i<=$line_count;$i++){
  446. $strat = ($i - 1)*$line_size;
  447. //开始剪切文字
  448. $txt=mb_substr($title,$strat,$line_size);
  449. //开始判断增涨高度
  450. if($i==1){
  451. $top=$s_top;
  452. }else{
  453. $top=$s_top+($line_height*($i-1));
  454. }
  455. //计算文字宽度
  456. $text_w=$defaultCharWidth*(mb_strlen($txt));
  457. if($type==0){
  458. //计算中心位置
  459. $left=ceil(($temp_info['width'] - $text_w) / 2);
  460. }else{
  461. $left=10;
  462. }
  463. imagettftext($Img,$fontsize,0,$left,$top, $black, $font, $txt);
  464. }
  465. //将$im输出
  466. ImageJpeg($Img,$outimg,80);
  467. //销毁$im对象
  468. ImageDestroy($Img);
  469. }
  470. //获得PSD元数据
  471. static function GetPSDInfo($filename){
  472. $info=array();
  473. $cont=file_get_contents($filename,NULL,NULL,155,17055);
  474. $preg_soft="#<xap:CreatorTool>([^<]*)</xap:CreatorTool>#iUs";
  475. $preg_soft2="#<stEvt:softwareAgent>([^<]*)</stEvt:softwareAgent>#iUs";
  476. $preg_colormode="#<photoshop:ColorMode>(\d*)</photoshop:ColorMode>#iUs";
  477. $preg_resolution="#<tiff:XResolution>(\d*)/(\d*)</tiff:XResolution>#iUs";
  478. $preg_pixel="#<exif:PixelXDimension>(\d*)</exif:PixelXDimension>(\s*)<exif:PixelYDimension>(\d*)</exif:PixelYDimension>#iUs";
  479. preg_match_all($preg_soft,$cont,$arr_soft);
  480. preg_match_all($preg_soft2,$cont,$arr_soft2);
  481. preg_match_all($preg_colormode,$cont,$arr_colormode);
  482. preg_match_all($preg_resolution,$cont,$arr_resolution);
  483. preg_match_all($preg_pixel,$cont,$arr_pixel);
  484. if(isset($arr_soft[1][0])){ $info['CreatorTool']=$arr_soft[1][0];}else{$info['CreatorTool']="";}
  485. if(isset($arr_soft2[1][0])){ $info['softwareAgent']=$arr_soft2[1][0];}else{$info['softwareAgent']="unknown";}
  486. if(isset($arr_colormode[1][0])){ $colormode=$arr_colormode[1][0];}else{ $colormode="10"; }
  487. if((isset($arr_resolution[1][0]))and(isset($arr_resolution[2][0]))){ $info['Resolution']=($arr_resolution[1][0]/$arr_resolution[2][0])."PPI";}else{ $info['Resolution']="unknown";}
  488. if((isset($arr_pixel[1][0]))and(isset($arr_pixel[3][0]))){ $info['PixelX']=$arr_pixel[1][0]; $info['PixelY']=$arr_pixel[3][0];}else{ $info['PixelX']=0; $info['PixelY']=0;}
  489. $cm_arr=array(0=>"Bitmap",1=>"Grayscale",2=>"Indexed",3=>"RGB",4=>"CMYK",5=>"unknown",6=>"unknown",7=>"MultichannelColorMode",8=>"SingleMode",9=>"LabMode",10=>"unknown");
  490. $info['ColorMode']=$cm_arr[$colormode];
  491. return $info;
  492. }
  493. /**
  494. * a.合成图片信息 复制一张图片的矩形区域到另外一张图片的矩形区域
  495. * @param [type] $bg_image [目标图]
  496. * @param [type] $sub_image [被添加图]
  497. * @param [type] $add_x [目标图x坐标位置]
  498. * @param [type] $add_y [目标图y坐标位置]
  499. * @param [type] $add_w [目标图宽度区域]
  500. * @param [type] $add_h [目标图高度区域]
  501. * @param [type] $out_image [输出图路径]
  502. * @return [type] [description]
  503. */
  504. static function ImageCopyImage($bg_image,$sub_image,$add_x,$add_y,$add_w,$add_h,$out_image){
  505. $bg_image_c = imagecreatefromstring(file_get_contents($bg_image));
  506. $sub_image_c = imagecreatefromstring(file_get_contents($sub_image));
  507. imagecopyresampled($bg_image_c, $sub_image_c, $add_x, $add_y, 0, 0, $add_w, $add_h, imagesx($sub_image_c), imagesy($sub_image_c));
  508. imagejpeg($bg_image_c, $out_image, 80);
  509. imagedestroy($sub_image_c);
  510. imagedestroy($bg_image_c);
  511. }
  512. /**
  513. * 处理成圆图片,如果图片不是正方形就取最小边的圆半径,从左边开始剪切成圆形
  514. * @param string $imgpath [description]
  515. * @return [type] [description]
  516. */
  517. static function RoundImg($imgpath) {
  518. $ext = pathinfo($imgpath);
  519. $src_img = null;
  520. //组件支持检测
  521. if(!function_exists('mime_content_type')){
  522. echo '<h1 style="color:#F00;text-align:center;">FastPHP.TOP 系统错误:请安装PHP的fileinfo扩展!</h1>';
  523. exit();
  524. }
  525. $mime=mime_content_type($imgpath);
  526. switch ($mime) {
  527. case 'image/jpeg':
  528. $src_img = imagecreatefromjpeg($imgpath);
  529. break;
  530. case 'image/png':
  531. $src_img = imagecreatefrompng($imgpath);
  532. break;
  533. }
  534. $wh = getimagesize($imgpath);
  535. $w = $wh[0];
  536. $h = $wh[1];
  537. $w = min($w, $h);
  538. $h = $w;
  539. $img = imagecreatetruecolor($w, $h);
  540. //这一句一定要有
  541. imagesavealpha($img, true);
  542. //拾取一个完全透明的颜色,最后一个参数127为全透明
  543. $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
  544. imagefill($img, 0, 0, $bg);
  545. $r = $w / 2; //圆半径
  546. $y_x = $r; //圆心X坐标
  547. $y_y = $r; //圆心Y坐标
  548. for ($x = 0; $x < $w; $x++) {
  549. for ($y = 0; $y < $h; $y++) {
  550. $rgbColor = imagecolorat($src_img, $x, $y);
  551. if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
  552. imagesetpixel($img, $x, $y, $rgbColor);
  553. }
  554. }
  555. }
  556. imagepng($img, $imgpath.'.png', 8);
  557. imagedestroy($img);
  558. }
  559. /**
  560. * 十六进制颜色值转成rgb
  561. * @param [type] $hex [description]
  562. * @return [type] [description]
  563. */
  564. static function HexToRGB($hex) {
  565. $hex = str_replace("#", "", $hex);
  566. if (strlen($hex) == 3) {
  567. $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
  568. $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
  569. $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
  570. } else {
  571. $r = hexdec(substr($hex, 0, 2));
  572. $g = hexdec(substr($hex, 2, 2));
  573. $b = hexdec(substr($hex, 4, 2));
  574. }
  575. return array($r, $g, $b);
  576. }
  577. /**
  578. * rgb颜色值转十六进制
  579. * @param [type] $rgb [description]
  580. * @return [type] [description]
  581. */
  582. static function RGBToHex($rgb) {
  583. $hex = "#";
  584. $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
  585. $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
  586. $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
  587. return $hex;
  588. }
  589. }