php的图片验证码代码

谁能提供详细又完整的又没有错误的验证代码?... 谁能提供详细又完整的又没有错误的验证代码? 展开
 我来答
新程php培训
推荐于2016-02-10 · TA获得超过1872个赞
知道小有建树答主
回答量:504
采纳率:0%
帮助的人:463万
展开全部
这个是phpcms的验证码,经过十几万个网站经验的,非常好用
<?php

session_start();

$enablegd = 1;
//判断图像处理函数是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}

ob_clean(); //清理缓冲

if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //获取$consts中的一个随机数
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //获取$vowels中的一个随机数
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //显示4个字符

$imageX = strlen($radomstring)*8; //图像的宽
$imageY = 20; //图像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一个真彩色图像

//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配颜色并说明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中间背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中间背景2

//与左上角的颜色相同的都会被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往图像上写入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);

//画边框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);

//画一些随机出现的点
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//画随机出现的线
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //画线
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //画椭圆
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('请检查文件目录完整性:/images/checkcode/');

$checkcodefile = $files[rand(0, count($files)-1)]; //随机其中一个文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //获得文件名

header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>
earyuoyt134
2009-11-06 · TA获得超过1091个赞
知道大有可为答主
回答量:1432
采纳率:0%
帮助的人:1265万
展开全部
<?php
srand((double)microtime()*1000000);//设置随机数的种子
$im=imagecreate(45,18); //im:验证码图片;设置一个45*18像素的画布
$black=imagecolorallocate($im,0,0,0); //设置black的颜色范围
$white=imagecolorallocate($im,255,255,255);//设置white的颜色范围
$gray=imagecolorallocate($im,200,200,200);//设置gray的颜色范围
imagefill($im,0,0,$gray);//区域填充,在im图像的坐标x,y(0,0)处用

gray颜色执行区域填充
session_register("autonum");//注册一个新的变量(autonum)到session中
$_SESSION["autonum"]="";
for($i=0;$i<4;$i++){//循环输出一个4位的随机数
//mt_rand()作用:取得乱数值。
$str=mt_rand(1,3); //产生1-3的随机数,用来指定验证码字体
$size=mt_rand(3,6); //用来产生3-6的随机数,用来指定每位验证码数字的

高度
$authnum=mt_rand(0,9);//产生0-9的随机数,用来显示验证码数字
$_SESSION["autonum"].=$authnum;//将验证码连接成字符串保存在session变

量autonum中
imagestring($im,$size,(5+$i*10),$str,$authnum,imagecolorallocate

($im,rand(0,130),rand(0,130),rand(0,130)));
//imagestring:水平地绘制一行字符串。整句的作用是:显示验证码数字
}
for($i=0;$i<200;$i++){//在创建的图片中显示点
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand

(0,255));
imagesetpixel($im,rand()%70,rand()%30,$randcolor);
}
imagepng($im);//以png格式输出验证码图片
imagedestroy($im);//释放关联内存
?>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wuchangqian
2009-11-06 · TA获得超过1317个赞
知道小有建树答主
回答量:913
采纳率:0%
帮助的人:787万
展开全部
<?php
Header("Content-type: image/png");
define("_FONT_DIR", $_SERVER['SystemRoot']."\\fonts\\times.ttf");
class textPNG {
//var $font = "CALIBRI.TTF"; //default font. directory relative to script directory.
var $msg = "test"; // default text to display.
var $size = 26;
var $rot = 7; // rotation in degrees.
var $pad = 0; // padding.
var $transparent = 1; // transparency set to on.
var $red = 0; // white text...
var $grn = 0;
var $blu = 0;
var $bg_red = 10; // on black background.
var $bg_grn = 10;
var $bg_blu = 10;

function draw() {
$width = 0;
$height = 0;
$offset_x = 0;
$offset_y = 0;
$bounds = array();
$image = "";

// determine font height.
$bounds = ImageTTFBBox($this->size, $this->rot, _FONT_DIR, "W");
if ($this->rot < 0) {
$font_height = abs($bounds[7]-$bounds[1]);
} else if ($this->rot > 0) {
$font_height = abs($bounds[1]-$bounds[7]);
} else {
$font_height = abs($bounds[7]-$bounds[1]);
}

// determine bounding box.
$bounds = ImageTTFBBox($this->size, $this->rot, _FONT_DIR, $this->msg);
if ($this->rot < 0) {
$width = abs($bounds[4]-$bounds[0]);
$height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height;
$offset_x = 0;

} else if ($this->rot > 0) {
$width = abs($bounds[2]-$bounds[6]);
$height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height;
$offset_x = abs($bounds[0]-$bounds[6]);

} else {
$width = abs($bounds[4]-$bounds[6]);
$height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height;
$offset_x = 0;
}

$image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);

$background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
$foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);

if ($this->transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, false);

// render it.
ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, _FONT_DIR, $this->msg);

// output PNG object.
imagePNG($image);
}
}

$text = new textPNG;
if (isset($_REQUEST['msg']) ) $text->msg = $_REQUEST['msg']; // text to display
if (isset($_REQUEST['font'])) $text->font = $_REQUEST['font']; // font to use (include directory if needed).
if (isset($_REQUEST['size'])) $text->size = $_REQUEST['size']; // size in points
if (isset($_REQUEST['rot'])) $text->rot = $_REQUEST['rot']; // rotation
if (isset($_REQUEST['pad'])) $text->pad = $_REQUEST['pad']; // padding in pixels around text.
if (isset($_REQUEST['red'])) $text->red = $_REQUEST['red']; // text color
if (isset($_REQUEST['grn'])) $text->grn = $_REQUEST['grn']; // ..
if (isset($_REQUEST['blu'])) $text->blu = $_REQUEST['blu']; // ..
if (isset($_REQUEST['bg_red'])) $text->bg_red = $_REQUEST['bg_red']; // background color.
if (isset($_REQUEST['bg_grn'])) $text->bg_grn = $_REQUEST['bg_grn']; // ..
if (isset($_REQUEST['bg_blu'])) $text->bg_blu = $_REQUEST['bg_blu']; // ..
if (isset($_REQUEST['tr'])) $text->transparent = $_REQUEST['tr']; // transparency flag (boolean).
$text->draw();
?>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
丸子爸爸的日常
2015-01-13 · 知道合伙人软件行家
丸子爸爸的日常
知道合伙人软件行家
采纳数:857 获赞数:985
PHP攻城狮,熟悉js,jq,css,html

向TA提问 私信TA
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式