很多站长朋友们都不太清楚php图形校验,今天小编就来给大家整理php图形校验,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 我在本地测试下面的php图形验证源码,老是显示xx.php无法下载(来自localhost),哪位能否帮我看下代码? 2、 实现php中图形验证码刷新的问题 3、 怎么用php生成图像,生成验证码 我在本地测试下面的php图形验证源码,老是显示xx.php无法下载(来自localhost),哪位能否帮我看下代码?帮你测试了下,你里面有句写少了
$backColor=ImageColorAllocate($im,rand(220,255),rand(220,255));
RGB值是3个的,再加个rand(220,255)变成
$backColor=ImageColorAllocate($im,rand(220,255),rand(220,255),rand(220,255));
这样就OK了
你这里应该是运行到 $backColor=ImageColorAllocate这里就出错了,所以图片没有生成,所以出现你看到的错误,因为这个函数你少写了个参数。
<?
phpinfo();
?
这样可以看到php相关信息,你看看GD相关的信息
开启的话GD Support 是enabled 的
实现php中图形验证码刷新的问题首先要说明,浏览器对图片,JS等文件会进行缓存当浏览器访问图片的时候,浏览器会查看缓存中是否有这张图片如果有则使用缓存图片,没有则对服务器重新发起访问而浏览器判断是否存在缓存文件是通过文件的url进行识别的如果url不同,浏览器就会对服务器发起新的请求所有加上一个随机参数就能实现验证码图片的刷新因为随机数不同,所以url就不同,所以每次浏览器都会对验证码图片发起新的访问,达到刷新验证码的功能无论是img.src = "imgcode.php?"+Math.random();还是imgcode.php?tm="+Math.random();都是为了不要使用浏览器中图片缓存其中tm没有任何意思,你可以随便取你想要的名字甚至就像第一种情况不用给名字
怎么用php生成图像,生成验证码<?php
//验证码类
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
private $code;//验证码
private $codelen = 4;//验证码长度
private $width = 90;//宽度
private $height = 40;//高度
private $img;//图形资源句柄
private $font;//指定的字体
private $fontsize = 20;//指定字体大小
private $fontcolor;//指定字体颜色
//构造方法初始化
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
}
//生成随机码
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//生成线条、雪花
private function createLine() {
//线条
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//雪花
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//输出
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对外生成
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//获取验证码
public function getCode() {
return strtolower($this->code);
}
}
关于php图形校验的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php图形校验 php绘制图形函数的详细内容...