找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1662|回复: 0
打印 上一主题 下一主题

修改了一个很不错的php验证码(支持中文)

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2018-2-14 09:25:38 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

            php英文验证码
captcha.class.php
[U]复制代码[/U] 代码如下:
width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
}
function showImg()
{
//创建图片
$this->createImg();
//设置干扰元素
$this->setDisturb();
//设置验证码
$this->setCaptcha();
//输出图片
$this->outputImg();
}
function getCaptcha()
{
return $this->code;
}
private function createImg()
{
$this->im = imagecreatetruecolor($this->width, $this->height);
$bgColor = imagecolorallocate($this->im, 0, 0, 0);
imagefill($this->im, 0, 0, $bgColor);
}
private function setDisturb()
{
$area = ($this->width * $this->height) / 20;
$disturbNum = ($area > 250) ? 250 : $area;
//加入点干扰
for ($i = 0; $i im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
}
//加入弧线
for ($i = 0; $i im, rand(128, 255), rand(125, 255), rand(100, 255));
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
}
}
private function createCode()
{
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
for ($i = 0; $i codeNum; $i++) {
$this->code .= $str{rand(0, strlen($str) - 1)};
}
}
private function setCaptcha()
{
$this->createCode();
for ($i = 0; $i codeNum; $i++) {
$color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));
$size = rand(floor($this->height / 5), floor($this->height / 3));
$x = floor($this->width / $this->codeNum) * $i + 5;
$y = rand(0, $this->height - 20);
imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);
}
}
private function outputImg()
{
if (imagetypes() & ) {
header('Content-type:image/jpeg');
imagejpeg($this->im);
} elseif (imagetypes() & ) {
header('Content-type: image/gif');
imagegif($this->im);
} elseif (imagetype() & ) {
header('Content-type: image/png');
imagepng($this->im);
} else {
die("Don't support image type!");
}
}
}

demo.php
[U]复制代码[/U] 代码如下:
showImg();
上面是支持英文的,脚本之家再附上一个支持中文的验证码
[U]复制代码[/U] 代码如下:
            
            
您可能感兴趣的文章:
  • PHP中文汉字验证码
  • php中文字母数字验证码实现代码
  • PHP 正则表达式验证中文的问题
  • 支持中文字母数字、自定义字体php验证码代码
  • php中文验证码实现示例分享
  • 支持中文、字母、数字的PHP验证码
  • php中文验证码实现方法
  • php使用正则验证中文
  • php实现的简单中文验证码功能示例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端