PHP生成图片验证码功能详解

墨辰丷
发布: 2023-03-28 08:36:02
原创
1781 人浏览过

这篇文章主要介绍了PHP生成图片验证码功能,结合实例形式简单介绍了php生成验证码图片的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

具体如下:

只是简单的用随机函数实现了图片的生成,没有对验证的整个流程做介绍。

代码如下:

<?php
/**
 * Created by JetBrains PhpStorm.
 * User: lee
 * To change this template use File | Settings | File Templates.
 */
header("content-type:image/png");
$validateLength=4;
$strToDraw="";
$chars=[
  "0","1","2","3","4",
  "5","6","7","8","9",
  "a","b","c","d","e","f","g",
  "h","i","j","k","l","m","n",
  "o","p","q","r","s","t",
  "u","v","w","x","y","z",
  "A","B","C","D","E","F","G",
  "H","I","J","K","L","M","N",
  "O","P","Q","R","S","T",
  "U","V","W","X","Y","Z"
];
$imgW=80;
$imgH=25;
$imgRes=imagecreate($imgW,$imgH);
$imgColor=imagecolorallocate($imgRes,255,255,100);
$color=imagecolorallocate($imgRes,0,0,0);
for($i=0;$i<$validateLength;$i++){
  $rand=rand(1,58);
  $strToDraw=$strToDraw." ".$chars[$rand];
}
imagestring($imgRes,5,0,5,$strToDraw,$color);
for($i=0;$i<100;$i++){
  imagesetpixel($imgRes,rand(0,$imgW),rand(0,$imgH),$color);
}
imagepng($imgRes);
imagedestroy($imgRes);
登录后复制

运行效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php实现网站验证码功能

php封装的验证码类详解

php实现封装的验证码

以上是PHP生成图片验证码功能详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板