PHP图片类(验证码,水印,缩略)

jopen 11年前

class image{    static $errno=null;    static $error=array(     '1'=>'原图或者水印不存在',     '2'=>'水印图片宽高不能大于原图',     '3'=>'原图无法创建画布',     '4'=>'水印图无法创建画布',     '5'=>'加水印失败',     '6'=>'原图加水印的效果图无法创建画布',     '7'=>'保存水印效果图失败',     '8'=>'要缩略的图片不存在',     '9'=>'缩略的图片宽高不能小于效果图的宽高',     '10'=>'缩略图的画布无法创建',     '11'=>'缩略失败',        '12'=>'生成缩略效果图片文件失败'    );    /*     水印     所用主要函数imagestring    int $strnum 验证码中字符串的个数    int $width 验证码区域的宽度    int $height 验证码区域的高度    */    static function verify($strnum,$width,$height){     $im=imagecreatetruecolor($width,$height);     $bgcolor=imagecolorallocate($im,200,200,200);     imagefill($im,0,0,$bgcolor);     $str='abcdefghijkmnpqrstuvwxyz23456789';     $str=substr(str_shuffle($str),0,$strnum);//获取随机字符串     for($i=0,$j=$width/$strnum;$i<$strnum;$i++){      $strcolor=imagecolorallocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));      imagestring($im,5,$j*$i+5,mt_rand(1,$height/4),$str[$i],$strcolor);     }     header('Content-type:image/png');     imagepng($im);     imdestroy($im);    }    /*     水印     函数:imagecopymerge     string dst 要加水印的图片(目标图)     string src 用来当做水印的图片(水印图)     string savename 加水印后的效果图     int $postion 水印位置 0左上角 1右上角 2右下角 3左下角     int $pct 透明度      */      static function water($dst,$src,$savename='',$postion=2,$pct=50){     if(!file_exists($dst)||!file_exists($src)){      self::$errno=1;      return false;     }     $dstinfo=self::getImgInfo($dst);     $srcinfo=self::getImgInfo($src);     if(($dstinfo['width']<$srcinfo['width'])||($dstinfo['height']<$srcinfo['height'])){      self::$errno=2;      return false;     }     $dstcreatefunc='imagecreatefrom'.(($dstinfo['type']=='jpg')?'jpeg':$dstinfo['type']);     if(!function_exists($dstcreatefunc)){      self::$errno=3;      return false;     }     $srccreatefunc='imagecreatefrom'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);     if(!function_exists($srccreatefunc)){      self::$errno=4;      return false;     }     $dstimg=$dstcreatefunc($dst);//创建目标图画布资源     $srcimg=$srccreatefunc($src);//创建水印图画布资源           switch($postion){//判断位置,确定放到目标图的位置坐标      case 0:       $dst_x=0;       $dst_y=0;       break;      case 1:       $dst_x=$dstinfo['width']-$srcinfo['width'];       $dst_y=0;       break;      case 3:       $dst_x=0;          $dst_y=$dstinfo['height']-$srcinfo['height'];       break;      default:       $dst_x=$dstinfo['width']-$srcinfo['width'];       $dst_y=$dstinfo['height']-$srcinfo['height'];     }       if(!imagecopymerge($dstimg,$srcimg,$dst_x,$dst_y,0,0,$srcinfo['width'],$srcinfo['height'],$pct)){      self::$errno=5;      return false;     }     if(!$savename){        $savename=$dst;        unlink($dst);     }     $dstimgfun='image'.(($dstinfo['type']=='jpg')?'jpeg':$dstinfo['type']);     if(!function_exists($dstimgfun)){      self::$errno=6;      return false;     }     if(!$dstimgfun($dstimg,$savename)){      self::$errno=7;      return false;     }     return true;    }          //获取图片的长,宽,类别    static function getImgInfo($image){     $imginfo=array();     $img=getimagesize($image);     $type=explode('/',$img['mime']);     $type=end($type);     $imginfo['width']=$img[0];     $imginfo['height']=$img[1];     $imginfo['type']=$type;     return $imginfo;    }      /*    string src 要缩略的图(原图)    int width 缩略后的小图宽度    int height 缩略后的小图高度    string savename 缩略效果图的保存路径    !!两边留白    所用函数imagecopyresampled    */      static function thumb($src,$savename='',$width=200,$height=200){     if(!file_exists($src)){       self::$errno=8;       return false;     }     $srcinfo=self::getImgInfo($src);     if(($srcinfo['width']<$width)&&($srcinfo['height']<$height)){//判断原图和缩略图的宽高大小       self::$errno=9;       return false;     }       $dst=imagecreatetruecolor($width,$height);     $white=imagecolorallocate($dst,255,255,255);     imagefill($dst,0,0,$white);//创建缩略图画布,以白色填充          $srccreatefunc='imagecreatefrom'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);     if(!function_exists($srccreatefunc)){      self::$errno=3;      return false;     }     $srcimg=$srccreatefunc($src);//创建原图画布       $p=min($width/$srcinfo['width'],$height/$srcinfo['height']);//确定缩放比例     $dst_w=$p*$srcinfo['width'];//确定原图缩略后的宽度      $dst_h=$p*$srcinfo['height'];//确定原图缩略后的高度     $dst_x=($width-$dst_w)/2;//确定原图缩略后的小图放在缩略图位置的x坐标     $dst_y=($height-$dst_h)/2;//确定原图缩略后的小图放在缩略图位置y坐标     if(!imagecopyresampled($dst,$srcimg,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$srcinfo['width'],$srcinfo['height'])){      self::$errno=11;      return false;     }       $imgfunc='image'.(($srcinfo['type']=='jpg')?'jpeg':$srcinfo['type']);     if(!function_exists($imgfunc)){      self::$errno=10;      return false;     }       if(!$savename){      $savename=$src;      unlink($src);     }     if(!$imgfunc($dst,$savename)){      self::$errno=12;      return false;     }     return true;    }      }