*@copyright(c)2011
*@author:ernest
*@time:2011
*@version:new
-->
<?php
class verifyimg {
public $fontsize = 15; //定义字体大小
public $length = 4; //定义字符串长度
public $width = 70; //定义图片宽度
public $height = 30; //定义图片高度
public $im = null; //生成一张指定宽高的图片
public $font = 'c:/windows/fonts/arial.ttf';
public $strnum = "";
public function build() {
$strings = array ('1', '2','3', '4', '5', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' );
// $strnum = "";
$count = count ( $strings );
for($i = 1; $i <= $this->length; $i ++) { //循环随机取字符生成字符串
$strnum .= $strings [rand ( 0, $count - 1 )];
}
session_start ();
$_session [verifycode] = $strnum;
$this->im = imagecreate ( $this->width, $this->height );
$backgroundcolor = imagecolorallocate ( $this->im, 255, 255, 255 ); //生成背景色
$framecolor = imagecolorallocate ( $this->im, 0, 255, 0 );
for($i = 0; $i < $this->length; $i ++) {
$chary = ($this->height + 9) / 2 + rand ( - 1, 1 ); //定义字符y坐标
$charx = $i * 15 + 8; //定义字符x坐标
// $text_color = imagecolorallocate($this->im, 255, 0, 0); //生成字符颜色
$text_color = imagecolorallocate ( $this->im, mt_rand ( 50, 255 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );
$angle = rand ( - 20, 20 ); //生成字符角度
//写入字符
imagettftext ( $this->im, $this->fontsize, $angle, $charx, $chary, $text_color, $this->font, $strnum [$i] );
}
for($i = 0; $i <= 5; $i ++) { //循环画背景线
$linecolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
$linex = mt_rand ( 1, $this->width - 1 );
$liney = mt_rand ( 1, $this->height - 1 );
imageline ( $this->im, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );
}
for($i = 0; $i <= 32; $i ++) { //循环画背景点,生成麻点效果
$pointcolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );
imagesetpixel ( $this->im, mt_rand ( 1, $this->width - 1 ), mt_rand ( 1, $this->height - 1 ), $pointcolor );
}
imagerectangle ( $this->im, 0, 0, $this->width - 1, $this->height - 1, $framecolor ); //画边框
ob_clean ();
header ( 'content-type:image/png' );
imagepng ( $this->im );
imagedestroy ( $this->im );
}
}
$img = new verifyimg ();
$img->build ();
?>
