标签:php
其实里面没必要封装函数,只是当时觉得视觉上好看而已,结构清晰点
<?php class captcha{ //验证码-字符串 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‘; public function __construct(){ //字体大小通过图片宽高动态生成的,但感觉不太完美 $this->font_size = ($this->img_height*2/5 > $this->img_height*4/5 ? $this->img_height*4/5 : $this->img_height*2/5); } public function run(){ //创建图片资源 $this->createImage(); //往图片中添加雪花 $this->addaSterisk(); //往图片中添加字符 $this->addfont(); //往图片中添加线条 $this->addLine(); //将图片输出至浏览器 $this->outputImg(); } //返回验证码字符串 public function getCode(){ return $this->codes; } //创建图片资源 private function createImage(){ //创建图片资源 $this->img = imagecreatetruecolor($this->img_length,$this->img_height); //创建颜色 $color_bg = imagecolorallocate($this->img, mt_rand(210, 255), mt_rand(210, 255), mt_rand(210, 255)); //设置图片背景色 imagefill($this->img, 0, 0, $color_bg); } //往图片中添加线条 private function addLine(){ //添加指定数量的线条 for ($i = 0; $i < $this->line_num; $i++) { //创建随机颜色--参数(图片资源,R,B,G) $color_line = imagecolorallocate($this->img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200)); //添加线条,位置随机--参数(图片资源,起点-x,起点-y,终点-x,终点-y,颜色) //不可调整 //imageline($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++) { imageline($this->img, $src_x+$j, $src_y+$j, $dest_x+$j,$dest_y+$j, $color_line); } } } //往图片中添加雪花 private function addaSterisk(){ //添加指定数量的雪花 for ($i = 0; $i < $this->sterisk_num; $i++) { //创建随机颜色--参数(图片资源,R,B,G) $color_Ster = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); //添加雪花,位置随机--参数(图片资源,倾斜角度,左下角-x,左下角-y,颜色,字符串) imagestring($this->img,mt_rand(0,360),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),‘*‘,$color_Ster); } } private function addfont(){ for ($i = 0; $i < $this->code_num; $i++) { //随机从字符列表中取一个字符 $code = substr(str_shuffle($this->charlist),-1); //记录到验证码字符串中 $this->codes .= $code; //创建随机颜色--参数(图片资源,R,B,G) $color_font = imagecolorallocate($this->img, mt_rand(10, 180), mt_rand(10, 180), mt_rand(10, 180)); //添加雪花,位置随机--参数(图片资源,字体大小,倾斜角度,左下角-x,左下角-y,字体颜色,字体,字符串) // 左下角-y,字体的基准高度是估计的,由于字体大小使用磅,不同字符的长宽像素相差甚大 imagettftext($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); } } //输出图片至浏览器 private function outputImg(){ //通知浏览器是png格式 header(‘Content-type:image/png‘); //以png格式输出 imagepng($this->img); //销毁内存中的图片资源 imagedestroy($this->img); } public function __set($key,$value){ } public function __get($value){ } }
标签:php
原文地址:http://woodcutter.blog.51cto.com/9079117/1880993