PHP碎码——自己写的验证码
其实里面没必要封装函数,只是当时觉得视觉上好看而已,结构清晰点
<?phpclasscaptcha{//验证码-字符串private$codes;//图片长度private$img_length=150;//图片高度private$img_height=30;//字符列表,用以生成随机验证码private$charlist='1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';//随机码的个数private$code_num=4;//字体大小--初始化时算出的private$font_size;//干扰线数目private$line_num=5;//干扰雪花数目private$sterisk_num=50;//验证码--图片private$img;//字体文件路径private$ttf='./instance/font/Elephant.ttf';publicfunction__construct(){//字体大小通过图片宽高动态生成的,但感觉不太完美$this->font_size=($this->img_height*2/5>$this->img_height*4/5?$this->img_height*4/5:$this->img_height*2/5);}publicfunctionrun(){//创建图片资源$this->createImage();//往图片中添加雪花$this->addaSterisk();//往图片中添加字符$this->addfont();//往图片中添加线条$this->addLine();//将图片输出至浏览器$this->outputImg();}//返回验证码字符串publicfunctiongetCode(){return$this->codes;}//创建图片资源privatefunctioncreateImage(){//创建图片资源$this->img=p_w_picpathcreatetruecolor($this->img_length,$this->img_height);//创建颜色$color_bg=p_w_picpathcolorallocate($this->img,mt_rand(210,255),mt_rand(210,255),mt_rand(210,255));//设置图片背景色p_w_picpathfill($this->img,0,0,$color_bg);}//往图片中添加线条privatefunctionaddLine(){//添加指定数量的线条for($i=0;$i<$this->line_num;$i++){//创建随机颜色--参数(图片资源,R,B,G)$color_line=p_w_picpathcolorallocate($this->img,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));//添加线条,位置随机--参数(图片资源,起点-x,起点-y,终点-x,终点-y,颜色)//不可调整//p_w_picpathline($this->img,mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),$color_line);//可以调整线条的粗细$src_x=mt_rand(0,$this->img_length);$src_y=mt_rand(0,$this->img_height);$dest_x=mt_rand(0,$this->img_length);$dest_y=mt_rand(0,$this->img_height);for($j=0;$j<1;$j++){p_w_picpathline($this->img,$src_x+$j,$src_y+$j,$dest_x+$j,$dest_y+$j,$color_line);}}}//往图片中添加雪花privatefunctionaddaSterisk(){//添加指定数量的雪花for($i=0;$i<$this->sterisk_num;$i++){//创建随机颜色--参数(图片资源,R,B,G)$color_Ster=p_w_picpathcolorallocate($this->img,mt_rand(220,255),mt_rand(220,255),mt_rand(220,255));//添加雪花,位置随机--参数(图片资源,倾斜角度,左下角-x,左下角-y,颜色,字符串)p_w_picpathstring($this->img,mt_rand(0,360),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),'*',$color_Ster);}}privatefunctionaddfont(){for($i=0;$i<$this->code_num;$i++){//随机从字符列表中取一个字符$code=substr(str_shuffle($this->charlist),-1);//记录到验证码字符串中$this->codes.=$code;//创建随机颜色--参数(图片资源,R,B,G)$color_font=p_w_picpathcolorallocate($this->img,mt_rand(10,180),mt_rand(10,180),mt_rand(10,180));//添加雪花,位置随机--参数(图片资源,字体大小,倾斜角度,左下角-x,左下角-y,字体颜色,字体,字符串)//左下角-y,字体的基准高度是估计的,由于字体大小使用磅,不同字符的长宽像素相差甚大p_w_picpathttftext($this->img,$this->font_size,mt_rand(-30,30),($this->img_length/$this->code_num)*$i+mt_rand(1,$this->font_size*0.2),$this->img_height*0.7+mt_rand(-$this->img_height*0.2,$this->img_height*0.2),$color_font,$this->ttf,$code);}}//输出图片至浏览器privatefunctionoutputImg(){//通知浏览器是png格式header('Content-type:p_w_picpath/png');//以png格式输出p_w_picpathpng($this->img);//销毁内存中的图片资源p_w_picpathdestroy($this->img);}publicfunction__set($key,$value){}publicfunction__get($value){}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。