有获得图片的宽,高,后缀的函数,有等比例缩略图的函数,有添加水印的函数,有产生验证码的函数
<?php defined('ACC')||exit('无权访问'); //图片处理类 class ImageTool{ //imageInfo分析图片的信息 //return array public static function imageInfo($image){ //判断图片是否存在 if(!file_exists($image)){ return false; } $info=getimagesize($image); if($info==false){ return false; } $img['width']=$info[0]; $img['height']=$info[1]; $img['ext']=substr($info['mime'],strpos($info['mime'],'/')+1); return $img; } //加水印功能 //water(原图,水印图,替换原图或新图路径,水印位置,透明度); public static function water($dst,$water,$save=null,$pos=2,$alpha=50){ //是否存在 if(!file_exists($dst)||!file_exists($water)){ return false; } //图片信息 $dinfo=self::imageInfo($dst); $winfo=self::imageInfo($water); if($winfo['height']>$dinfo['height']||$winfo['width']>$dinfo['width']){ return false; } //获得函数名 $dfunc='imagecreatefrom'.$dinfo['ext']; $wfunc='imagecreatefrom'.$winfo['ext']; //判断方法是否存在 if(!function_exists($dfunc)||!function_exists($wfunc)){ return false; } //创建画布 $dim=$dfunc($dst); $wim=$wfunc($water); //计算水印位置坐标 switch($pos){ case 0://左上角 $posx=0; $posy=0; break; case 1://右上角 $posx=$dinfo['width']-$winfo['width']; $posy=0; break; case 2://左下角 $posx=0; $posy=$dinfo['height']-$winfo['height']; break; default://右下角 $posx=$dinfo['width']-$winfo['width']; $posy=$dinfo['height']-$winfo['height']; } imagecopymerge($dim,$wim,$posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha); //保存 if(!$save){ $save=$dst; unlink($dst);//删除原图 } $createfunc='image'.$dinfo['ext']; $createfunc($dim,$save); imagedestroy($dim); imagedestroy($wim); return true; } //thumb生成缩略图,等比缩放,留白 public static function thumb($dst,$save=null,$width=200,$height=200){ //判断原图是否存在 $dinfo=self::imageInfo($dst); if($dinfo==false){ return false; } //计算缩放比例 $calc=min($width/$dinfo['width'],$height/$dinfo['height']); //创建原始图画布 $dfunc='imagecreatefrom'.$dinfo['ext']; $dim=$dfunc($dst); //创建缩略图画布 $tim=imagecreatetruecolor($width,$height); //创建白色 $white=imagecolorallocate($tim,255,255,255); imagefill($tim,0,0,$white); //复制并缩略 $dwidth=(int)$dinfo['width']*$calc; $dheight=(int)$dinfo['height']*$calc; $paddingx=(int)($width-$dwidth)/2; $paddingy=(int)($height-$dheight)/2; imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']); //保存 if(!$save){ $save=$dst; unlink($dst); } $createfunc='image'.$dinfo['ext']; $createfunc($tim,$save); imagedestroy($tim); imagedestroy($dim); return true; } public static function code($width=50,$height=30){ $rs=imagecreatetruecolor($width,$height);//创建画布资源 $gray=imagecolorallocate($rs, 100, 100, 100); $fontcolor=imagecolorallocate($rs, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255)); $linecolor1=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200)); $linecolor2=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200)); $linecolor3=imagecolorallocate($rs, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200)); //填充颜色 imagefill($rs, 0, 0, $gray); //随机字符串 $str=substr(str_shuffle('ABCDEFGHJKMNOPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789'),0,4); //书写字符串 imagestring($rs, 5, 8, 5, $str, $fontcolor); //画干扰线 imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor1); imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor2); imageline($rs,0,mt_rand(0,30),50,mt_rand(0,30),$linecolor3); //输出 header('content-type:image/png'); imagepng($rs); //销毁 imagedestroy($rs); return $str; } } ?>