class Myfonts
{
function Myfonts()
{
// konstruktor. Inicialize mainigus.
/* Example of use in article (main) module
$text_img = generate_png_text('Nodrošināt sievietēm visā pasaulē skaistumu...','C:/Works/marycohr/public/img/fonts/1022Eur_.ttf','img_cache',16,'','','','');
$template->set_var('module_data', "
", 'BLOCK_main_text');
Piezime:
"C:/Works/marycohr/public/img/fonts" - direktorija kas satur fonta failus, kurus lietos klase. (Piemeram "1022Eur_.ttf")
"img_cache" - direktorija, kur tiek ievietotas png bildes
*/
}
function javascript_to_html($text)
{
$matches = null ;
preg_match_all('/%u([0-9A-F]{4})/i',$text,$matches) ;
if(!empty($matches)) for($i=0;$i[]{}-+_-=' ;
$box = @ImageTTFBBox($size,0,$font,$test_chars) ;
return $box[3] ;
}
/*
attempt to create an image containing the error message given.
if this works, the image is sent to the browser. if not, an error
is logged, and passed back to the browser as a 500 code instead.
*/
function fatal_error($message)
{
// send an image
if(function_exists('ImageCreate'))
{
$width = ImageFontWidth(5) * strlen($message) + 10 ;
$height = ImageFontHeight(5) + 10 ;
if($image = ImageCreate($width,$height))
{
$background = ImageColorAllocate($image,255,255,255) ;
$text_color = ImageColorAllocate($image,0,0,0) ;
ImageString($image,5,5,5,$message,$text_color) ;
header('Content-type: image/png') ;
ImagePNG($image) ;
ImageDestroy($image) ;
exit ;
}
}
// send 500 code
header("HTTP/1.0 500 Internal Server Error") ;
print($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)
{
// remove '#'
if(substr($hex,0,1) == '#')
$hex = substr($hex,1) ;
// expand short form ('fff') color
if(strlen($hex) == 3)
{
$hex = substr($hex,0,1) . substr($hex,0,1) .
substr($hex,1,1) . substr($hex,1,1) .
substr($hex,2,1) . substr($hex,2,1) ;
}
if(strlen($hex) != 6)
$this->fatal_error('Error: Invalid color "'.$hex.'"') ;
// convert
$rgb['red'] = hexdec(substr($hex,0,2)) ;
$rgb['green'] = hexdec(substr($hex,2,2)) ;
$rgb['blue'] = hexdec(substr($hex,4,2)) ;
return $rgb ;
}
/*
convert embedded, javascript unicode characters into embedded HTML
entities. (e.g. '%u2018' => '‘'). returns the converted string.
*/
/*
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/
*/
//'C:/Works/marycohr/public/img/fonts/embosst3.ttf'
function generate_png_text($_text = 'none', $_font_file, $_cache_folder = 'img_cache', $_font_size, $_font_color,
$_background_color, $_transparent_background, $_cache_images = true )
{
//---- < ---- set function variables ---------------------
$font_file = $_font_file;
if($_font_size=='') $font_size ='50'; else $font_size = $_font_size;
if($_font_color=='') $font_color = '#000000'; else $font_color = $_font_color;
if($_background_color=='') $background_color = '#ffffff'; else $background_color = $_background_color;
if($_transparent_background=='') $transparent_background = true; else $transparent_background = $_transparent_background;
if($_cache_images=='') $cache_images = true; else $cache_images = $_cache_images;
if($_cache_images=='') $cache_images = true; else $cache_images = $_cache_images;
$cache_folder = $_cache_folder;
//---- > ----------------------------------------------------
$mime_type = 'image/png' ;
$extension = '.png' ;
$send_buffer_size = 4096 ;
// check for GD support
if(!function_exists('ImageCreate'))
{
$this->fatal_error('Error: Server does not support PHP image generation') ;
}
// clean up text
if(empty($_text))
{
$this->fatal_error('Error: No text specified.') ;
}
$text = $_text;
if(get_magic_quotes_gpc())
{
$text = stripslashes($text) ;
}
$text = $this->javascript_to_html($text) ;
// look for cached copy, send if it exists
$hash = md5(basename($font_file) . $font_size . $font_color .
$background_color . $transparent_background . $text) ;
$cache_filename = $cache_folder . '/' . $hash . $extension ;
if(isset($cache_images)&& $cache_images && isset($cache_filename) && (file_exists($cache_filename)))
{
// header('Content-type: ' . $mime_type) ;
$file = @fopen($cache_filename,'rb');
while(!feof($file))
{
($buffer = @fread($file,$send_buffer_size));
}
fclose($file) ;
// exit ;
}
// check font availability
$font_found = is_readable($font_file) ;
if(!$font_found)
{
$this->fatal_error('Error: The server is missing the specified font.') ;
}
// create image
$background_rgb = $this->hex_to_rgb($background_color) ;
$font_rgb = $this->hex_to_rgb($font_color) ;
$dip = $this->get_dip($font_file,$font_size) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$image = @ImageCreate(abs($box[2]-$box[0]),abs($box[5]-$dip)) ;
if(!$image || !$box)
{
$this->fatal_error('Error: The server could not create this heading image.') ;
}
// allocate colors and draw text
$background_color = @ImageColorAllocate($image,$background_rgb['red'],
$background_rgb['green'],$background_rgb['blue']) ;
$font_color = ImageColorAllocate($image,$font_rgb['red'],
$font_rgb['green'],$font_rgb['blue']) ;
ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],
$font_color,$font_file,$text) ;
// set transparency
if($transparent_background)
ImageColorTransparent($image,$background_color) ;
// header('Content-type: ' . $mime_type) ;
// ImagePNG($image) ;
// save copy of image for cache
if($cache_images)
{
@ImagePNG($image,$cache_filename) ;
}
ImageDestroy($image) ;
return str_replace($_cache_folder.'/','',$cache_filename);
}
}
?>