You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					49 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					49 lines
				
				1.5 KiB
			| 
											1 year ago
										 | import base64 | ||
| 
											2 years ago
										 | import io | ||
|  | import os | ||
|  | import random | ||
| 
											1 year ago
										 | from PIL import Image, ImageDraw, ImageFont | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class CaptchaService: | ||
|  |     """
 | ||
|  |     验证码模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def create_captcha_image_service(cls): | ||
| 
											2 years ago
										 |         # 创建空白图像 | ||
|  |         image = Image.new('RGB', (160, 60), color='#EAEAEA') | ||
|  | 
 | ||
|  |         # 创建绘图对象 | ||
|  |         draw = ImageDraw.Draw(image) | ||
|  | 
 | ||
|  |         # 设置字体 | ||
|  |         font = ImageFont.truetype(os.path.join(os.path.abspath(os.getcwd()), 'assets', 'font', 'Arial.ttf'), size=30) | ||
|  | 
 | ||
|  |         # 生成两个0-9之间的随机整数 | ||
|  |         num1 = random.randint(0, 9) | ||
|  |         num2 = random.randint(0, 9) | ||
|  |         # 从运算符列表中随机选择一个 | ||
|  |         operational_character_list = ['+', '-', '*'] | ||
|  |         operational_character = random.choice(operational_character_list) | ||
|  |         # 根据选择的运算符进行计算 | ||
|  |         if operational_character == '+': | ||
|  |             result = num1 + num2 | ||
|  |         elif operational_character == '-': | ||
|  |             result = num1 - num2 | ||
|  |         else: | ||
|  |             result = num1 * num2 | ||
|  |         # 绘制文本 | ||
| 
											1 year ago
										 |         text = f'{num1} {operational_character} {num2} = ?' | ||
| 
											2 years ago
										 |         draw.text((25, 15), text, fill='blue', font=font) | ||
|  | 
 | ||
|  |         # 将图像数据保存到内存中 | ||
|  |         buffer = io.BytesIO() | ||
|  |         image.save(buffer, format='PNG') | ||
|  | 
 | ||
|  |         # 将图像数据转换为base64字符串 | ||
|  |         base64_string = base64.b64encode(buffer.getvalue()).decode() | ||
|  | 
 | ||
|  |         return [base64_string, result] |