<?php
/**---------------------------------------------------------------------------*/
/** PHP 數字運算 驗證碼類
/** @版權注釋
/** 原創 張燦庭 如果您有任何疑問和想法可以發郵件(123294587@qq.com)
/** 本類允許轉載、複制和修改,但轉載、複制和修改的同時請保留原始的出處和作者聲明,這也是對作者勞動成果的一(yī)種尊重!
/**---------------------------------------------------------------------------*/
// 開(kāi)啓session
session_start();
//頭部 防止緩存
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Type:image/gif');
//調用類
$code = new codeClass();
$code->transparent = true;
$code->solid = true;
$code->pixel = 20;
$code->line = 0;
$code->arc = 0;
$code->dashed = 0;
$code->output('authcode');
unset($code);
class codeClass {
public $width = 80; //高度
public $height = 20; //寬度
public $transparent = true; //透明
public $solid = true; //邊框
public $dashed = 0; //虛線
public $arc = 0; //弧線
public $line = 0; //直線
public $pixel = 0; //幹擾點
private $img; //對象
//輸出驗證碼
public function output($name){
$this->create(); //建立對象
$this->drawBg(); //白(bái)色背景
$this->drawAngle(); //邊框
$this->drawPixel(); //幹擾像素
$this->drawLine(); //幹擾線
$this->drawArc(); //弧線
$this->drawDashed(); //虛線
$this->drawText($name); //寫入文字
//銷毀對象
imagedestroy($this->img);
}
//建立對象
private function create(){
$this->width = empty($this->width) ? 80 : $this->width;
$this->height = empty($this->height) ? 20 : $this->height;
$this->img = imagecreatetruecolor($this->width, $this->height);
imagecolorallocate($this->img, 0, 0, 0);
}
//背景
private function drawBg(){
if($this->transparent == true){
imagefill($this->img, 0, 0, imagecolorallocate($this->img, 255, 255, 255)); //填充白(bái)色
//imagecolortransparent($this->img, imagecolorallocate($this->img, 255, 255, 255)); //背景透明
}
}
//邊框
private function drawAngle(){
//灰色邊框
if($this->solid == true){
imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 204, 204, 204));
}
}
//輸出驗證碼
public function drawText($name){
//數組
$math = $this->getText();
$x = 5; //出生(shēng)點
//循環數組
foreach($math[0] as $value){
//文字顔色
$rgb = $this->getColor();
$color = imagecolorallocate($this->img, $rgb['r'], $rgb['g'], $rgb['b']);
//位置
$y = ($this->height - imagefontwidth(5)) / 2; // Y軸 居中(zhōng)
imagestring($this->img, 5, $x, $y, $value, $color);
$x = $x + imagefontwidth(5) * strlen($value); // X軸
}
//保存 session
$_SESSION[$name] = $answer;
//輸出圖像
imagegif($this->img);
}
//等式 答案
private function getText(){
//數值數組
$math = array(
1,
1,
1,
1,
5,
6,
7,
8,
9,
10,
);
//随機
$x = $math[array_rand($math, 1)];
$y = $math[array_rand($math, 1)];
$array = array();
$array[] = array($x, '+', $y, ' =', '?'); //算術表達式
$array[] = $x + $y; //答案
return $array;
}
//文字顔色
private function getColor(){
$color = array();
//暗色
$color[] = '#000080';
$color[] = '#0000EE';
$color[] = '#008B00';
$color[] = '#009ACD';
$color[] = '#191970';
$color[] = '#1C86EE';
$color[] = '#2F4F4F';
$color[] = '#4B0082';
//亮色
$color[] = '#CD0000';
$color[] = '#8E8E38';
$color[] = '#A0522D';
$color[] = '#EE1289';
$color[] = '#FF0000';
$color[] = '#FF4500';
$rgb = $color[array_rand($color, 1)];
return $this->hColor2RGB($rgb);
}
//轉換爲rgb
function hColor2RGB($hexColor) {
$color = str_replace('#', '', $hexColor);
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
return $rgb;
}
//繪制橢圓
private function drawArc(){
if($this->arc > 1){
for ($i = 0; $i < $this->arc; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagearc($this->img, rand(-$this->width, $this->width), rand(-$this->height, $this->height), $this->width, $this->height, $this->width, $this->height, $color);
}
}
}
//繪制虛線
private function drawDashed(){
if($this->dashed > 1){
for ($i = 0; $i < $this->dashed; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagedashedline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);
}
}
}
//繪制對角線
private function drawLine (){
if($this->line > 1){
for ($i = 0; $i < $this->line; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);
}
}
}
//繪制噪點
private function drawPixel(){
if($this->pixel > 1){
for ($i = 0; $i < $this->pixel; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->img, rand(0, $this->width), rand(0, $this->height), $color);
}
}
}
}
?>
/**---------------------------------------------------------------------------*/
/** PHP 數字運算 驗證碼類
/** @版權注釋
/** 原創 張燦庭 如果您有任何疑問和想法可以發郵件(123294587@qq.com)
/** 本類允許轉載、複制和修改,但轉載、複制和修改的同時請保留原始的出處和作者聲明,這也是對作者勞動成果的一(yī)種尊重!
/**---------------------------------------------------------------------------*/
// 開(kāi)啓session
session_start();
//頭部 防止緩存
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-Type:image/gif');
//調用類
$code = new codeClass();
$code->transparent = true;
$code->solid = true;
$code->pixel = 20;
$code->line = 0;
$code->arc = 0;
$code->dashed = 0;
$code->output('authcode');
unset($code);
class codeClass {
public $width = 80; //高度
public $height = 20; //寬度
public $transparent = true; //透明
public $solid = true; //邊框
public $dashed = 0; //虛線
public $arc = 0; //弧線
public $line = 0; //直線
public $pixel = 0; //幹擾點
private $img; //對象
//輸出驗證碼
public function output($name){
$this->create(); //建立對象
$this->drawBg(); //白(bái)色背景
$this->drawAngle(); //邊框
$this->drawPixel(); //幹擾像素
$this->drawLine(); //幹擾線
$this->drawArc(); //弧線
$this->drawDashed(); //虛線
$this->drawText($name); //寫入文字
//銷毀對象
imagedestroy($this->img);
}
//建立對象
private function create(){
$this->width = empty($this->width) ? 80 : $this->width;
$this->height = empty($this->height) ? 20 : $this->height;
$this->img = imagecreatetruecolor($this->width, $this->height);
imagecolorallocate($this->img, 0, 0, 0);
}
//背景
private function drawBg(){
if($this->transparent == true){
imagefill($this->img, 0, 0, imagecolorallocate($this->img, 255, 255, 255)); //填充白(bái)色
//imagecolortransparent($this->img, imagecolorallocate($this->img, 255, 255, 255)); //背景透明
}
}
//邊框
private function drawAngle(){
//灰色邊框
if($this->solid == true){
imagerectangle($this->img, 0, 0, $this->width-1, $this->height-1, imagecolorallocate($this->img, 204, 204, 204));
}
}
//輸出驗證碼
public function drawText($name){
//數組
$math = $this->getText();
$x = 5; //出生(shēng)點
//循環數組
foreach($math[0] as $value){
//文字顔色
$rgb = $this->getColor();
$color = imagecolorallocate($this->img, $rgb['r'], $rgb['g'], $rgb['b']);
//位置
$y = ($this->height - imagefontwidth(5)) / 2; // Y軸 居中(zhōng)
imagestring($this->img, 5, $x, $y, $value, $color);
$x = $x + imagefontwidth(5) * strlen($value); // X軸
}
//保存 session
$_SESSION[$name] = $answer;
//輸出圖像
imagegif($this->img);
}
//等式 答案
private function getText(){
//數值數組
$math = array(
1,
1,
1,
1,
5,
6,
7,
8,
9,
10,
);
//随機
$x = $math[array_rand($math, 1)];
$y = $math[array_rand($math, 1)];
$array = array();
$array[] = array($x, '+', $y, ' =', '?'); //算術表達式
$array[] = $x + $y; //答案
return $array;
}
//文字顔色
private function getColor(){
$color = array();
//暗色
$color[] = '#000080';
$color[] = '#0000EE';
$color[] = '#008B00';
$color[] = '#009ACD';
$color[] = '#191970';
$color[] = '#1C86EE';
$color[] = '#2F4F4F';
$color[] = '#4B0082';
//亮色
$color[] = '#CD0000';
$color[] = '#8E8E38';
$color[] = '#A0522D';
$color[] = '#EE1289';
$color[] = '#FF0000';
$color[] = '#FF4500';
$rgb = $color[array_rand($color, 1)];
return $this->hColor2RGB($rgb);
}
//轉換爲rgb
function hColor2RGB($hexColor) {
$color = str_replace('#', '', $hexColor);
$rgb = array(
'r' => hexdec(substr($color, 0, 2)),
'g' => hexdec(substr($color, 2, 2)),
'b' => hexdec(substr($color, 4, 2))
);
return $rgb;
}
//繪制橢圓
private function drawArc(){
if($this->arc > 1){
for ($i = 0; $i < $this->arc; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagearc($this->img, rand(-$this->width, $this->width), rand(-$this->height, $this->height), $this->width, $this->height, $this->width, $this->height, $color);
}
}
}
//繪制虛線
private function drawDashed(){
if($this->dashed > 1){
for ($i = 0; $i < $this->dashed; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagedashedline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);
}
}
}
//繪制對角線
private function drawLine (){
if($this->line > 1){
for ($i = 0; $i < $this->line; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imageline($this->img, rand(0, $this->width), 0, rand(0, $this->width), $this->height, $color);
}
}
}
//繪制噪點
private function drawPixel(){
if($this->pixel > 1){
for ($i = 0; $i < $this->pixel; $i++){
$color = imagecolorallocate($this->img, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->img, rand(0, $this->width), rand(0, $this->height), $color);
}
}
}
}
?>