php的图片验证码代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php的图片验证码代码相关的知识,希望对你有一定的参考价值。

谁能提供详细又完整的又没有错误的验证代码?

这个是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;

?>
参考技术A <?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);//释放关联内存
?>
参考技术B <?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();
?>
参考技术C http://jingyan.baidu.com/article/d45ad1488e7aa569552b80f3.html

这里有更加丰富详细的验证码

如何用PHP生成验证码

PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中。PHP生成验证码的大致流程有:

1、产生一张png的图片;

2、为图片设置背景色;

3、设置字体颜色和样式;

4、产生4位数的随机的验证码;

5、把产生的每个字符调整旋转角度和位置画到png图片上;

6、加入噪点和干扰线防止注册机器分析原图片来恶意破解验证码;

7、输出图片;

8、释放图片所占内存。

session_start(); 
getCode(4,60,20); 
 
function getCode($num,$w,$h)  
    $code = ""; 
    for ($i = 0; $i < $num; $i++)  
        $code .= rand(0, 9); 
     
    //4位验证码也可以用rand(1000,9999)直接生成 
    //将生成的验证码写入session,备验证时用 
    $_SESSION["helloweba_num"] = $code; 
    //创建图片,定义颜色值 
    header("Content-type: image/PNG"); 
    $im = imagecreate($w, $h); 
    $black = imagecolorallocate($im, 0, 0, 0); 
    $gray = imagecolorallocate($im, 200, 200, 200); 
    $bgcolor = imagecolorallocate($im, 255, 255, 255); 
    //填充背景 
    imagefill($im, 0, 0, $gray); 
 
    //画边框 
    imagerectangle($im, 0, 0, $w-1, $h-1, $black); 
 
    //随机绘制两条虚线,起干扰作用 
    $style = array ($black,$black,$black,$black,$black, 
        $gray,$gray,$gray,$gray,$gray 
    ); 
    imagesetstyle($im, $style); 
    $y1 = rand(0, $h); 
    $y2 = rand(0, $h); 
    $y3 = rand(0, $h); 
    $y4 = rand(0, $h); 
    imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED); 
    imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED); 
 
    //在画布上随机生成大量黑点,起干扰作用; 
    for ($i = 0; $i < 80; $i++)  
        imagesetpixel($im, rand(0, $w), rand(0, $h), $black); 
     
    //将数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成 
    $strx = rand(3, 8); 
    for ($i = 0; $i < $num; $i++)  
        $strpos = rand(1, 6); 
        imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black); 
        $strx += rand(8, 12); 
     
    imagepng($im);//输出图片 
    imagedestroy($im);//释放图片所占内存 

参考技术A php生成验证码,php验证码,php怎样生成验证码?
工具/原料
这个验证码较实用,大家可以应用到项目中。
方法/步骤
1.
<?php
/*设置文件头为图片输出*/
Header("Content-type: image/JPEG");

/*调用生成验证码函数*/
$checkcode = make_rand(4);

/**
 * 生成验证码字符
 * @param int $length 验证码字符长度
 * @return string
 */
function make_rand($length="32")
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$result="";
for($i=0;$i<$length;$i++)
$num[$i]=rand(0,25);
$result.=$str[$num[$i]];

return $result;

2.
/*调用输出验证码图片函数*/
getAuthImage($checkcode, 160, 40);

/**
 * 生成验证码图片
 * @param string $text 验证码字符
 */
function getAuthImage($text, $w, $y) 
/*设置图片的宽度和高度*/
$im_x = $w;
$im_y = $y;
/*创建图片*/
$im = imagecreatetruecolor($im_x,$im_y);
$text_c = ImageColorAllocate($im, mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$tmpC0=mt_rand(100,255);
$tmpC1=mt_rand(100,255);
$tmpC2=mt_rand(100,255);
$buttum_c = ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
imagefill($im, 16, 13, $buttum_c);
3.
/*字体文件*/
$font = 't1.ttf';
for ($i=0;$i<strlen($text);$i++)

$tmp =substr($text,$i,1);
$array = array(-1,1);
$p = array_rand($array);
$an = $array[$p]*mt_rand(1,10);//角度
$size = 28;
imagettftext($im, $size, $an, 15+$i*$size, 35, $text_c, $font, $tmp);


/*将字符写入文件中*/
$distortion_im = imagecreatetruecolor ($im_x, $im_y);
imagefill($distortion_im, 16, 13, $buttum_c);
for ( $i=0; $i<$im_x; $i++) 
for ( $j=0; $j<$im_y; $j++) 
$rgb = imagecolorat($im, $i , $j);
if( (int)($i+20+sin($j/$im_y*2*M_PI)*10) <= imagesx($distortion_im)&& (int)($i+20+sin($j/$im_y*2*M_PI)*10) >=0 ) 
imagesetpixel ($distortion_im, (int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4) , $j , $rgb);



4.
/*干扰元素点的数量*/
$count = 160;
/*创建干扰元素点*/
for($i=0; $i<$count; $i++)
$randcolor = ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);

/*创建干扰线条*/
$rand = mt_rand(5,30);
$rand1 = mt_rand(15,25);
$rand2 = mt_rand(5,10);
for ($yy=$rand; $yy<=+$rand+2; $yy++)
for ($px=-80;$px<=80;$px=$px+0.1)

$x=$px/$rand1;
if ($x!=0)

$y=sin($x);

$py=$y*$rand2;
imagesetpixel($distortion_im, $px+80, $py+$yy, $text_c);


5.
/*以PNG格式将图像输出到浏览器*/
ImagePNG($distortion_im);

/*销毁图像*/
ImageDestroy($distortion_im);
ImageDestroy($im);

本回答被提问者和网友采纳
参考技术B 自带验证码类,有需要,这里看
http://jingyan.baidu.com/article/d45ad1488e7aa569552b80f3.html

以上是关于php的图片验证码代码的主要内容,如果未能解决你的问题,请参考以下文章

PHP GD 生成图片验证码+session获取储存验证码

如何使用PHP添加GIF动态图片验证码

如何用PHP生成验证码

PHP生成图片验证码demoOOP面向对象版本

php生成图片验证码

求 PHP 图片验证码类 给出详细调用方法 谢谢!!!