首先在表单页面展现之前,生成一副图片,并添加上一些干扰像素或线段,防止OCR程序自动识别,再由PHP程序自动生成随机的待验证的一串数字和字母组合的字符,调用imagettftext()函数画到图片中,并把这串字符保存到Session级变量中.
完整实例:自动检测背景和字体,并随机选取背景图片中的一块范围,随机使用字体,显示验证字符串时随机显示字体大小,字符间距,字符颜色等,代码如下:
- <?php
-
-
-
-
-
-
- session_start();
- class validimg
- {
-
- var $backgroundpath = ‘validbg’;
-
- var $width =’80′;
-
- var $height =’25′;
-
- var $background;
-
- var $text=’abcd’;
-
- var $fontpath = ‘validbg’;
-
- var $font=’simhei.ttf’;
-
- var $font_width = ’20′;
-
- function validimg($text)
- {
- $this->text = $text;
-
- $bgdir = @dir($this->backgroundpath);
- while(false !== ($image = $bgdir ->read()))
- {
- if($image != ‘.’ && $image != ‘..’ && $this->checktype($image) != false)
- {
- $backgroundarr[] = $image;
-
- }
- }
- $bgdir->close();
-
- $fonts = @dir($this->fontpath);
- while(false !== ($font = $fonts ->read()))
- {
- if($font != ‘.’ && $font != ‘..’ && $this->checktype($font,’FONT’) != false)
- {
- $fontsarr[] = $font;
- }
- }
- $fonts->close();
- $this->font = $fontsarr[array_rand($fontsarr,1)];
- $this->background = $backgroundarr[array_rand($backgroundarr,1)];
-
- $this->output();
- }
-
-
- function createbackground()
- {
- switch ($this->checktype($this->background))
- {
- case ‘jpg’:
- $bghanddle = @imagecreatefromjpeg( $this->backgroundpath.’/’.$this->background);
- break;
- case ‘gif’:
- $bghanddle = @imagecreatefromgif( $this->backgroundpath.’/’.$this->background);
- break;
- case ‘png’:
- $bghanddle = @imagecreatefrompng( $this->backgroundpath.’/’.$this->background);
- break;
- default:
- }
- return $bghanddle;
- }
-
- function checktype( $image,$type = ‘IMAGE’)
- {
- $ext = substr( $image, strrpos($image,’.')+1);
- if($type == ‘IMAGE’)
- {
- if ($ext == ‘jpg’ || $ext ==’gif’ || $ext ==’png’)
- return $ext;
- else return false;
- }else if($type == ‘FONT’)
- {
- if ($ext == ‘ttf’)
- return $ext;
- else return false;
- }
- }
-
- function output()
- {
- header(“content-type:image/png;”);
-
- $img = @imagecreatetruecolor( $this->width,$this->height);
- $bghanddle = $this->createbackground();
-
- if($bghanddle)
- {
- $randx=rand(0,(imagesx($bghanddle) – $this->width));
- $randy=rand(0,(imagesy($bghanddle) – $this->height));
- }
- imagecopy($img,$bghanddle,0,0,$randx,$randy,$this->width,$this->height);
-
- for($i=0;$i<strlen($this->text);$i++)
- {
- $angle = rand(-30,30);
- $fontsize = rand(15,20);
- $x = rand($this->font_width*$i,$this->font_width*$i+10);
- $color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
- imagettftext($img,$fontsize,$angle,$x,20,$color,$this->fontpath.’/’.$this->font,substr($this->text,$i,1));
- }
- imagepng($img);
-
- imagedestroy($img);
- imagedestroy($bghanddle);
- }
- }
- new validimg($_SESSION['valid']);
- ?>
测试文件validtest.php,代码如下:
- <?php
-
-
-
-
- session_start();
- $_SESSION['valid']= randstr();
- echo $_SESSION['valid'];
-
-
-
- function randstr($num=4)
- {
- $chars = ‘ABDEFGHJKLMNPQRSTVWXYabdefghijkmnpqrstvwxy23456789′;
- $randstr=”;
- for($i=0;$i<$num;$i++)
- {
- $randstr.=substr($chars,rand(0,strlen($chars)),1);
- }
- return $randstr;
- }
- ?>
- <img src=”validimg.php” />
- 生成gif动态验证码,PHP生成GIF动画来实现动态图片验证码,以下是实现过程,ImageCode函数通过GIFEncoder类实现的GIF动画
-
-
-
-
-
-
- function ImageCode($string = '', $width = 75, $height = 25)
- {
- $authstr = $string ? $string : ((time() % 2 == 0) ? mt_rand(1000, 9999) : mt_rand(10000, 99999));
-
- $board_width = $width;
- $board_height = $height;
-
- for($i = 0; $i < 32; $i++)
- {
- ob_start();
- $image = imagecreate($board_width, $board_height);
- imagecolorallocate($image, 0,0,0);
-
- $colorList[] = ImageColorAllocate($image, 15,73,210);
- $colorList[] = ImageColorAllocate($image, 0,64,0);
- $colorList[] = ImageColorAllocate($image, 0,0,64);
- $colorList[] = ImageColorAllocate($image, 0,128,128);
- $colorList[] = ImageColorAllocate($image, 27,52,47);
- $colorList[] = ImageColorAllocate($image, 51,0,102);
- $colorList[] = ImageColorAllocate($image, 0,0,145);
- $colorList[] = ImageColorAllocate($image, 0,0,113);
- $colorList[] = ImageColorAllocate($image, 0,51,51);
- $colorList[] = ImageColorAllocate($image, 158,180,35);
- $colorList[] = ImageColorAllocate($image, 59,59,59);
- $colorList[] = ImageColorAllocate($image, 0,0,0);
- $colorList[] = ImageColorAllocate($image, 1,128,180);
- $colorList[] = ImageColorAllocate($image, 0,153,51);
- $colorList[] = ImageColorAllocate($image, 60,131,1);
- $colorList[] = ImageColorAllocate($image, 0,0,0);
- $fontcolor = ImageColorAllocate($image, 0,0,0);
- $gray = ImageColorAllocate($image, 245,245,245);
-
- $color = imagecolorallocate($image, 255,255,255);
- $color2 = imagecolorallocate($image, 255,0,0);
-
- imagefill($image, 0, 0, $gray);
-
- $space = 15;
- if($i > 0)
- {
- for ($k = 0; $k < strlen($authstr); $k++)
- {
- $colorRandom = mt_rand(0,sizeof($colorList)-1);
- $float_top = rand(0,4);
- $float_left = rand(0,3);
- imagestring($image, 6, $space * $k, $top + $float_top, substr($authstr, $k, 1), $colorList[$colorRandom]);
- }
- }
-
- for ($k = 0; $k < 20; $k++)
- {
- $colorRandom = mt_rand(0,sizeof($colorList)-1);
- imagesetpixel($image, rand()%70 , rand()%15 , $colorList[$colorRandom]);
-
- }
-
- for($k = 0; $k < 3; $k++)
- {
- $colorRandom = mt_rand(0, sizeof($colorList)-1);
-
- $todrawline = 1;
- if($todrawline)
- {
- imageline($image, mt_rand(0, $board_width), mt_rand(0,$board_height), mt_rand(0,$board_width), mt_rand(0,$board_height), $colorList[$colorRandom]);
- }
- else
- {
- $w = mt_rand(0,$board_width);
- $h = mt_rand(0,$board_width);
- imagearc($image, $board_width - floor($w / 2) , floor($h / 2), $w, $h, rand(90,180), rand(180,270), $colorList[$colorRandom]);
- }
- }
- imagegif($image);
- imagedestroy($image);
- $imagedata[] = ob_get_contents();
- ob_clean();
- ++$i;
- }
-
- $gif = new GIFEncoder($imagedata);
- Header ('Content-type:image/gif');
- echo $gif->GetAnimation();
- }
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>无标题文档</title>
- <script language="javascript">
- function refresh_code()
- {
- form1.imgcode.src="verifycode.php?a="+Math.random();
- }
- </script>
- </head>
- <body>
- <form id="form1" name="form1" method="post" action="checkcode.php">
- <label for="code">验证码:</label>
- <input type="text" name="code" id="textfield" />
- <img id="imgcode" src="VerifyCode.php" alt="验证码" />
- <a href="javascript:refresh_code()">看不清?换一个</a>
- <input type="submit" name="button" id="button" value="提交" />
- </form>
- </body>
- </html>
checkcode.php文件如下:
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <?php
- ini_set('display_errors', 'Off');
- session_start();
- if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
- print("验证码正确,");
- }else{
- print("验证码错误,");
- }
- echo "提交的验证码:".strtoupper($_POST["code"]).",正确的验证码:".strtoupper($_SESSION["VerifyCode"]);
- ?>
|