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生成图片验证码功能示例

PS:这里再为大家推荐几款比较实用的图片处理工具供大家参考使用:

在线图片转换BASE64工具:

ICO图标在线生成工具:

在线Email邮箱图标制作工具:

在线图片格式转换(jpg/bmp/gif/png)工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/073e36d9a038e67dc3a81e959685645a.html