首页 > 编程知识 正文

简单的php代码生成验证图片,简单的php代码生成验证图片失败

时间:2023-12-28 11:56:40 阅读:327553 作者:TMBY

本文目录一览:

怎么用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;$i6;$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;$i100;$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的图片验证码代码

这个是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;$i80;$i++)

{

imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);

}

//画随机出现的线

for ($x=0; $x9;$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/pngrn");

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/jpegrn");

include $checkcodefile;

}

?

怎么用PHP写个验证码

首先,当用户打开页面时随机产生一个session,然后根据这个值生成验证码图片。

第二,将验证码图片显示到表单上。

第三,当用户提交时表单时,比较session里的值与表单中验证码的值进行比较。

简单的实现过程:

复杂的验证码图片生成:

求助!!在php中想实现图片验证码的效果

1.rand一个数串。

2.存入cookie中或session中。

3.将数用gd库方法生成图片。

4.用户输入后与cookie或session对比。

done

编写验证码点击“看不清,换一张”是怎么实现的,全代码是什么,需要什么控件?

1、编写php验证码生成文件verification.php。

2、新建login.html,编写登录表单。

3、调用显示验证码图片。

4、编写换验证码JS函数。

5、调用:看不清,换一张验证码。

6、编写登录处理文件dealLogin.php。

7、测试登录。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。