首页 > php学习 > php生成验证码类【还不错】

php生成验证码类【还不错】

2014年12月11日 发表评论 阅读评论

<?php
session_start();
header(‘Content-type:image/png’);
$serverTime = $_SERVER[‘REQUEST_TIME’];
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8 && $serverTime – $_SESSION[‘confirmCode’][‘time’] < 20) {
$_SESSION[‘confirmCode’][‘code’] = ‘whatabadman!’;
$img_file = ‘yzmBad.gif’;
$fp = fopen($img_file, ‘rb’);
$content = fread($fp, filesize($img_file));
fclose($fp);
echo $content;
} else {
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8) {
$_SESSION[‘confirmCode’][‘errorTimes’] = 1;
} else {
$_SESSION[‘confirmCode’][‘errorTimes’] = intval($_SESSION[‘confirmCode’][‘errorTimes’]) + 1;
}
}
class imageCaptcha
{
private $height;
private $width;
private $textNum;
private $textContent;
private $fontColor;
private $randFontColor;
private $fontSize;
private $fontFamily;
private $bgColor;
private $randBgColor;
private $textLang;
private $noisePoint;
private $noiseLine;
private $distortion;
private $distortionImage;
private $showBorder;
private $image;
public function imageCaptcha()
{
$this->textNum = 4;
$this->fontSize = 14;
$this->fontFamily = ”;
$this->textLang = ‘en’;
$this->noisePoint = 0;
$this->noiseLine = 0;
$this->distortion = false;
$this->showBorder = false;
}
public function set_show_mode($w, $h, $num, $fc, $fz, $ff_url, $lang, $bc, $m, $n, $b, $border)
{
$this->width = $w;
$this->height = $h;
$this->textNum = $num;
$this->fontColor = sscanf($fc, ‘#%2x%2x%2x’);
$this->fontSize = $fz;
$this->fontFamily = $ff_url;
$this->textLang = $lang;
$this->bgColor = sscanf($bc, ‘#%2x%2x%2x’);
$this->noisePoint = $m;
$this->noiseLine = $n;
$this->distortion = $b;
$this->showBorder = $border;
}
public function initImage()
{
if (empty($this->width)) {
$this->width = floor($this->fontSize * 1.3) * $this->textNum + 10;
}
if (empty($this->height)) {
$this->height = floor($this->fontSize * 2.5);
}
$this->image = imagecreatetruecolor($this->width, $this->height);
if (empty($this->bgColor)) {
$this->randBgColor = imagecolorallocate($this->image, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
} else {
$this->randBgColor = imagecolorallocate($this->image, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
}
imagefill($this->image, 0, 0, $this->randBgColor);
}
public function randText($type)
{
$string = ”;
switch ($type) {
case ‘en’:
$str = ‘ABCDEFGHJKLMNPQRSTVWXYabcdehkmnprsvwxy35678’;
for ($i = 0; $i < $this->textNum; $i++) {
$string = $string . ‘,’ . $str[mt_rand(0, 42)];
}
break;
}
return substr($string, 1);
}
public function createText()
{
$text_array = explode(‘,’, $this->randText($this->textLang));
$this->textContent = join(”, $text_array);
if (empty($this->fontColor)) {
$this->randFontColor = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
} else {
$this->randFontColor = imagecolorallocate($this->image, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);
}
for ($i = 0; $i < $this->textNum; $i++) {
$angle = mt_rand(-1, 1) * mt_rand(1, 20);
imagettftext($this->image, $this->fontSize, $angle, 5 + $i * floor($this->fontSize * 1.3), floor($this->height * 0.75), $this->randFontColor, $this->fontFamily, $text_array[$i]);
}
}
public function createNoisePoint()
{
for ($i = 0; $i < $this->noisePoint; $i++) {
$pointColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $pointColor);
}
}
public function createNoiseLine()
{
for ($i = 0; $i < $this->noiseLine; $i++) {
$lineColor = imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), 20);
imageline($this->image, 0, mt_rand(0, $this->width), $this->width, mt_rand(0, $this->height), $lineColor);
}
}
public function distortionText()
{
$this->distortionImage = imagecreatetruecolor($this->width, $this->height);
imagefill($this->distortionImage, 0, 0, $this->randBgColor);
for ($x = 0; $x < $this->width; $x++) {
for ($y = 0; $y < $this->height; $y++) {
$rgbColor = imagecolorat($this->image, $x, $y);
imagesetpixel($this->distortionImage, (int) ($x + sin($y / $this->height * 2 * M_PI – M_PI * 0.5) * 3), $y, $rgbColor);
}
}
$this->image = $this->distortionImage;
}
public function createImage()
{
$this->initImage();
$this->createText();
$this->createNoisePoint();
$this->createNoiseLine();
if ($this->distortion != false) {
$this->distortionText();
}
if ($this->showBorder) {
imagerectangle($this->image, 0, 0, $this->width – 1, $this->height – 1, $this->randFontColor);
}
imagepng($this->image);
imagedestroy($this->image);
if ($this->distortion != false) {
imagedestroy($this->distortionImage);
}
return $this->textContent;
}
}
$code_obj = new imageCaptcha();
$code_obj->set_show_mode(’85’, ’30’, ‘4’, ”, ’14’, ‘font/’ . rand(1, 6) . ‘.ttf’, ‘en’, ‘#fcfefb’, mt_rand(5, 10), mt_rand(1, 3), false, false);
$_SESSION[‘confirmCode’][‘code’] = $code_obj->createImage();
$_SESSION[‘confirmCode’][‘time’] = $serverTime;

?>

分类: php学习 标签:
  1. 本文目前尚无任何评论.
您必须在 登录 后才能发布评论.
css.php