利用PHP 简单实现加减法验证码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
  
header('Content-Type: image/png');
$im = imagecreatetruecolor( 200 , 50 );<br>//生成图片长宽
  
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);//背景色
  
imagefilledrectangle($im, 0, 0, 200, 49, $white);
  
// The text to draw
$rand = rand( 1 ,2 ) ;
$one = rand(1,9);
$two = rand(1,9);
if( $rand == 1 ){
  
    $result = $one + $two;
    $text = "$one+$two=?";
  
}else{
    if( $one < $two ){
        list( $two , $one ) = [ $one , $two ];
    }
    $result = $one - $two;
    $text = "$one-$two=?";
}
  
session_start();
$_SESSION['code'] = $result;
// Replace font
$font = './Arvo-Regular.ttf';
  
//imageline( $im , 0 , rand( 1,49 ) , 199 , 30 , $grey );
//干扰线<br>
// Add the text
$i = 0;
$len = strlen( $text );
while( $i < $len  ){
    if( is_numeric( $text[$i] ) ){
        imagettftext($im, 20, rand(-45,45), 20 * ($i+1) , 30, $black, $font, $text[$i]);
    }else{
        imagettftext($im, 20, 0 , 20 * ($i+1) , 30, $black, $font, $text[$i]);
    }
//echo $text[$i];
    $i ++;
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

  如果生成图片为空,请检测字体引入路径是否有问题。

作者:itbaby

出处:https://www.cnblogs.com/itbaby/p/10101143.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   知风阁  阅读(1450)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2017-12-11 LNMP1.3 一键配置环境,简单方便
2017-12-11 lnmp1.3 配置pathinfo---thinkphp3.2 亲测有效
2017-12-11 centos7 忘记mysql root密码办法
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示