php的图片验证码代码

作者&投稿:上陆 (若有异议请与网页底部的电邮联系)
php 如何把文字转成图片显示(象验证码那样)~

需要安装PHP GD2库. 基本要用到以下几个函数
//设置一个图片文件名字$png_name = time() . '.png';//画一个200*50的方框的图片$img = imagecreate(200, 50); //设置背景颜色(白色)$bg_color = imagecolorallocate($img, 255, 255, 255); //设置字体颜色(黑色)$txt_color = imagecolorallocate($img, 0, 0, 0); //给图片添上背景颜色imagefilledrectangle($img, 0, 0, 200, 50, $bg_color); //取一个字符,然后画在方块中,其中:21 是字体大小$deg 是字体偏斜角度(0-360°)$x 是横坐标(在图片中,这里是0-200)$y 是纵坐标(在图片中,这里是0-50)$txt_color 是字体颜色captcha.ttf 是字体库$letter 是要写入的字符imagefttext($img, 21, $deg, $x, $y, $txt_color, 'captcha.ttf', $letter);//在图片中画一个点,用以干扰,所以坐标是随机的imagesetpixel($img, rand()%200, rand()%50, $txt_color);//在图片中画一条线,同样用以干扰,坐标随机(两组rand()确定了线段的起点和终点)imageline($img, rand()%200, rand()%50, rand()%200, rand()%50, $bg_color);//将$img输出为文件.imagepng($img, $png_name);

  php登陆页面+验证码的实现,参考如下:

  1、首先新建一个php站点;

  2、先新建一个命名为yzm.php文件,双击编辑,清空Dreamweaver自动生成的HTML代码,如下;

  <?php
  session_start();
  header("Content-Type:image/png"); //设置页面的头信息输出为png图片$im=imagecreate(60,20); //创建一个画布
  $im_color=imagecolorallocate($im,100,100,100); //填充验证码背景为灰色
  for($i=0;$i<4;$i++)
  {
  $line_color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imageline($im,rand(0,60),rand(0,20),rand(0,60),rand(0,20),$line_color);
  }
  //实用循环画四条随机颜色的干扰线
  $n=rand(1000,9999);
  $_SESSION["y"]=$n;
  $p=0;

  for($i=0;$i<4;$i++)
  {
  $p=$p+10;
  $num=substr($n,$i,1); //把验证码数字一个一个的取出来
  $num_color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imagettftext($im,rand(10,15),rand(-10,10),$p,rand(10,15),$num_color,"font1.ttf",$num);

  }
  //设置每个验证码数字不同的颜色,数字角度偏差和字体。
  imagepng($im); //输出验证码
  imagedestroy($im); //释放内存
  ?>

  3、新建login.php文件;
  用户名文本框昵称为name;
  密码文本框为psw;
  验证码为yzm;
  表单的提交方式为post,提交到check.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;
}
?>

<?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);//释放关联内存
?>

<?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();
?>

http://jingyan.baidu.com/article/d45ad1488e7aa569552b80f3.html

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


惠普无线网络打印机设置
在下一步设置中,您需要输入无线网络的账号和密码。设置完成后,确保打印机能够连接到网络。为使惠普能够找到您的产品并提供解决方案,您可以在惠普官网上注册一个账户。注册时需填写姓氏、名字、电子邮件和登录密码,手机号码为可选填项。如果提供了手机号码,您将收到验证短信,需要输入验证码完成验证。

惠普192.168.223.1是什么地址
为了让惠普找到您的产品并提供解决方案,可以在惠普官网上注册账户。注册时需要填写姓、名、邮箱、登录密码等信息,其中手机号为选填项。如果填写了手机号,手机会收到验证短信,需要输入验证码进行验证。如果未填写手机号,注册后邮箱会收到验证码,请查看邮件并填写验证码进行验证。如收件箱未看到验证邮件...

hp惠普如何设置u盘启动?
3、设置完成后,电脑重启,会出现4位数字的验证码,按照屏幕提示的数字输入后(不要用小键盘),更改的设置生效。方法四:如果你的惠普电脑原来是Win10系统要改成Win7那么需要进行一些设置。1、重启笔记本按F10进入BIOS,如果没反应则启动时按ESC,在以下界面,再按F10进入BIOS界面;2、在System ...

点击[http:\/\/pinyin.cn\/1oSli0pEOT0] 查看这张图片。[访问验证码...
http:\/\/zhidao.baidu.com\/link?url=1Ha5HPqEpWD1aa2J-Sjbla1y19eg7W_e4FU2URO_cx0o4u6gcu8apUy1GJBazup9cHBAmOCu_FC701B4P8vFC_ http:\/\/wenwen.sogou.com\/z\/q432234324.htm 参照以上方法先试试.

惠普笔记本电脑账户怎么验证邮箱
1win+I选择“更改电脑设置”2左侧菜单选择“用户”后,右侧选择“切换到Microsoft账户”3输入当前本地账户密码4输入自己有的电子邮箱地址。(若无电子邮箱可以点击注册新电子邮件地址)5填写注册信息(如:密码、姓氏、名字等)6填写安全信息,共三项,必须选择二项或二项以上填写7输入信息验证码后点击完成。

惠普打印机ip地址192.168.223.1怎么填写?
在开始设置之前,别忘了准备无线网络的SSID(网络名称)和密码。在打印机连接界面,输入这些信息以建立连接。最后,为了确保惠普打印机支持您的产品并提供帮助,您可以在惠普官方网站上注册一个账户。注册时,您需要提供姓名、电子邮件和登录密码。如果您提供了手机号码,您将收到验证码短信;如果没有提供,...

惠普14-d014TX怎么样?惠普14-d014TX好吗
您好,感谢您关注惠普产品。1、HP 14-d014TX是一款14英寸笔记本,采用i5-3230M处理器,具有强劲内芯,蕴藏强劲动力,NVIDIA GeForce 820M独显自带显存,清晰画质,视觉新享受,为您展现更强的色彩表现力,4G内存+500G硬盘,DVD刻录光驱,接口全面,传输更快,分享更多它凭借卓越的性能及超高的性价比,是...

hp电脑管理员密码忘了怎么打开
使用Microsoft账户密码重置 如果您的电脑使用的是Microsoft账户登录,您可以通过以下步骤重置账户密码:1. 访问[Microsoft账户](https:\/\/account.microsoft.com\/)网站,输入您的Microsoft账户绑定的电子邮件地址或电话号码。2. 根据提示,通过短信或电子邮件接收验证码进行身份验证。3. 在指定字段输入验证码。4...

惠普笔记本怎么保修升级?
3在“用户注册信息”页面正确输入相关信息,进入“下一步”。4注册成功后,在“个人使用者注册信息”页面,选择“返回”。5在“用户自助服务”页面选择“产品保修信息查询”。6在“查询产品保修信息”页面输入正确的“产品号”“序列号”和“验证码”,并选择“确认”7 在“产品保修详细信息”页面选择“...

惠普笔记本买来就保修两年吗?
方法二:HP官方网站注册升级两年保修 登陆www.icare.hp.com.cn,点击“家庭使用者注册”链接(如果用户以前注册过,跳过此步)注册完后返回www.icare.hp.com.cn 在“用户自助服务”页面选择“产品保修信息查询”。在“查询产品保修信息”页面输入正确的“产品号”“序列号”和“验证码”,并选择“确认...

鹤山区13394215346: php图片验证码实现 -
梅纨倍司: 可以用php的GD库做//随机生成验证码class randomString{function createRandomStr($strLen){list($usec, $sec) = explode(' ', microtime());(float) $sec + ((float) $usec * 100000);$number = '';$number_len = $strLen;$stuff = '1234567890...

鹤山区13394215346: PHP图形验证码识别 -
梅纨倍司: <?php Header("Content-type: image/gif");/** 初始化*/$border = 0; //是否要边框 1要:0不要$how = 4; //验证码位数$w = $how*15; //图片宽度$h = 20; //图片高度$fontsize = 5; //字体大小$alpha = "abcdefghijkmnopqrstuvwxyz"; //验证...

鹤山区13394215346: 如何用PHP生成验证码 -
梅纨倍司: PHP生成验证码的原理:使用PHP的GD库,生成一张带验证码的图片,并将验证码保存在Session中.PHP生成验证码的大致流程有:1、产生一张png的图片;2、为图片设置背景色;3、设置字体颜色和样式;4、产生4位数的随机的验证码;...

鹤山区13394215346: php的图像处理之效验码
梅纨倍司: 主要就是GD库的应用了.下面这个是比较简单的验证码,更复杂的效果自己可以进行扩展. 大部分我都有写注释,你可以看着学习一下,如还不懂,请提出.<?php session_start(); //生成验证码图片 header("Content-type: image/png")...

鹤山区13394215346: 求一个PHP格式的验证码代码~谢谢~ -
梅纨倍司:session_start(); function random($len) {if($i==0) { $str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $s=""; for($i=0;$i$s.=$str[rand(0,35)]; }} return strtoupper($s); } $code=random(4); //随机生成的字符串 $width = 50; //验证码...

鹤山区13394215346: php中的验证码,这段代码是什么意思?请高手指教!谢谢! -
梅纨倍司: 这个代码的意思,就是刷新图片显示,另外一个地方肯定有下面的标签:<img id='captcha_img' /> 你的这个语句就修改这个图片标签显示的图片内容,修改后相当于:<img id='captcha_img' src='/Shop/index.php?con=simple&act=captcha&h=40&w=120&random=0.12654879'/>

鹤山区13394215346: php 验证上传的文件类型为图片,并获得文件的后缀名 -
梅纨倍司: 以下是我上传了一个图片后显示的 $_FILES['filename']的信息 [filename] => Array ( [name] => Winter.jpg [type] => image/jpeg [tmp_name] => /tmp/php2jw7QX [error] => 0 [size] => 105542 ) 其中type是文件类型的minitype 表示方法,例如普通的...

鹤山区13394215346: 用php生成在图片的验证码,可是图片显示不出来?这是怎么回事? -
梅纨倍司: 路径不对,但凡是不显示肯定是路径不对,看看session能都打印处验证码的值,打不出来就说明图片生成有问题

鹤山区13394215346: 怎样用PHP制作验证码 -
梅纨倍司: //验证码类 class ValidateCode {private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子private $code;//验证码private $codelen = 4;//验证码长度private $width = 90;//宽度private $height = 40;...

鹤山区13394215346: 验证码怎么用php实现? -
梅纨倍司: <?php/** Filename: authpage.php*/ srand((double)microtime()*1000000); //验证用户输入是否和验证码一致if(isset($HTTP_POST_VARS['authinput'])){if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0) ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网