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;
}
?>

Ⅱ 验证码图片生成

我去😓我昨天搞定

Ⅲ 麻烦给我一个最简单的图片验证代码,麻烦给我一个最简单的图片验证代码

后台文件:

1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Drawing;
6using System.Web;
7using System.Web.Security;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.WebControls.WebParts;
11using System.Web.UI.HtmlControls;
12
13public partial class Default2 : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if(!this.IsPostBack)
18 {
19 this.GenImg(this.GenCode(4));
20 }
21
22 }
23 //产生随机字符串
24 private string GenCode(int num)
25 {
26 string[] source={"0","1","2","3","4","5","6","7","8","9",
27 "A","B","C","D","E","F","G","H","I","J","K","L","M","N",
28 "O","P","Q","R","S","T","U","V","W","X","Y","Z"};
29 string code="";
30 Random rd=new Random();
31 for(int i=0;i < num;i++)
32 {
33 code += source[rd.Next(0,source.Length)];
34 }
35 return code;
36 }
37
38 //生成图片
39 private void GenImg(string code)
40 {
41 Bitmap myPalette = new Bitmap(60, 20);//定义一个画板
42
43 Graphics gh = Graphics.FromImage(myPalette);//在画板上定义绘图的实例
44
45 Rectangle rc = new Rectangle(0, 0, 60, 20);//定义一个矩形
46
47 gh.FillRectangle(new SolidBrush(Color.Blue), rc);//填充矩形
48 gh.DrawString(code, new Font("宋体", 16), new SolidBrush(Color.White), rc);//在矩形内画出字符串
49
50 myPalette.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//将图片显示出来
51
52 Session["ValidateCode"] = code;//将字符串保存到Session中,以便需要时进行验证
53
54 gh.Dispose();
55 myPalette.Dispose();
56 }
57
58}

HTML文件:

加入一个HTML控件Image即可

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<img src="Default2.aspx" />

Ⅳ 图片验证码的换一张怎么实现

<img onclick="this.src='servlet/drawRand'"......................

Ⅳ JS如何实现图片验证码

<script language="JavaScript">
function reloadcode(){
var verify=document.getElementById('safecode');
verify.setAttribute('src','images/chknumber.jsp?'+Math.random());
//这里必须加入随机数不然地址相同我发重新加载
}
</script>
=========================

然后再验证码图片里面写onclick呼出上面的函数重新加载.

程序代码
<img src="images/chknumber.jsp" id="safecode" border="0" onclick="reloadcode()" style="cursor:hand;padding:2px 8px 0pt 3px;" />

Ⅵ JS点击图片后显示验证码 如何实现

最简单的方法你把图一的显示验证码也弄成一个图片,比如叫code.png,上面你的代码就能换成
<img src="code.png" onclick="this.src ={$maccms.path_tpl}picture/captcha.png"

Ⅶ 图片验证码

IE工具。选项里。高级。显示图片设置下。或恢复默认下试试。是IE设置的原因。

Ⅷ 验证码代码

这是PHP的

function random($len)
{
if($i==0)
{
$str="";
$length=strlen($str);
$s="";
for($i=0;$i <$len;$i++){
$s.=$str[rand(0,($length-1))];
}
}
return strtoupper($s);
}
$code=random(4); //随机生成的字符串
$width = 50; //验证码图片的宽度
$height = 20; //验证码图片的高度
@header("Content-Type:image/png");
$_SESSION["code"] = $code;
$im=imagecreate($width,$height);
//背景色
$back=imagecolorallocate($im,255, 255, 255);
//模糊点颜色
$pix=imagecolorallocate($im,187,230,247);
//字体色
$font=imagecolorallocate($im,41,163,238);
//绘模糊作用的点
for($i=0;$i <1000;$i++)
{
imagesetpixel($im,rand(0,$width),rand(0,$height),$pix);
}
for($i=0;$i<5;$i++){
$font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagearc($im,mt_rand(-$width,$width),mt_rand(-$height,$height),mt_rand(30,$width*2),mt_rand(20,$height*2),mt_rand(0,360),mt_rand(0,360),$font_color);
}
imagestring($im,6, 7, 2,$code, $font);
imagerectangle($im,0,0,$width-1,$height-1,$font);
//$size=20;
//@imagefttext($im, $size , 0, 5, $size+3, $font, 'c:\\WINDOWS\\Fonts\\simsun.ttc',$code);
imagepng($im);
imagedestroy($im);

Ⅸ 怎么做后台登陆图片验证代码(一点头绪都没有) - 技术问答

生成验证码的文件(生成的是图片文件):1、产生若干随机数2、通过GD函数绘制该随机数3、把随机数写入session前端页面:1、调用验证码文件,显示验证码2、用户输入验证码验证页面:判断用户输入的是否和session中一致

Ⅹ 下面验证码 代码是什么格式的图片

BMP格式的:
Response.ContentType = "image/BMP"