| 1 | # Copyright (c) 2007 Brandon Low |
|---|
| 2 | # Licensed under the GPL v2 |
|---|
| 3 | # |
|---|
| 4 | # Some ideas in this file from http://code.google.com/p/django-captcha/ |
|---|
| 5 | from cStringIO import StringIO |
|---|
| 6 | from django.http import HttpResponse, HttpResponseBadRequest |
|---|
| 7 | from models import Captcha |
|---|
| 8 | from settings import * |
|---|
| 9 | from plesonet_captcha.generator.Visual import Tests |
|---|
| 10 | from settings import CAPTCHA_WIDTH, CAPTCHA_HEIGHT |
|---|
| 11 | |
|---|
| 12 | def captcha_image(request,id): |
|---|
| 13 | try: |
|---|
| 14 | c = Captcha.objects.get(pk=id) |
|---|
| 15 | except Captcha.DoesNotExist: |
|---|
| 16 | return HttpResponseBadRequest("Invalid captcha id") |
|---|
| 17 | |
|---|
| 18 | text = c.text |
|---|
| 19 | |
|---|
| 20 | # begin drawing |
|---|
| 21 | # font = ImageFont.truetype(FONT_PATH,FONT_SIZE) |
|---|
| 22 | # # Size of the rendered text |
|---|
| 23 | # (xs,ys) = font.getsize(text) |
|---|
| 24 | # # 2 pixel border around the text |
|---|
| 25 | # (x_size, y_size) = (xs+4,ys+4) |
|---|
| 26 | # image = Image.new('RGB', (x_size, y_size), BGCOLOR) |
|---|
| 27 | # draw = ImageDraw.Draw(image) |
|---|
| 28 | # # Draw the text, starting from (2,2) so the text won't be edge |
|---|
| 29 | # draw.text((2, 2), text, font=font, fill=COLOR) |
|---|
| 30 | # # Make some messes on the image |
|---|
| 31 | # draw.arc((0,y_size/3,x_size,y_size*2), 220, 320, fill=LINE_COLOR) |
|---|
| 32 | # draw.line((0,0)+image.size, fill=LINE_COLOR) |
|---|
| 33 | # end drawing |
|---|
| 34 | |
|---|
| 35 | image = Tests.PseudoGimpy(word=text).render(size=(CAPTCHA_WIDTH, CAPTCHA_HEIGHT)) |
|---|
| 36 | |
|---|
| 37 | # Saves the image in a StringIO object, so you can write the response |
|---|
| 38 | # in a HttpResponse object |
|---|
| 39 | file = StringIO() |
|---|
| 40 | image.save(file,"PNG") |
|---|
| 41 | response = HttpResponse() |
|---|
| 42 | response['Content-Type'] = 'image/png' |
|---|
| 43 | response['Content-Length'] = str(file.tell()) |
|---|
| 44 | file.seek(0) |
|---|
| 45 | response.write(file.read()) |
|---|
| 46 | file.close() |
|---|
| 47 | return response |
|---|