欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP生成图片验证码功能示例

程序员文章站 2024-03-06 14:25:02
本文实例讲述了php生成图片验证码功能。分享给大家供大家参考,具体如下: 只是简单的用随机函数实现了图片的生成,没有对验证的整个流程做介绍。 代码如下: &l...

本文实例讲述了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常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。