#! /usr/bin/python ########################################## # The Blogsticker Factory # # Version 1.1 # # (c) 2002 by Andrea Roceal James # ########################################## import Image, cgi, sys, StringIO, DateTime import ImageDraw, ImageFont from string import find, split, strip, rstrip, upper, replace # Convert from hex RGB values (as strings) to a 3-integer tuple def ConvertColor(Red, Green, Blue): HEX = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'A':10,'B':11,'C':12,'D':13,'E':14,'F':15} intRed = 0 intGreen = 0 intBlue = 0 if (len(Red) == 2) and HEX.has_key(Red[0]) and HEX.has_key(Red[1]): intRed = (HEX[Red[0]] * 16) + HEX[Red[1]] if (len(Green) == 2) and HEX.has_key(Green[0]) and HEX.has_key(Green[1]): intGreen = (HEX[Green[0]] * 16) + HEX[Green[1]] if (len(Blue) == 2) and HEX.has_key(Blue[0]) and HEX.has_key(Blue[1]): intBlue = (HEX[Blue[0]] * 16) + HEX[Blue[1]] return (intRed,intGreen,intBlue) # Justify text & format into multiple lines, set color & draw def FormatText(strText, objImg, objDraw, strFont, strColor, strBG): # Determine the color of the text & the background textcolor = (0,0,0) if len(strColor) > 5: red = strColor[0:2] green = strColor[2:4] blue = strColor[4:6] textcolor = ConvertColor(red,green,blue) textcolor = textcolor + (255,) bgcolor = (255,255,255) if len(strBG) > 5: red = strBG[0:2] green = strBG[2:4] blue = strBG[4:6] bgcolor = ConvertColor(red,green,blue) bgcolor = bgcolor + (255,) # Load a font font = ImageFont.load('fonts/luBS10.pil') try: font = ImageFont.load(strFont) except IOError: pass # Check the text size & center the text in the image strTestText = replace(strText, '\n', '') strTestText = replace(strTestText, '\015', ' ') textwidth, textheight = objDraw.textsize(strTestText, font=font) x, y = objImg.size lstLines = [] textlength = len(strTestText) ratio = float(textwidth) / float(textlength) if find(strText, '\015') > 0: lstLines = split(strText, '\015\012') elif find(strText, '\n') > 0: lstLines = split(strText,'\n') elif textwidth > x: linesize = int((1.0/ratio) * x) lstWords = split(strText, ' ') strLine = "" while len(lstWords) > 0: if len(strLine + lstWords[0]) < linesize: strLine = strLine + lstWords.pop(0) + " " elif strLine: lstLines.append(rstrip(strLine)) strLine = lstWords.pop(0) + " " else: strLine = lstWords.pop(0) + " " lstLines.append(rstrip(strLine)) else: lstLines.append(strText) maxlength = len(lstLines[0]) if len(lstLines) > 1: i = 1 while i < len(lstLines): if len(lstLines[i]) > maxlength: maxlength = len(lstLines[i]) i = i + 1 maxwidth = int((maxlength + 2) * ratio) + 6 maxheight = (len(lstLines) * (textheight + 2)) + 8 objImg = Image.new('RGBA', (maxwidth,maxheight), bgcolor) objDraw = ImageDraw.Draw(objImg) i = 0 for Line in lstLines: xt = (maxwidth - objDraw.textsize(Line,font=font)[0])/2 yt = (maxheight - (textheight * len(lstLines)))/2 + (textheight * i) objDraw.text((xt,yt), strip(Line), fill=textcolor,font=font) i = i + 1 return(objImg) # Get the fields posted to this script inQry = cgi.FieldStorage() #Check for a specified maximum width maxwidth = 150 if inQry.has_key('maxwidth'): maxwidth = int(inQry['maxwidth'].value) # Create a new image object, and create a draw object for adding text, border img = Image.new('RGBA', [maxwidth,40], (255,255,255,255)) draw = ImageDraw.Draw(img) # Default sticker text strText = 'WWW.BLOGSTICKERS.COM' # Get the text submitted to the script if inQry.has_key('stickertext'): strText = upper(inQry['stickertext'].value) # Check for background and text color strTxtCol = strBGCol = "" if inQry.has_key('textcolor'): strTxtCol = inQry['textcolor'].value if inQry.has_key('bgcolor'): strBGCol = inQry['bgcolor'].value # Check for a font strFont = "fonts/luBS10.pil" if inQry.has_key('font'): strFont = inQry['font'].value img = FormatText(strText, img, draw, strFont, strTxtCol, strBGCol) draw = ImageDraw.Draw(img) # Draw the border (4 pixels) i = 0 x0 = y0 = i x1 = img.size[0] - 1 y1 = img.size[1] - 1 while i < 4: draw.rectangle([x0,y0,x1,y1], outline=(0,0,0,255)) i = i + 1 x0 = y0 = (x0 + 1) x1 = x1 - 1 y1 = y1 - 1 strCorners = "sharp" # If rounded corners are requested, add the transparency if inQry.has_key('corners'): strCorners = inQry['corners'].value if strCorners == "round": r,g,b,a = img.split() adraw = ImageDraw.Draw(a) x0 = y0 = 0 x1,y1 = img.size x1 = x1 - 1 y1 = y1 - 1 adraw.polygon([(x0,y0),(x0+1,y0),(x0,y0+1)],outline=0) adraw.polygon([(x1,y0),(x1-1,y0),(x1,y0+1)],outline=0) adraw.polygon([(x0,y1),(x0+1,y1),(x0,y1-1)],outline=0) adraw.polygon([(x1,y1),(x1-1,y1),(x1,y1-1)],outline=0) img = Image.merge('RGBA',(r,g,b,a)) # Write out the image temp = StringIO.StringIO() img.save(temp, 'PNG') # Make sure the image isn't cached dtGMT = DateTime.gmtime() strExpires = dtGMT.Format('%a, %d %b %Y %H:%m:%S') + " GMT\n\n" sys.stdout.write('Content-type: text/plain\n') sys.stdout.write('Expires:' + strExpires) sys.stdout.write(temp.getvalue())