txt2img tag shows on the web text as images, helping to avoid get indexed email address and some other information you don't want to be on search engines.
Usage:
{{worker.email|txt2img:18|safe}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from django.conf import settings
from django.template import Library
import Image, ImageDraw, ImageFont
from os import path
import hashlib
register = Library()
@register.filter
def txt2img(text,FontSize=14,bg="#ffffff",fg="#000000",font="FreeMono.ttf"):
'''
txt2img tag shows on the web text as images, helping to avoid get
indexed email address and some other information you don't want
to be on search engines. Fonts should reside in the same folder of txt2img.
Usage:
{{worker.email|txt2img:18|safe}}
'''
font_dir = settings.MEDIA_ROOT+"/txt2img/" # Set the directory to store the images
img_name_temp = text+"-"+bg.strip("#")+"-"+fg.strip("#")+"-"+str(FontSize) # Remove hashes
img_name="%s.jpg" % (hashlib.md5(img_name_temp).hexdigest())
if path.exists(font_dir+img_name): # Make sure img doesn't exist already
pass
else:
font_size = FontSize
fnt = ImageFont.truetype(font_dir+font, font_size)
w, h= fnt.getsize(text)
img = Image.new('RGBA', (w, h), bg)
draw = ImageDraw.Draw(img)
draw.fontmode = "0"
draw.text((0,0), text, font=fnt, fill=fg)
img.save(font_dir+img_name,"JPEG",quality=100)
imgtag = '<img src="'+settings.MEDIA_URL+'txt2img/'+img_name+'" alt="'+text+'" />'
return imgtag
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.