php验证码
<?php
/**
* Generate Validation Code
*
* Copyright 2008 Goodjobs.cn
* @author andy <lvqingan@yahoo.com.cn>
*
*/
class ValidationCode
{
// Length of Validation Code
private $mValidationCodeLength = 4;
// Validation Code Candidate
private $mValidationCodeCandidate = '3456789abcdefghjkmnpqrstuvwxy';
// Final Validation Code
private $mValidationCode = '';
// Image for Validatio Code
private $mValidationImage = '';
// Disturb Color
private $mDisturbColor = '';
// Number of Disturb Pix
private $mDisturbLevel = 128;
// Width of Image
private $mValidationImageWidth = '90';
// Height of Image
private $mValidationImageHeight = '25';
// 算术验证码第一个数字
private $firstNumber = '';
// 算术验证码第二个数字
private $secondNumber = '';
// 算术验证码符号
private $symbol = '';
// 算术验证码符号数组
private $symbolMap = ['+','×'];
/**
* Output Header
*
*/
private function OutFileHeader()
{
header ("Content-type: image/png");
}
/**
* Get Font
*/
private function getFont()
{
return dirname(__FILE__).'/../data/fonts/Arial_Bold.ttf';
}
/**
* Create Validation Code
*
*/
private function CreateValidationCode()
{
for ($i = 0; $i < $this->mValidationCodeLength; $i++) {
$pos = mt_rand(0, strlen($this->mValidationCodeCandidate) - 1);
$this->mValidationCode .= strtoupper(substr($this->mValidationCodeCandidate, $pos, 1));
}
return $this->mValidationCode;
}
/**
* Create Math Validation Code
*
*/
private function CreateMathValidationCode()
{
$this->firstNumber = rand(0,9);
$this->secondNumber = rand(0,9);
$this->symbol = $this->symbolMap[array_rand($this->$symbolMap)];
switch ($this->symbol) {
case '+':
$this->mValidationCode = $this->firstNumber + $this->secondNumber;
break;
case '×':
$this->mValidationCode = $this->firstNumber * $this->secondNumber;
break;
default:
# code...
break;
}
return $this->mValidationCode;
}
/**
* Create Validation Image
*
*/
private function CreateValidationImage()
{
$this->mValidationImage = @imagecreate($this->mValidationImageWidth, $this->mValidationImageHeight);
imagecolorallocate($this->mValidationImage, 192, 192, 192);
return $this->mValidationImage;
}
/**
* Set Disturb Color
*
*/
private function SetDisturbColor()
{
for ($i = 0; $i <= $this->mDisturbLevel; $i++)
{
// Random Color
$this->mDisturbColor = imagecolorallocate($this->mValidationImage, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->mValidationImage, rand(0, $this->mValidationImageWidth), rand(0, $this->mValidationImageHeight), $this->mDisturbColor);
}
}
/**
* Set Width and Height of Validation Image
*
* @param $width
* @param $height
*
*/
public function SetValidationImageSize($width, $height)
{
if($width == '' || $height == '') {
return false;
}
$this->mValidationImageWidth = $width;
$this->mValidationImageHeight = $height;
return true;
}
/**
* Draw Validation Code to Image
*
*/
private function DrawValidationCodeToImage()
{
for ($i=0; $i <= $this->mValidationCodeLength; $i++)
{
$backgroundColor = imagecolorallocate($this->mValidationImage, rand(0,255), rand(0,128), rand(0,255));
$charX = floor($this->mValidationImageWidth / $this->mValidationCodeLength) * $i + 5;
$charY = rand($this->mValidationImageHeight - 6, $this->mValidationImageHeight - 2);
$angle = rand(-25, 25);
imagettftext ($this->mValidationImage, 14, $angle, $charX, $charY, $backgroundColor, $this->getFont, $this->mValidationCode[$i]);
}
}
/**
* Draw Math Validsation Code to Image
*
*/
private function DrawMathValidationCodeToImage()
{
$mathArr = [$this->firstNumber, $this->Symbol, $this->secondNumber, '='];
foreach($mathArr as $key=>$value){
$backgroundColor = imagecolorallocate($this->mValidationImage, rand(0,255), rand(0,128), rand(0,255));
$charX = floor($this->mValidationImageWidth / count($mathArr) ) * $key + 5;
$charY = rand($this->mValidationImageHeight - 6, $this->mValidationImageHeight - 2);
$angle = rand(-25, 25);
imagettftext ($this->mValidationImage, 14, $angle, $charX, $charY, $backgroundColor, $this->getFont, $value);
}
}
/**
* Output Image
*
*/
public function OutputValidationImage()
{
$this->OutFileHeader();
$outPutfunc = ['OutputCode','OutputMathCode'];
$func = $outPutfunc[array_rand($outPutfunc)];
$this->$func();
imagepng($this->mValidationImage);
imagedestroy($this->mValidationImage);
}
/**
* Output Normal Code
*/
private function OutputCode()
{
$this->CreateValidationCode();
$this->CreateValidationImage();
$this->SetDisturbColor();
$this->DrawValidationCodeToImage();
}
/**
* Output Math Code
*/
private function OutputMathCode()
{
$this->CreateMathValidationCode();
$this->CreateValidationImage();
$this->SetDisturbColor();
$this->DrawMathValidationCodeToImage();
}
/**
* Get Validation Code
*
* @return string
*/
public function getValidationCode() {
return $this->mValidationCode;
}
}