本文实例讲述了PHP生成图片验证码功能。分享给大家供大家参考,具体如下:
只是简单的用随机函数实现了图片的生成,没有对验证的整个流程做介绍。
代码如下:
rush: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,100);
$color=imagecolorallocate($imgRes,0);
for($i=0;$i<$validateLength;$i++){
$rand=rand(1,58);
$strToDraw=$strToDraw." ".$chars[$rand];
}
imagestring($imgRes,5,$strToDraw,$color);
for($i=0;$i<100;$i++){
imagesetpixel($imgRes,rand(0,$imgW),$imgH),$color);
}
imagepng($imgRes);
imagedestroy($imgRes);
运行效果如下:
PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:
在线图片转换BASE64工具:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。