topical media & game development
professional-search-06-seophp-dynatext-heading.php / php
<?php
/*
Dynamic Heading Generator
By Stewart Rosenberger
http://www.stewartspeak.com/headings/
This script generates PNG images of text, written in
the font/size that you specify. These PNG images are passed
back to the browser. Optionally, they can be cached for later use.
If a cached image is found, a new image will not be generated,
and the existing copy will be sent to the browser.
Additional documentation on PHP's image handling capabilities can
be found at http://www.php.net/image/
*/
font_size = 23 ;
background_color = '#ffffff' ;
cache_images = true ;
mime_type = 'image/png' ;
send_buffer_size = 4096 ;
// check for GD support
if(!function_exists('ImageCreate'))
fatal_error('Error: Server does not support PHP image generation') ;
// clean up text
if(empty(text = text = stripslashes(text = javascript_to_html(hash = md5(basename(font_size . background_color . text) ;
cache_folder . '/' . extension ;
if(file = @fopen(mime_type) ;
while(!feof(buffer = fread(send_buffer_size))) ;
fclose(font_found = is_readable(font_found)
{
fatal_error('Error: The server is missing the specified font.') ;
}
// create image
background_color) ;
font_color) ;
font_file,box = @ImageTTFBBox(font_file,image = @ImageCreate(abs(box[0]),abs(dip)) ;
if(!box)
{
fatal_error('Error: The server could not create this heading image.') ;
}
// allocate colors and draw text
image,background_rgb['green'],font_color = ImageColorAllocate(font_rgb['red'],
font_rgb['blue']) ;
ImageTTFText(font_size,0,-box[5]-box[1],
font_file,transparent_background)
ImageColorTransparent(background_color) ;
header('Content-type: ' . image) ;
// save copy of image for cache
if(image,image) ;
exit ;
/*
try to determine the "dip" (pixels dropped below baseline) of this
font for this size.
*/
function get_dip(size)
{
%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
size,0,test_chars) ;
return message)
{
// send an image
if(function_exists('ImageCreate'))
{
message) + 10 ;
image = ImageCreate(height))
{
image,255,255,255) ;
image,0,0,0) ;
ImageString(message,image) ;
ImageDestroy(message) ;
exit ;
}
/*
decode an HTML hex-code into an array of R,G, and B values.
accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
*/
function hex_to_rgb(hex,0,1) == '#')
hex,1) ;
// expand short form ('fff') color
if(strlen(hex = substr(hex,0,1) .
substr(hex,1,1) .
substr(hex,2,1) ;
}
if(strlen(hex.'"') ;
// convert
hex,0,2)) ;
hex,2,2)) ;
hex,4,2)) ;
return text)
{
text,matches)) for(i<sizeof(i++)
matches[0][matches[1][text) ;
return
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.