1 <?php 2 /*如何知道图片的类型和大 3 * 利用getimagesize(),获得以下属性 4 Array 5 ( 6 [0] => 533 //宽 7 [1] => 300 //高 8 [2] => 2 //图片类型 jpg 9 [3] => width="533" height="300" 10 [bits] => 8 11 [channels] => 3 12 [mime] => image/jpeg 13 ) 14 * 15 * 16 $arr = getimagesize('./sanyecao.jpg'); 17 echo image_type_to_mime_type($arr[2]),'类型<br/>'; 18 print_r($arr); 19 */ 20 //先获得图片的信息 21 //水印,把制定图片复制到目标上,并加上透明效果 22 //缩略图:把大尺寸图片复制到小尺寸图片上 23 class ImageTool{ 24 //image info 分析图片的信息,返回信息数组 25 public static function imageInfo($image){ 26 //判断文件是否存在 27 if(!file_exists($image)){ 28 return false; 29 } 30 $info = getimagesize($image); 31 if($info == false){ 32 return false; 33 } 34 $img['width'] = $info['0']; 35 $img['height'] = $info['1']; 36 $img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1); 37 //print_r($img); 38 return $img; 39 } 40 //加水印 41 //string $dst 带操作图片 42 //$wter 水印图片 43 //$savepath 保存路径 不填则默认替换原始图 44 //$alpha 水印透明度 45 //$pos 水印位置 0:左上角 1:右上角 2:左下角 3:右下角 46 public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50){ 47 if(!file_exists($dst) || !file_exists($water)){ 48 echo '文件不存在'; 49 return false; 50 } 51 //保证水印不能比带操作图片大 52 $dinfo = self::ImageInfo($dst); //原始图片信息 53 $winfo = self::ImageInfo($water); //水印图片信息 54 55 //水印图片不能大于操作图片 56 if($winfo['height']>$dinfo['height'] || $winfo['width']>$dinfo['width']){ 57 return false; 58 } 59 //加水印,把两张图都读到画布上 根据类型创建画布 动态加载函数 60 $dfunc = 'imagecreatefrom'.$dinfo['ext']; 61 $wfunc = 'imagecreatefrom'.$winfo['ext']; 62 if(!function_exists($dfunc) || !function_exists($wfunc)){ 63 return false; 64 } 65 //动态加载函数来创建画布 66 $dim = $dfunc($dst); //创建带操作画布 67 $wim = $wfunc($water); //创建水印画布 68 //根据水印位置计算粘贴坐标 69 switch($pos){ 70 case 0: //左上角 71 $posx = 0; 72 $posy = 0; 73 break; 74 case 1: //右上角 75 $posx = $dinfo['width']-$winfo['width']; 76 $posy = 0; 77 break; 78 case 3: //左下角 79 $posx = $dinfo['width']-$winfo['width']; 80 $posy = $dinfo['height']-$winfo['height']; 81 break; 82 case 2: 83 default: //默认右下角 84 $posx = 0; 85 $posy = $dinfo['height']-$winfo['height']; 86 break; 87 } 88 //正式加水印 89 imagecopymerge($dim,$wim,$posx ,$posy ,0 ,0 ,$winfo['width'],$winfo['height'],$alpha); 90 //保存图片 91 if(!$save){ 92 $save = $dst; 93 unlink($dst); //删除原图 94 } 95 $createfunc = 'image'.$dinfo['ext']; 96 $createfunc($dim,$save); 97 imagedestroy($dim); 98 imagedestroy($wim); 99 return true; 100 } 101 //生成缩略图 102 // 103 // 104 public static function thumb($dst,$save=NULL,$widht=200,$height=200) 105 { 106 $dinfo = self::ImageInfo($dst); 107 //var_dump(self::ImageInfo($dst)); 108 //var_dump($dinfo); 109 //判断待处理图片是否存在 110 if($dinfo == false){ 111 return false; 112 } 113 //处理缩放 114 //计算缩放比例 115 // 116 $calc = min($widht/$dinfo['width'],$height/$dinfo['height']); 117 118 //创建画布 119 $dfunc = 'imagecreatefrom'.$dinfo['ext']; 120 //echo $dfunc; 121 //动态加载函数来创建画布 122 $dim = $dfunc($dst); //创建带操作画布 123 //创建缩略画布 124 $tim = imagecreatetruecolor($widht,$height); 125 $white = imagecolorallocate($tim,255,255,255); 126 //填充缩略画布 127 imagefill($tim,0,0,$white); 128 $dwidht = $dinfo['width']*$calc; 129 $dheight = $dinfo['height']*$calc; 130 131 //留白计算 132 $paddingx = (int)($widht - $dwidht)/2; 133 $paddingy = (int)($height - $dheight)/2; 134 //复制并缩略 135 imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidht,$dheight,$dinfo['width'],$dinfo['height']); 136 //保存图片 137 if(!$save){ 138 $save = $dst; 139 unlink($dst); 140 } 141 $createfunc = 'image'.$dinfo['ext']; 142 $createfunc($tim,$save); 143 imagedestroy($dim); 144 imagedestroy($tim); 145 return true; 146 } 147 //生成验证码 148 public static function captcha($width=55,$height=28){ 149 //造化布 150 $image = imagecreatetruecolor($width,$height); 151 $d_image = imagecreatetruecolor($width,$height); 152 //造背景色 153 $gray = imagecolorallocate($image,200,200,200); 154 $d_gray = imagecolorallocate($d_image,200,200,200); 155 //填充背景色 156 imagefill($image,0,0,$gray); 157 imagefill($d_image,0,0,$d_gray); 158 //造随机字体颜色 159 $color = imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125)); 160 //造随机线条颜色 161 $color1 = imagecolorallocate($image,mt_rand(100,125),mt_rand(100,125),mt_rand(0,125)); 162 $color2 = imagecolorallocate($image,mt_rand(100,125),mt_rand(100,125),mt_rand(0,125)); 163 $color3 = imagecolorallocate($image,mt_rand(100,125),mt_rand(100,125),mt_rand(0,125)); 164 //在画布上画线 165 imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),$color1); 166 imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),$color2); 167 imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),$color3); 168 169 //在画布上写字 170 $text = substr(str_shuffle('abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ23456789'),0,4); 171 imagestring($image,5,7,5,$text,$color); 172 173 for($i = 0; $i<60; $i++){ 174 $offset = 3; //最大波动频率 175 $round = 2; //两个周期 176 $posy =round( SIN($i*$round*2*M_PI/60)*$offset); 177 imagecopy($d_image,$image,$i,$posy,$i,0,1,25); 178 } 179 //显示,销毁 180 ob_clean(); 181 header('content-type:image/jpeg'); 182 imagejpeg($d_image); 183 imagedestroy($d_image); 184 imagedestroy($image); 185 186 return $text; 187 } 188 } 189 //print_r(ImageTool::imageInfo('./sanyecao.jpg')); 190 //生成水印图 191 /* 192 var_dump(ImageTool::water('./sanyecao.jpg','./xiao.png','./aaa1.jpg',0)); 193 var_dump(ImageTool::water('./sanyecao.jpg','./xiao.png','./aaa2.jpg',1)); 194 var_dump(ImageTool::water('./sanyecao.jpg','./xiao.png','./aaa3.jpg',2)); 195 var_dump(ImageTool::water('./sanyecao.jpg','./xiao.png','./aaa4.jpg',3)); 196 */ 197 /* 198 生成留白的等比例缩略图 199 */ 200 /* 201 var_dump(ImageTool::thumb('./sanyecao.jpg','./sanyecao1.jpg',200,200)); 202 var_dump(ImageTool::thumb('./sanyecao.jpg','./sanyecao2.jpg',200,300)); 203 var_dump(ImageTool::thumb('./sanyecao.jpg','./sanyecao3.jpg',300,200)); 204 */ 205 //生成验证码 206 $tool = new ImageTool; 207 $aa = $tool -> captcha(); 208 var_dump($aa); 209 ?>