PHP - 简单验证码

叁歲伎倆 2023-06-23 01:54 51阅读 0赞

```php
<?php
header(“content-type:image/jpeg”);
$width = 120;
$height = 40;
$img = imagecreatetruecolor($width,$height);

$color_bg = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$color_str = imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
$element = [‘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’];
$str = “”;
for($i=0;$i<5;$i++) {
$str.=$element[rand(0,count($element)-1)];
}

imagefill($img,0,0,$color_bg);

//imagesetpixel — 画一个单一像素
//imagesetpixel( resource $image, int $x, int $y, int $color) : bool
//imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
//循环输出,可以重复输出点
for($i=0;$i<100;$i++) {
imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),$color_str);
}

  1. //循环输出多条线
  2. for($i=0;$i<3;$i++) \{
  3. imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),$color\_str);
  4. \}
  5. //imagettftext — 用 TrueType 字体向图像写入文本
  6. imagettftext($img,25,rand(-5,5),rand(5,15),rand(30,35),$color\_str,'fonts/ywsfxs.ttf',$str);

imagejpeg($img);
//最后要释放内存
imagedestroy($img);

//金龙制作
```

效果图;
![效果图片](https://img-blog.csdnimg.cn/20191110222350430.png?x-oss-process=image/watermark,type\_ZmFuZ3poZW5naGVpdGk,shadow\_10,text\_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjE5MDg5Mw==,size\_16,color\_FFFFFF,t\_70)

发表评论

表情:
评论列表 (有 0 条评论,51人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PHP 验证

    php 验证码 一个特别简单的PHP验证码的小案例,备注基本都写上了,不明白的可以跟我问哦,知无不言言无不尽。 html文件 文件名:vad-login.htmv

    相关 PHP实现验证

    PHP 利用自身带的gd库做验证码功能,也是很方便的,现在什么地方都在使用验证码,就是为了识别是人还是机器,防止一些不法分子对网站进行非法的流量攻击操作,比如论坛的灌水等,有了

    相关 php 实现验证

    \[导读\] 验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码。 session\_start();     $im