django-web开发使用图片验证码

一、单独封装一个工具类用来生成图片验证码

import random # 随机数
import string # 字符的
# 引入绘图的包
from PIL import Image,ImageDraw,ImageFont,ImageFile
# 引入缓存的包
from django.core.cache import cache


# 定义验证码的类
class Captcha(object):
    # 选用的字体的路径
    font_path = 'verdana.ttf'
    # 生成几位数的验证码
    number = 4
    # 生成验证码图片的宽度与高度
    size = (100,30)
    # 背景颜色,默认是白色
    bgcolor = (255,255,255)
    # 字体颜色,默认为蓝色
    fontcolor = (random.randint(0, 100), random.randint(0, 100), random.randint(0, 100))
    # 验证码字体大小
    fontsize = 25
    # 干扰线的颜色,默认是红色
    linecolor = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    # 是否要加入干扰线
    draw_line = True
    # 是否绘制干扰点
    draw_point = True
    # 加入干扰线的条数
    line_number = 2

    # 定义一个类方法
    # 用来随机生成一个字符串(包括英文和数字)
    @classmethod
    def __gene_text(cls):
        source = list(string.letters)
        for index in range(0,10):
            source.append(str(index))
        # 随机获取几位数字拼接
        return ''.join(random.sample(source,cls.number))

    # 定义一个类方法绘制干扰线
    @classmethod
    def __gene_line(cls,draw,width,height):
        # 开始位置
        begin = (random.randint(0,width),random.randint(0,height))
        # 结束位置
        end = (random.randint(0,width),random.randint(0,height))
        draw.line([begin, end], fill=cls.linecolor)

    # 定义一个类方法绘制干扰点
    @classmethod
    def __gene_points(cls,draw,point_chance,width,height):
        chance = min(100, max(0, int(point_chance)))  # 大小限制在[0, 100]
        for w in xrange(width):
            for h in xrange(height):
                tmp = random.randint(0, 100)
                if tmp > 100 - chance:
                    draw.point((w, h), fill=(0, 0, 0))

    # 生成验证码
    @classmethod
    def gene_code(cls):
        width, height = cls.size  # 宽和高
        image = Image.new('RGBA', (width, height), cls.bgcolor)  # 创建图片
        font = ImageFont.truetype(cls.font_path, cls.fontsize)  # 验证码的字体
        draw = ImageDraw.Draw(image)  # 创建画笔
        text = cls.__gene_text()  # 生成字符串
        font_width, font_height = font.getsize(text)
        draw.text(((width - font_width) / 2, (height - font_height) / 2), text, font=font,
                  fill=cls.fontcolor)  # 填充字符串
        # 如果需要绘制干扰线
        if cls.draw_line:
            # 遍历line_number次,就是画line_number根线条
            for x in xrange(0, cls.line_number):
                cls.__gene_line(draw, width, height)
        # 如果需要绘制噪点
        if cls.draw_point:
            cls.__gene_points(draw, 10, width, height)
        # 保存到缓存中,过期时间为2分钟
        cache.set(text.lower(), text.lower(), 120)
        return (text, image)

    # 验证验证码
    @classmethod
    def check_captcha(cls, captcha):
        captcha_cache = cache.get(captcha)
        if captcha_cache and captcha_cache == captcha:
            # 删除缓存
            cache.delete(captcha)
            return True
        else:
            return False

二、在settings.py中配置使用的缓存

# 配置本地缓存
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': [
            '127.0.0.1:11211',
        ]
    }
}

三、开启本地的memcached缓存

  • 1、如果没安装的

    memcached -d install
  • 2、启动

    memcached -d start
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水痕01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值