加减验证码

代码如下:


<?php
/*图片验证码文件,加减计算方式*/



class ImageCode{

 private $Jiashu  = 0;        //加数或者减数
 private $JianShu = 0;        //被加数或者被减数
 private $YunSuan = '';       //运算符
 private $DeShu   = 0;        //得数
 private $String  = '';       //字符串样式
 private $Img;                //图片对象
 private $Width   = 100;      //图片宽度
 private $Height  = 50;       //图片高度
 private $Ttf     = 'Num.ttf';//字体文件
 private $Session = 'code';   //Session变量

 private function JiaShu(){
  header('Content-type:image/png');
  $this -> Jiashu  = rand(1, 10);
  $this -> JianShu = rand(1, 10);
  $this -> YunSuan= $this -> Jiashu > $this -> JianShu ? '-' : '+';
  $this -> DeShu   = $this -> Jiashu > $this -> JianShu ? $this -> Jiashu - $this -> JianShu : $this -> Jiashu + $this -> JianShu;
 }

 public function Show( $W = 100, $H = 50, $T = 'Num.ttf', $Code = 'code' ){
  $this -> JiaShu();
  $this -> String = $this -> Jiashu . $this -> YunSuan . $this -> JianShu . '= ? ';
  $this -> Width  = $W;
  $this -> Height = $H;
  $this -> Ttf    = $T;
  $this -> Session= $Code;
  session_start();
  $_SESSION[$this -> Session] = $this -> DeShu;
  $this -> Images();
 }

 private function Images(){
  $this -> Img = imagecreate($this -> Width, $this -> Height);
  $background_color = imagecolorallocate ($this -> Img, 255, 255, 255);
  imagecolortransparent($this -> Img, $background_color);
        imagettftext($this -> Img, 14, 0, 1, 20, imagecolorallocate ($this -> Img, 0, 0, 0), $this -> Ttf, $this -> String );
  $this -> EchoImages();
 }

 private function EchoImages(){
  imagepng($this -> Img);
  imagedestroy($this -> Img);
 }

}

$ImageCode = new ImageCode;
$ImageCode -> Show(130, 35, 'Num.ttf', 'code');

 

转载于:https://my.oschina.net/18y/blog/795772

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Python生成数字加减验证码的示例代码: ```python import random from PIL import Image, ImageDraw, ImageFont def generate_captcha(): # 定义验证码的宽度和高度 width, height = 120, 50 # 创建一张空白图片 image = Image.new('RGB', (width, height), color = (255, 255, 255)) # 创建一个绘图对象 draw = ImageDraw.Draw(image) # 定义字体 font = ImageFont.truetype('arial.ttf', 30) # 定义数字和操作符 digits = [str(i) for i in range(10)] operators = ['+', '-'] # 随机生成两个数字和一个操作符 num1 = random.choice(digits) num2 = random.choice(digits) operator = random.choice(operators) # 计算表达式的值 if operator == '+': result = int(num1) + int(num2) else: result = int(num1) - int(num2) # 绘制表达式 expression = f'{num1} {operator} {num2} = ?' draw.text((10, 10), expression, font = font, fill = (0, 0, 0)) # 绘制干扰线 for i in range(5): x1, y1 = random.randint(0, width // 2), random.randint(0, height) x2, y2 = random.randint(width // 2, width), random.randint(0, height) draw.line((x1, y1, x2, y2), fill = (0, 0, 0), width = 2) # 返回验证码图片和表达式的结果 return image, result ``` 该函数会返回一个PIL图片对象和表达式的结果。可以将图片对象保存为文件,也可以将其直接显示在屏幕上。示例代码如下: ```python # 生成验证码 image, result = generate_captcha() # 保存图片 image.save('captcha.png') # 显示图片 image.show() # 打印表达式的结果 print(result) ``` 注意:上述代码中使用了PIL库,需要通过pip命令安装。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值