@login_required
def get_check_code_image(request):
"""
background #随机背景颜色
line_color #随机干扰线颜色
img_width = #画布宽度
img_height = #画布高度
font_color = #验证码字体颜色
font_size = #验证码字体尺寸
font = I#验证码字体
"""
import Image, ImageDraw, ImageFont, random
string = {'number':'12345679',
'litter':'ACEFGHKMNPRTUVWXY'}
background = (random.randrange(230,255),random.randrange(230,255),random.randrange(230,255))
line_color = (random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
img_width = 58
img_height = 30
font_color = ['black','darkblue','darkred']
font_size = 14
font = ImageFont.truetype('msyh.ttf',font_size)
request.session['verify'] = ''
#新建画布
im = Image.new('RGB',(img_width,img_height),background)
draw = ImageDraw.Draw(im)
code = random.sample(string['litter'],4)
#code = u'和谐社会'
#新建画笔
draw = ImageDraw.Draw(im)
#画干扰线
for i in range(random.randrange(3,5)):
xy = (random.randrange(0,img_width),random.randrange(0,img_height),
random.randrange(0,img_width),random.randrange(0,img_height))
draw.line(xy,fill=line_color,width=1)
#写入验证码文字
x = 2
for i in code:
y = random.randrange(0,10)
draw.text((x,y), i, font=font, fill=random.choice(font_color))
x += 14
request.session['verify'] += i
del x
del draw
buf = cStringIO.StringIO()
im.save(buf,'gif')
buf.seek(0)
print str(request.session['verify'])
return HttpResponse(buf.getvalue(),'image/gif')
urls.py如下:
#Check code
url(r'^get_check_code_image/$', 'blog.views.get_check_code_image',name='get_check_code_image'),
html:
<img onclick="this.setAttribute('src','/blog/get_check_code_image/?nocache='+Math.random());" src="/blog/get_check_code_image/" alt="CheckCode"/>
本地测试没有问题 ,能显示出来,但是挂上服务器,就显示不出来。
我用firefox的fireBug看了下,提示“加载指定URL失败”
不知道咋办了?