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
- Browser-native date input field by kytta 1 month, 1 week ago
- Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
- My firs Snippets by GutemaG 1 month, 3 weeks ago
- FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
- LazyPrimaryKeyRelatedField by LLyaudet 3 months ago
Comments
Please login first before commenting.