checkcode.php php
<?php session
/** 字體
* 生成驗證碼 this
* @author chenzhouyu spa
* 類用法 .net
* $checkcode = new checkcode(); code
* $checkcode->doimage(); 圖片
* //取得驗證 ip
* $_SESSION['securimage_code_value']=$checkcode->get_code(); 內存
*/
error_reporting(0);
class checkcode {
//驗證碼的寬度
public $width=130;
//驗證碼的高
public $height=50;
//設置字體的地址
private $font;
//設置字體色
public $font_color;
//設置隨機生成因子
public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';
//設置背景色
public $background = '#EDF7FF';
//生成驗證碼字符數
public $code_len = 4;
//字體大小
public $font_size = 20;
//驗證碼
private $code;
//圖片內存
private $img;
//文字X軸開始的地方
private $x_start;
function __construct() {
$this->font = "font/arial.ttf";
}
/**
* 生成隨機驗證碼。
*/
protected function creat_code() {
$code = '';
$charset_len = strlen($this->charset)-1;
for ($i=0; $i<$this->code_len; $i++) {
$code .= $this->charset[rand(1, $charset_len)];
}
$this->code = $code;
}
/**
* 獲取驗證碼
*/
public function get_code() {
return strtolower($this->code);
}
/**
* 生成圖片
*/
public function doimage() {
$code = $this->creat_code();
$this->img = imagecreatetruecolor($this->width, $this->height);
if (!$this->font_color) {
$this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));
} else {
$this->font_color = imagecolorallocate($this->img, hexdec(substr($this->font_color, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));
}
//設置背景色
$background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));
//畫一個櫃形,設置背景顏色。
imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);
$this->creat_font();
$this->creat_line();
$this->output();
}
/**
* 生成文字
*/
private function creat_font() {
$x = $this->width/$this->code_len;
for ($i=0; $i<$this->code_len; $i++) {
imagettftext($this->img, $this->font_size, rand(-30,30), $x*$i+rand(0,5), $this->height/1.4, $this->font_color, $this->font, $this->code[$i]);
if($i==0)$this->x_start=$x*$i+5;
}
}
/**
* 畫線
*/
private function creat_line() {
imagesetthickness($this->img, 3);
$xpos = ($this->font_size * 2) + rand(-5, 5);
$width = $this->width / 2.66 + rand(3, 10);
$height = $this->font_size * 2.14;
if ( rand(0,100) % 2 == 0 ) {
$start = rand(0,66);
$ypos = $this->height / 2 - rand(10, 30);
$xpos += rand(5, 15);
} else {
$start = rand(180, 246);
$ypos = $this->height / 2 + rand(10, 30);
}
$end = $start + rand(75, 110);
imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color);
if ( rand(1,75) % 2 == 0 ) {
$start = rand(45, 111);
$ypos = $this->height / 2 - rand(10, 30);
$xpos += rand(5, 15);
} else {
$start = rand(200, 250);
$ypos = $this->height / 2 + rand(10, 30);
}
$end = $start + rand(75, 100);
imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);
}
/**
* 輸出圖片
*/
private function output() {
header("content-type:image/png\r\n");
imagepng($this->img);
imagedestroy($this->img);
}
}
$checkcode = new checkcode();
$checkcode->doimage();
//取得驗證
session_start();
$_SESSION['securimage_code_value']=$checkcode->get_code();
注意:第50行 $this->font = "font/arial.ttf"; 須要 指定一個 字體
調用方法:
- <SCRIPT LANGUAGE="JavaScript">
- function reloadcode(){
- var d = new Date();
- document.getElementById('safecode').src="/checkcode.php?t="+d.toTimeString()
- }
- </SCRIPT>
- 驗證碼:<input name="chknumber" type="text" maxlength="4" class="chknumber_input"/><img src='checkcode.php' id="safecode" title="看不清楚?點擊切換!"/>