123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- <?php
- namespace image;
- use think\Exception;
- class Image
- {
-
- static function getImageInfo($imgfile)
- {
- $imageinfo=getimagesize($imgfile);
- $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');
- if($imageinfo){
- list($width, $height, $type)=$imageinfo;
- if($types[$type]){
- $arr['type']=$types[$type];
- $arr['width']=$width;
- $arr['height']=$height;
- return $arr;
- }else{
- return false;
- }
- }else{
- return false;
- }
- }
-
- static function convert($src,$dst='',$to_ext='jpg',$src_clear=false)
- {
- $info=self::getImageInfo($src);
-
- if($info['type']==$to_ext){
- return $src;
- }
- switch(strtolower($info['type'])){
- case 'jpg':
- case 'jpeg':
- $img=imagecreatefromjpeg($src);
- break;
- case 'gif':
- $img=imagecreatefromgif($src);
- break;
- case 'png':
- $img=imagecreatefrompng($src);
- break;
- case 'bmp':
- $img=imagecreatefrombmp($src);
- break;
- case 'webp':
- $img=imagecreatefromwebp($src);
- break;
- default:
- break;
- }
- if($src_clear){
- \unlink($src);
- }
-
- if($dst==""){
- $file=pathinfo($src);
- $dst=$file['dirname'].DS.$file['filename'].'.'.$to_ext;
- }
- if($img){
- switch($to_ext){
- case 'jpg':
- case 'jpeg':
- imagejpeg($img,$dst);
- return $dst;
- case 'png':
- imagepng($img,$dst);
- return $dst;
- case 'gif';
- imagegif($img,$dst);
- return $dst;
- case 'webp':
- imagewebp($img,$dst);
- return $dst;
- default:
- break;
- }
- }
- return false;
- }
-
- static function getAuthImage($text,$name,$im_x = 160,$im_y = 50) {
- $alpha = 126;
-
- $distortion_im = imagecreatetruecolor($im_x,$im_y);
- imagesavealpha($distortion_im,true);
-
- $tmpC0=mt_rand(100,255);
- $tmpC1=mt_rand(100,255);
- $tmpC2=mt_rand(100,255);
- $buttum_c = imagecolorallocatealpha($distortion_im,$tmpC0,$tmpC1,$tmpC2,$alpha);
-
- imagefill($distortion_im, 0, 0, $buttum_c);
-
- $font = public_path().'static/font/en.ttf';
- for ($i=0;$i<strlen($text);$i++){
- $text_c = imagecolorallocatealpha($distortion_im, mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),0);
- $tmp =substr($text,$i,1);
- $array = array(-1,1);
- $p = array_rand($array);
- $an = $array[$p]*mt_rand(-15,15);
- $size = 28;
- imagettftext($distortion_im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- $count = 100;
- for($i=0; $i<$count; $i++){
- $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
- }
-
- $rand = mt_rand(5,30);
- $rand1 = mt_rand(15,25);
- $rand2 = mt_rand(5,10);
- for ($yy=$rand; $yy<=+$rand+2; $yy++){
- for ($px=-80;$px<=80;$px=$px+0.1){
- $x=$px/$rand1;
- if ($x!=0){
- $y=sin($x);
- }
- $py=$y*$rand2;
- imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
- }
- }
- ob_clean();
- $returnPath = 'uploads/validateImg/'.$name.'.png';
- $savePath = public_path().$returnPath;
- ImagePNG($distortion_im,$savePath);
- ImageDestroy($distortion_im);
-
-
- return $returnPath;
- }
-
-
- static function getAuthImage2($text,$im_x = 160,$im_y = 50)
- {
- $alpha = 126;
-
- $distortion_im = imagecreatetruecolor($im_x,$im_y);
- imagesavealpha($distortion_im,true);
-
- $tmpC0=mt_rand(100,255);
- $tmpC1=mt_rand(100,255);
- $tmpC2=mt_rand(100,255);
- $buttum_c = imagecolorallocatealpha($distortion_im,$tmpC0,$tmpC1,$tmpC2,$alpha);
-
- imagefill($distortion_im, 0, 0, $buttum_c);
-
- $font = public_path().'static/font/en.ttf';
- for ($i=0;$i<strlen($text);$i++){
- $text_c = imagecolorallocatealpha($distortion_im, mt_rand(0,200),mt_rand(0,200),mt_rand(0,200),0);
- $tmp =substr($text,$i,1);
- $array = array(-1,1);
- $p = array_rand($array);
- $an = $array[$p]*mt_rand(-15,15);
- $size = 28;
- imagettftext($distortion_im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);
- }
-
- $count = 100;
- for($i=0; $i<$count; $i++){
- $randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
- imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
- }
-
- $rand = mt_rand(5,30);
- $rand1 = mt_rand(15,25);
- $rand2 = mt_rand(5,10);
- for ($yy=$rand; $yy<=+$rand+2; $yy++){
- for ($px=-80;$px<=80;$px=$px+0.1){
- $x=$px/$rand1;
- if ($x!=0){
- $y=sin($x);
- }
- $py=$y*$rand2;
- imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);
- }
- }
- ob_clean();
- imagepng($distortion_im);
- $content = ob_get_clean();
- ImageDestroy($distortion_im);
- return $content;
- }
-
- static function SaveBase64Img($data){
- $dir = DS.'uploads'.DS.'images'.DS.date('Ymd',time()).DS;
- $savepath = ROOT_PATH.$dir;
- if(!file_exists($savepath)){
- mkdir($savepath,0777,true);
- }
- $filename = time().GetRandStr(10).'.jpg';
- $data = substr($data,22);
- $tmp = base64_decode($data);
- file_put_contents($savepath.DS.$filename,$tmp);
- return $dir.$filename;
- }
-
- static function Probably($int){
- $len=strlen($int);
- if($len==1){
- return 0;
- }elseif($len==2 or $len==3){
- return round($int/10)."0";
- }else{
- return false;
- }
- }
-
- static function Probably2($int){
- $ints=array(
- 0=>array(0,29),
- 30=>array(30,59),
- 60=>array(60,89),
- 90=>array(90,119),
- 120=>array(120,149),
- 150=>array(150,179),
- 180=>array(180,209),
- 210=>array(210,239),
- 240=>array(240,256),
- );
- foreach($ints as $k=>$v){
- $min=$v[0];
- $max=$v[1];
- if(($int>=$min)and($int<=$max)){
- return $k;
- }
- }
- }
-
- static function GetImgColorList($src_file,$samples=50,$top=20){
-
- $src=imagecreatefromjpeg($src_file);
-
- $size_src=getimagesize($src_file);
- $w=$size_src['0'];
- $h=$size_src['1'];
- $max=$samples;
-
- if($w > $h){
- $w=$max;
- $h=$h*($max/$size_src['0']);
- }else{
- $h=$max;
- $w=$w*($max/$size_src['1']);
- }
-
- $image=imagecreatetruecolor($w, $h);
-
- imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
-
- ImageJpeg($image,RUNTIME_PATH.'colorindex.jpg',100);
- $sm_img=imagecreatefromjpeg(RUNTIME_PATH."colorindex.jpg");
- $sm_img_size=getimagesize(RUNTIME_PATH."colorindex.jpg");
-
- $x_num=$sm_img_size[0];
- $y_num=$sm_img_size[1];
- $Color_list=array();
- $i=0;
- for($x=0;$x<$x_num;$x++){
- for($y=0;$y<$y_num;$y++){
- $Color_index=ImageColorAt($sm_img,$x,$y);
- $c_arr=ImageColorsForIndex($sm_img,$Color_index);
-
- $r=self::Probably2($c_arr['red']);
- $g=self::Probably2($c_arr['green']);
- $b=self::Probably2($c_arr['blue']);
- $color_str=$r.','.$g.','.$b;
- $Color_list[$i]=$color_str;
- $i++;
- }
- }
- $arr=array_count_values($Color_list);
- arsort($arr);
- $step=1;
- $Color_Top=array();
- foreach($arr as $k=>$v){
- if($step<=$top){
- $Color_Top[]=$k;
- $step++;
- }
- }
- unlink(RUNTIME_PATH.'colorindex.jpg');
-
- imagedestroy($image);
- imagedestroy($src);
- if(is_array($Color_Top)){
- return $Color_Top;
- }else{
- return false;
- }
- }
-
-
- static function ImageCompress($src_file,$out_file="",$max='1024',$quality='80'){
-
-
- $type=getimagesize($src_file)['mime'];
-
- if($type=='image/jpeg'){
- $src=imagecreatefromjpeg($src_file);
- }else if($type=='image/png'){
- $src=imagecreatefrompng($src_file);
- }else if($type=='image/gif'){
- $src=imagecreatefromgif($src_file);
- }else{
- return false;
- }
-
- $size_src=getimagesize($src_file);
- $w=$size_src['0'];
- $h=$size_src['1'];
- if(($w>$max)or($h>$max)){
- if($w > $h){
- $w=$max;
- $h=$h*($max/$size_src['0']);
- }else{
- $h=$max;
- $w=$w*($max/$size_src['1']);
- }
- }
- $image=imagecreatetruecolor($w, $h);
- imagesavealpha($image, true);
- $trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127);
- imagefill($image, 0, 0, $trans_colour);
- imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
- if($out_file==""){
- $out_file=$src_file;
- }
- if($type=='image/jpeg'){
- imagejpeg($image,$out_file,$quality);
- }else if($type=='image/png'){
- imagepng($image,$out_file);
- }else if($type=='image/gif'){
- imagegif($image,$out_file);
- }
-
-
- imagedestroy($image);
- imagedestroy($src);
- }
-
-
-
-
- static function watermark($img_path,$text="",$min=470,$quality=100){
- $font = PathDS(APP_PATH.'data'.DS.'font'.DS.'zh.ttf');
- $str2=$text;
- $str2_w=(strlen($text)+3)*8;
- $im=imagecreatefromjpeg($img_path);
- $image_info=getimagesize($img_path);
- $img_w=$image_info[0];
- $img_h=$image_info[1];
-
- $white=imagecolorallocate($im,255,255,255);
- $black1=imagecolorallocate($im,100,100,100);
- $black2=imagecolorallocate($im,223,223,223);
- if($img_w>=$min){
- $l2=$img_w-$str2_w;
- $t2=$img_h-15;
- imagettftext($im,18,0, $l2+1, $t2+1, $black1, $font, $str2);
- imagettftext($im,18,0, $l2, $t2, $white, $font, $str2);
- }elseif($img_h>=$min){
- $l2=$img_w-15;
- $t2=$str2_w;
- imagettftext($im,18,90, $l2+1, $t2+1, $black1, $font, $str2);
- imagettftext($im,18,90, $l2, $t2, $white, $font, $str2);
- }else{
- return false;
- }
-
- ImageJpeg($im,$img_path,$quality);
-
- ImageDestroy($im);
- }
-
- static function ImageAddText($temp,$outimg,$title,$color,$type,$top,$line_size,$fontsize=20,$defaultCharWidth=26)
- {
-
- $font = PathDS(APP_PATH.'data'.DS.'font'.DS.'zh.ttf');
- $temp_info=self::getImageInfo($temp);
-
- $Img=imagecreatefromjpeg($temp);
-
- $color=self::HexToRGB($color);
-
- $black=imagecolorallocate($Img,$color[0],$color[1],$color[2]);
-
- $s_top=$top;
-
- $line_height=50;
-
- $line_size=$line_size;
-
- $count=mb_strlen($title);
-
- $line_count=ceil($count/$line_size);
-
- for($i=1;$i<=$line_count;$i++){
- $strat = ($i - 1)*$line_size;
-
- $txt=mb_substr($title,$strat,$line_size);
-
- if($i==1){
- $top=$s_top;
- }else{
- $top=$s_top+($line_height*($i-1));
- }
-
- $text_w=$defaultCharWidth*(mb_strlen($txt));
- if($type==0){
-
- $left=ceil(($temp_info['width'] - $text_w) / 2);
- }else{
- $left=10;
- }
- imagettftext($Img,$fontsize,0,$left,$top, $black, $font, $txt);
- }
-
- ImageJpeg($Img,$outimg,80);
-
- ImageDestroy($Img);
- }
-
- static function GetPSDInfo($filename){
- $info=array();
- $cont=file_get_contents($filename,NULL,NULL,155,17055);
- $preg_soft="#<xap:CreatorTool>([^<]*)</xap:CreatorTool>#iUs";
- $preg_soft2="#<stEvt:softwareAgent>([^<]*)</stEvt:softwareAgent>#iUs";
- $preg_colormode="#<photoshop:ColorMode>(\d*)</photoshop:ColorMode>#iUs";
- $preg_resolution="#<tiff:XResolution>(\d*)/(\d*)</tiff:XResolution>#iUs";
- $preg_pixel="#<exif:PixelXDimension>(\d*)</exif:PixelXDimension>(\s*)<exif:PixelYDimension>(\d*)</exif:PixelYDimension>#iUs";
- preg_match_all($preg_soft,$cont,$arr_soft);
- preg_match_all($preg_soft2,$cont,$arr_soft2);
- preg_match_all($preg_colormode,$cont,$arr_colormode);
- preg_match_all($preg_resolution,$cont,$arr_resolution);
- preg_match_all($preg_pixel,$cont,$arr_pixel);
- if(isset($arr_soft[1][0])){ $info['CreatorTool']=$arr_soft[1][0];}else{$info['CreatorTool']="";}
- if(isset($arr_soft2[1][0])){ $info['softwareAgent']=$arr_soft2[1][0];}else{$info['softwareAgent']="unknown";}
- if(isset($arr_colormode[1][0])){ $colormode=$arr_colormode[1][0];}else{ $colormode="10"; }
- 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";}
- 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;}
- $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");
- $info['ColorMode']=$cm_arr[$colormode];
- return $info;
- }
-
- static function ImageCopyImage($bg_image,$sub_image,$add_x,$add_y,$add_w,$add_h,$out_image){
- $bg_image_c = imagecreatefromstring(file_get_contents($bg_image));
- $sub_image_c = imagecreatefromstring(file_get_contents($sub_image));
- 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));
- imagejpeg($bg_image_c, $out_image, 80);
- imagedestroy($sub_image_c);
- imagedestroy($bg_image_c);
- }
-
- static function RoundImg($imgpath) {
- $ext = pathinfo($imgpath);
- $src_img = null;
-
- if(!function_exists('mime_content_type')){
- echo '<h1 style="color:#F00;text-align:center;">FastPHP.TOP 系统错误:请安装PHP的fileinfo扩展!</h1>';
- exit();
- }
- $mime=mime_content_type($imgpath);
- switch ($mime) {
- case 'image/jpeg':
- $src_img = imagecreatefromjpeg($imgpath);
- break;
- case 'image/png':
- $src_img = imagecreatefrompng($imgpath);
- break;
- }
- $wh = getimagesize($imgpath);
- $w = $wh[0];
- $h = $wh[1];
- $w = min($w, $h);
- $h = $w;
- $img = imagecreatetruecolor($w, $h);
-
- imagesavealpha($img, true);
-
- $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
- imagefill($img, 0, 0, $bg);
- $r = $w / 2;
- $y_x = $r;
- $y_y = $r;
- for ($x = 0; $x < $w; $x++) {
- for ($y = 0; $y < $h; $y++) {
- $rgbColor = imagecolorat($src_img, $x, $y);
- if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
- imagesetpixel($img, $x, $y, $rgbColor);
- }
- }
- }
- imagepng($img, $imgpath.'.png', 8);
- imagedestroy($img);
- }
-
- static function HexToRGB($hex) {
- $hex = str_replace("#", "", $hex);
- if (strlen($hex) == 3) {
- $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
- $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
- $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
- } else {
- $r = hexdec(substr($hex, 0, 2));
- $g = hexdec(substr($hex, 2, 2));
- $b = hexdec(substr($hex, 4, 2));
- }
- return array($r, $g, $b);
- }
-
- static function RGBToHex($rgb) {
- $hex = "#";
- $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
- $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
- $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
- return $hex;
- }
- }
|