<?php
session_start();
class image{
var $Width = 50;
var $Height = 20;
var $Text=array(
'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z');
function image()
{
if(!empty($_GET['W'])){$this->Width=intval($_GET['W']);}
if(!empty($_GET['H'])){$this->Height=intval($_GET['H']);}
header("Content-type: image/png");
$IM = @imagecreate($this->Width,$this->Height); //创建一个空白图像
$RGB = $this->rand_color();
$Background_Color = imagecolorallocate($IM, $RGB[0], $RGB[1], $RGB[2]);//填充背景
//$RGB = $this->rand_color();
$RCO = imagecolorallocate($IM, $RGB[2], $RGB[1], $RGB[2]);
$RGB = $this->rand_color();
$RCT = imagecolorallocate($IM, $RGB[2], $RGB[1], $RGB[0]);
$Style = array($RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCO,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT,$RCT);
imagesetstyle($IM,$Style);
imageline($IM,0,0,$this->Width/rand(1,3),rand(1,$this->Height),IMG_COLOR_STYLED);//画一条线
imageline($IM,$this->Width/rand(1,4),0,$this->Width,$this->Height,IMG_COLOR_STYLED);//画一条线
imageline($IM,0,rand(0,$this->Height/2),$this->Width,rand(0,$this->Height),IMG_COLOR_STYLED);//画一条线
$TC = imagecolorallocate($IM,255,255,255);
$Text = $this->Text[rand(0,35)];
$_SESSION['_VERIFY']=$Text;
imagestring($IM,4,rand(0,$this->Width/4),rand(2,$this->Height/3),$Text,$TC);
//$RGB = $this->rand_color();
$TC = imagecolorallocate($IM,0,0,0);
$Text = $this->Text[rand(0,35)];
$_SESSION['_VERIFY'].=$Text;
imagestring($IM,4,$this->Width/2-5,$this->Heigth/2+rand(0,5),$Text,$TC);
$RGB = $this->rand_color();
$TC = imagecolorallocate($IM,$RGB[0],$RGB[2],$RGB[1]);
$Text = $this->Text[rand(0,35)];
$_SESSION['_VERIFY'].=$Text;
imagestring($IM,5,$this->Width-$this->Width/4,$this->Heigth/2+rand(1,7),$Text,$TC);
imagepng($IM);
imagedestroy($IM);
}
function rand_color()
{
$Red = rand(0,255);
$Green = rand(0,255);
$Blue = rand(0,255);
return array($Red,$Green,$Blue);
}
}
new image();
?>
直接调用就可以了
比如说:把下面的
代码保存为verifyimg.php
那么可以在模板页面或者html页面直接使用
<img src='verifyimg.php' id='verify' ><a href='#' onclick="document.getElementById('verify').src='verifyimg.php?No='+math.random();'">换个验证码</a>
posted on 2007-11-15 12:39
鱼有所思 阅读(654)
评论(2) 编辑 收藏 引用 网摘 所属分类:
PHP