一个验证码需要有以下步骤:

  • 验证底图
  • 验证码内容
  • 生成验证码
  • 对比校验

验证码需要依靠PHP的GD扩展库。一些集成环境是默认安装了GD拓展库。

<?php 
//创建一个100*30px图片,默认黑色
$image = imagecreatetruecolor(100, 30);
//给$image背景图片分配颜色,后三个参数是颜色的RGB.255,255,255白色
$bgcolor = imagecolorallocate($image,255,255,255);
//填充$image颜色为$bgcolor,中间两个为坐标。
imagefill($image, 0, 0, $bgcolor);
for($i=0;$i<4;$i++){
    $fontsize = 6;
    $fontcolor = imagecolorallocate($image,0,0,0);
    $fontcontent = rand(0,9);
    //x,y即为坐标。
    $x = ($i*100/4) + rand(5,10);
    $y = rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}

header("content-type:image/png");
//输出图片的时候一定要使用header告诉PHP其格式。
imagepng($image);
imagedestroy($image);
 ?>

 

相关文章:

  • 2021-07-16
  • 2021-12-06
  • 2021-07-28
  • 2021-11-02
  • 2021-07-20
  • 2021-11-28
  • 2021-06-17
  • 2022-01-20
猜你喜欢
  • 2021-10-07
  • 2021-12-26
  • 2021-07-08
  • 2021-12-06
  • 2021-12-05
  • 2021-07-23
  • 2021-08-21
相关资源
相似解决方案