#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-

import os
import time
import gd 
from mod_python import psp


IMAGEPATH = "/home/httpd/html/images/date.png"

def index(req):
    """Browser request. Run template file."""
    
    make_png()
    req.content_type = 'text/html'
    tmpl=psp.PSP(req, filename='png.html')
    tmpl.run()
	
	
def make_png():
    """Creates an image as png from gd module and writes it to disk."""
    
    os.environ["GDFONTPATH"] = "/home/httpd/fonts/"
    FONT = "adventure"
    
    
    # Specify the image size
    
    im = gd.image((350, 200))

    
    # Take care of colors
    
    white = im.colorAllocate((255, 255, 255))
    black = im.colorAllocate((0, 0, 0))
    red = im.colorAllocate((255, 0, 0))
    blue = im.colorAllocate((0, 0, 255))

    
    # Set image color.
    
    im.colorTransparent(white)
    im.interlace(1)
    im.rectangle((0, 0), (350, 199), black)
    im.arc((150, 100), (195, 175), 0, 0, blue)
    im.fill((1, 100), red)


    # Add the text to the image.
    
    im.string_ttf(FONT, 20.0, 0.0, (10, 100), time.asctime(), blue)
    

    # Write image to file.
    
    f=open(IMAGEPATH, "w")
    im.writePng(f)
    f.close()
