php圖片驗證碼產生程式碼

來源:互聯網
上載者:User

session_start();
class authnum {
 //圖片對象、寬度、高度、驗證碼長度
 private $im;
 private $im_width;
 private $im_height;
 private $len;
 //隨機字串、y軸座標值、隨機顏色
 private $randnum;
 private $y;
 private $randcolor;
 //背景色的紅綠藍,預設是淺灰色
 public $red=238;
 public $green=238;
 public $blue=238;
 /**
  * 可選設定:驗證碼類型、幹擾點、幹擾線、y軸隨機
  * 設為 false 表示不啟用
 **/
 //預設是大小寫數字混合型,1 2 3 分別表示 小寫、大寫、數字型
 public $ext_num_type='';
 public $ext_pixel = false; //幹擾點
 public $ext_line  = false; //幹擾線
 public $ext_rand_y= true;  //y軸隨機
 function __construct ($len=4,$im_width='',$im_height=25) {
  // 驗證碼長度、圖片寬度、高度是執行個體化類時必需的資料
  $this->len      = $len; $im_width = $len * 15;
  $this->im_width = $im_width;
  $this->im_height= $im_height;
  $this->im = imagecreate($im_width,$im_height);
 }
 // 設定圖片背景顏色,預設是淺灰色背景
 function set_bgcolor () {
  imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
 }
 // 獲得任意位元的隨機碼
 function get_randnum () {
  $an1 = 'abcdefghijklmnopqrstuvwxyz';
  $an2 = 'abcdefghijklmnopqrstuvwxyz';
  $an3 = '0123456789';
  if ($this->ext_num_type == '')  $str = $an1.$an2.$an3;
  if ($this->ext_num_type == 1) $str = $an1;
  if ($this->ext_num_type == 2) $str = $an2;
  if ($this->ext_num_type == 3) $str = $an3;
  for ($i = 0; $i < $this->len; $i++) {
   $start = rand(1,strlen($str) - 1);
   $randnum .= substr($str,$start,1);
  }
  $this->randnum = $randnum;
  $_session[an] = $this->randnum;
 }
 // 獲得驗證碼圖片y軸
 function get_y () {
  if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
  else $this->y = $this->im_height / 4 ;
 }
 // 獲得隨機色
 function get_randcolor () {
  $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
 }
 // 添加幹擾點
 function set_ext_pixel () {
  if ($this->ext_pixel) {
   for($i = 0; $i < 100; $i++){
       $this->get_randcolor();
       imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
   }
  }
 }
 // 添加幹擾線
 function set_ext_line () {
  if ($this->ext_line) {
   for($j = 0; $j < 2; $j++){
    $rand_x = rand(2, $this->im_width);
    $rand_y = rand(2, $this->im_height);
    $rand_x2 = rand(2, $this->im_width);
    $rand_y2 = rand(2, $this->im_height);
    $this->get_randcolor();
    imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
   }
  }
 }
 /**建立驗證碼映像:
  * 建立畫布(__construct函數)
  * 設定畫布背景($this->set_bgcolor();)
  * 擷取隨機字串($this->get_randnum ();)
  * 文字寫到圖片上(imagestring函數)
  * 添加幹擾點/線($this->set_ext_line(); $this->set_ext_pixel();)
  * 輸出圖片
 **/
 function create () {
  $this->set_bgcolor();
  $this->get_randnum ();
  for($i = 0; $i < $this->len; $i++){
   $font = rand(4,6);
      $x    = $i/$this->len * $this->im_width + rand(1, $this->len);
   $this->get_y();
   $this->get_randcolor();
      imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
  }
      $this->set_ext_line();
      $this->set_ext_pixel();
   header("content-type:image/png");
   imagepng($this->im);
   imagedestroy($this->im);     //釋放映像資源
 }

}//end class
/**使用驗證碼類的方法:
 * $an = new authnum(驗證碼長度,圖片寬度,圖片高度);
 * 執行個體化時不帶參數則預設是四位的60*25尺寸的常規驗證碼圖片
 * 表單頁面檢測驗證碼的方法,對比 $_session[an] 是否等於 $_post[驗證碼文字框id]
 * 可選配置:
 *  1.驗證碼類型:$an->ext_num_type=1;  值為1是小寫類型,2是大寫類型,3是數字類型
 *  2.幹擾點:$an->ext_pixel = false;   值為false表示不添加幹擾點
 *  3.幹擾線:$an->ext_line = false;    值為false表示不添加幹擾線
 *  4.y軸隨機:$an->ext_rand_y = false; 值為false表示不支援圖片y軸隨機
 *  5.圖片背景:改變 $red $green $blue 三個成員變數的值即可
**/
$an = new authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //幹擾點
$an->ext_line  = false; //幹擾線
$an->ext_rand_y= true; //y軸隨機
$an->green = 238;
$an->create();
?>

好下面來看一款驗證碼調用執行個體

例子demo:

以下為引用的內容:
<!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=gb2312" />
<title>hi.baidu.com/ji_haiyang</title>
</head>

<body>
<form action ="demo1.php教程" method="post" name="form1">
<input name ="test" type="text" value="1234" />
<div onclick='this.innerhtml="<img src=vcode.php?r="+math.random()+"></img>"'>
<img src="vcode.php"></img>
</div>
<input name ="test2" type="text" value="1" />
<input name ="submit" type="submit" value="submit" />
</form>

</body>
</html>


 


例子demo1:

<?php

session_start();
$test = $_post['test'];
$test = trim($test);
$submit = $_post['submit'];
if($test==$_session['vcode']){
echo 'true,輸入驗證碼正確';
} else {
echo 'false,輸入驗證碼錯誤';
}

?>
 

調用檔案vcode.php: 以下為引用的內容:
<?php
/**
* 預設驗證碼session為vcode.即:$_session['vcode'];
* 注意在給變數符值時不要把變數的名子和session衝突
* 注:在驗證時不分大小寫
*/
include("inc_vcode_class.php");
$v = new vcode;
//$config['width'] = 50;   //驗證碼寬
//$config['height'] = 20;   //驗證碼高
//$config['vcode'] = "vcode"; //檢查驗證碼時用的session
//$config['type'] = "default"; //驗證碼展示的類型default:大寫字母,string:小寫字母,int:數字 php程式員站
//$config['length'] = 4;   //驗證碼長度
//$config['interfere']= 10;   //幹擾線強度,範圍為1-30,0或空為不起用幹擾線
//$v->init($config); //配置
$v->create();
?>

驗證碼類inc_vcode_class.php: www~phperz~com 以下為引用的內容:
<?php
/**
* 驗證碼類
* 注:需要gd庫支援
*/
session_start();
class vcode{
private $config;
private $im;
private $str;

function __construct(){
$this->config['width'] = 50;
$this->config['height'] = 20;
$this->config['vcode'] = "vcode";
$this->config['type'] = "default";
$this->config['length'] = 4;
$this->config['interfere'] = 10;

$this->str['default'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['string'] = "abcdefghijklmnopqrstuvwxyz";
$this->str['int']  = "0123456789";
}

//配置
public function init($config=array()){
if (!empty($config) && is_array($config)){
foreach($config as $key=>$value){
$this->config[$key] = $value;
}
}
}

//輸出驗證碼
public function create(){
if (!function_exists("imagecreate")){
return false;
}
$this->create_do();
}

//建立
private function create_do(){
$this->im = imagecreate($this->config['width'],$this->config['height']); php程式員之家
//設定背景為白色
imagecolorallocate($this->im, 255, 255, 255);

//為此背景加個邊框
$bordercolor= imagecolorallocate($this->im,37,37,37);
imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);

//產生驗證碼
$this->create_str();
$vcode = $_session[$this->config['vcode']];

//輸入文字
$fontcolor = imagecolorallocate($this->im,46,46,46);
for($i=0;$i<$this->config['length'];$i++){
imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
}

//加入幹擾線
$interfere = $this->config['interfere'];
$interfere = $interfere>30?"30":$interfere; php程式員站
if (!empty($interfere) && $interfere>1){
for($i=1;$i<$interfere;$i++){
$linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
$x = rand(1,$this->config['width']);
$y = rand(1,$this->config['height']);
$x2 = rand($x-10,$x+10);
$y2 = rand($y-10,$y+10);
imageline($this->im,$x,$y,$x2,$y2,$linecolor);
}
}

header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");
header("content-type:image/jpegrn");
imagejpeg($this->im);
imagedestroy($this->im);
exit;
}

//得到驗證碼
private function create_str(){
if ($this->config['type']=="int"){
for($i=1;$i<=$this->config['length'];$i++){
$vcode .= rand(0,9);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
$len = strlen($this->str[$this->config['type']]);
if (!$len){
$this->config['type'] = "default";
$this->create_str();
}
for($i=1;$i<=$this->config['length'];$i++){
$offset  = rand(0,$len-1);
$vcode .= substr($this->str[$this->config['type']],$offset,1);
}
$_session[$this->config['vcode']] = $vcode;
return true;
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.