這是類的代碼,文件名:
code.class.php <?php /* * @version 1.0 * @author YSY * @date 2013-07-07 * @name PHP驗(yàn)證碼類 * * 使用的方法: * Create()-----------創(chuàng)建圖片,并為圖片上顏色,顏色隨機(jī)生成 * imgColor()---------設(shè)置顏色,方便以后干擾素,文字顏色的需要 * imgDisturb()-------干擾素的生成 * imgCode()----------驗(yàn)證隨機(jī)碼的生成 * imgText()----------向圖片寫入文本 * outimg()-----------輸出圖片,提供三種格式輸出,png,jpg,gif */ class Caotcha{ private $image; //驗(yàn)證碼圖片 private $img_width; //驗(yàn)證碼圖片寬 private $img_height; //驗(yàn)證碼圖片高 private $img_type; //驗(yàn)證碼圖片輸出的類型,支持:png/jgg/gif private $img_line_num; //干擾線的數(shù)量 private $img_pix_num ; //干擾點(diǎn)的數(shù)量 private $img_code_num; //文本的字?jǐn)?shù) function __construct($width,$height,$type,$line,$pix,$code){ $this->img_width = $width; $this->img_height = $height; $this->img_type = $type; $this->img_line_num = $line; $this->img_pix_num = $pix; $this->img_code_num = $code; $this->Create(); } //*********創(chuàng)建圖片********* function Create(){ $this->image = imagecreate($this->img_width, $this->img_height); imagecolorallocate($this->image, rand(155, 255), rand(155, 255), rand(155, 255)); return $this->image; } //*********創(chuàng)建顏色********* function imgColor($red,$green,$blue){ $color = imagecolorallocate($this->image, $red, $green, $blue); return $color; } //*********干擾素********* function imgDisturb($color){ if ($this->img_line_num !=0){ for ($i=0;$i<$this->img_line_num;$i++){ imageline($this->image, rand(0, $this->img_width*0.2), rand(0, $this->img_height), rand($this->img_width*0.8, $this->img_width), rand(0, $this->img_height), $color); } }else { return false; } if ($this->img_pix_num != 0){ for ($i=0;$i<$this->img_pix_num;$i++){ imagesetpixel($this->image, rand(0, $this->img_width), rand(0, $this->img_height), $color); } }else { return false; } } //*********驗(yàn)證碼的生成********* function imgCode(){ for ($o=0;$o<$this->img_code_num;$o++){ $rand=$rand.dechex(rand(1,15)); } return $rand; } //*********向圖像寫入文本********* function imgText($ttf,$color,$code){ imagettftext($this->image, rand(12, 18), rand(0, 5), rand(10, 20), rand(15, $this->img_height), $color, $ttf, $code); } //*********輸出圖片********* function outimg(){ if ($this->img_type == 'png'){ header("content-type:image/png"); imagepng($this->image); }elseif ($this->img_type == 'jpg'){ header("content-type:image/jpeg"); imagejpeg($this->image); }elseif ($this->img_type == 'gif'){ header("content-type:image/gif"); imagegif($this->image); } } } ?> 例子,實(shí)例化,文件名code.php <?php session_start(); include 'try.php'; $pic = new Caotcha(80, 30,'gif',4,120,4); //實(shí)例化Caotcha,設(shè)置圖片的寬為80,高為30,輸出圖片類型gif,干擾線4,干擾點(diǎn)120,文本4 $color_l = $pic->imgColor(rand(170, 230), rand(170, 230), rand(170, 230));//設(shè)置兩種隨機(jī)顏色,color_l干擾素顏色,color_z文本的顏色 $color_z = $pic->imgColor(rand(100, 130), rand(100, 130), rand(100, 130)); $pic->imgDisturb($color_l); //調(diào)用的干擾素,并給它設(shè)置顏色 $code = $pic->imgCode(); //把生成的隨機(jī)碼調(diào)用出來(lái),方便賦給session $_SESSION[check_pic] = $code; $pic->imgText('hyww.ttf', $color_z, $code); //設(shè)置一下輸出文本的字體,還是輸出字體的顏色 $pic -> outimg(); //輸出圖片,這里有三種圖片格式,png,jpg,gif ?> 還有最后一個(gè)測(cè)試使用,文件名try.php,也就是輸入一下驗(yàn)證碼,驗(yàn)證是否正確 <?php session_start(); if ($_POST[sub]){ if ($_POST[check] == $_SESSION[check_pic]){ echo "驗(yàn)證碼正確".$_SESSION[check_pic]; }else { echo "驗(yàn)證碼錯(cuò)誤".$_SESSION[check_pic]; } } ?> <form action="" method ="post"> <img src="code.php"><br> <input type="text" name=check><br> <input type="submit" name="sub" value="提交"> </form> |
|
來(lái)自: YSY私館 > 《PHP知識(shí)》