程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP 動態隨機生成驗證碼類代碼

PHP 動態隨機生成驗證碼類代碼

編輯:關於PHP編程

下面是效果圖,這個效果圖是沒有開啟干擾碼的效果圖

下面是類代碼

復制代碼 代碼如下:
<?php
/************************************************
//FILE:ImageCode
//DONE:生成動態驗證碼類
//DATE"2010-3-31
//Author:www.5dkx.com 5D開心博客
************************************************************************/
class ImageCode{
private $width; //驗證碼圖片寬度
private $height; //驗證碼圖片高度
private $codeNum; //驗證碼字符個數
private $checkCode; //驗證碼字符
private $image; //驗證碼畫布
/************************************************************************
// Function:構造函數
// Done:成員屬性初始化
// Author:www.5dkx.com 5D開心博客
************************************************************************/
function __construct($width=60,$height=20,$codeNum=4)
{
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->checkCode = $this->createCheckCode();
}
function showImage()
{
$this->getcreateImage();
$this->outputText();
$this->setDisturbColor();
$this->outputImage();
}
function getCheckCode()
{
return $this->chekCode;
}
private function getCreateImage()
{
$this->image = imagecreatetruecolor($this->width,$this->height);
$back = imagecolorallocate($this->image,255,255,255);
$border = imagecolorallocate($this->image,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
//使用純白色填充矩形框,這裡用的話後面干擾碼失效
/*如果想用干擾碼的話使用下面的*/
//imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
}
private function createCheckCode()
{
for($i=0;$i<$this->codeNum;$i++)
{
$number = rand(0,2);
switch($number)
{
case 0: $rand_number = rand(48,57); break;//數字
case 1: $rand_number = rand(65,90);break;//大寫字母
case 2: $rand_number = rand(97,122);break;//小寫字母
}
$asc = sprintf("%c",$rand_number);
$asc_number = $asc_number.$asc;
}
return $asc_number;
}
private function setDisturbColor()//干擾嗎設置
{
for($i=0;$i<=100;$i++)
{
//$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255));
$color = imagecolorallocate($this->image,255,255,255);
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
//$color = imagecolorallocate($this->image,0,0,0);
//imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
private function outputText()
{
//隨機顏色、隨機擺放、隨機字符串向圖像輸出
for($i=0;$i<=$this->codeNum;$i++)
{
$bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255));
$x = floor($this->width/$this->codeNum)*$i+3;
$y = rand(0,$this->height-15);
imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color);
}
}
private function outputImage()
{
if(imagetypes()&IMG_GIF)
{
header("Content_type:image/gif");
imagegif($this->image);
}
elseif(imagetypes()&IMG_JPG)
{
header("Content-type:image/jpeg");
imagejpeg($this->image,"",0.5);
}
elseif(imagetypes()&IMG_PNG)
{
header("Content-type:image/png");
imagejpeg($this->image);
}
elseif(imagetypes()&IMG_WBMP)
{
header("Content-type:image/vnd.wap.wbmp");
imagejpeg($this->image);
}
else
{
die("PHP不支持圖像創建");
}
}
function __destruct()
{
imagedestroy($this->image);
}
}
/*顯示*/
/*******************************************************************
session_start();
$image = new ImageCode(60,20,4);
$image->showImage();
$_SESSION['ImageCode'] = $image->getCheckCode();
*******************************************************************/
?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved