Revision [7079]
This is an old revision of FontActionInfo made by OnkelJonas on 2005-04-03 03:41:13.
The script is from A List Apart/Stewart Rosenberger - I just did some crude hacks to make it work as an action (my PHP skills are extremely limited).
An entry would look like
{{font text="The text goes here" font="FontNameHere"}}
The script has variables for:
- Fontfile (needs to be on server of course)
- Font size (in points)
- Font color
- Background color
- Transparency
- Caching
It is still very rough, but it works.
It passes the image text as both the alt and title tags - I believe this will satisfy accesibility requirements.
It accepts .ttf (TrueType) and .otf (OpenType) font files.
TODO:
- pass font/fontsize/colors/transparency directly to the rendering script (at the moment you need a script for each font/size/color variation).
The action:
- <?php
- /*
- "font" action
- Parameters:
- text - The text to convert, also used for alt and title attributes
- link - The font to use
- */
- {
- foreach ($vars as $param => $value)
- {
- if ($param == 'text') {$text=$this->htmlspecialchars_ent($vars['text']);}
- if ($param == 'font') {$font=$this->htmlspecialchars_ent($vars['font']);}
- }
- }
- $output = "<img src=\"/fonts/".$font.".php?text=".$text."\" alt=\"".$text."\" title=\"".$text."\" />";
- // link?
- if ($link = $vars['link'])
- {
- }
- $output = $this->ReturnSafeHTML($output);
- print($output);
- ?>
The rendering script:
- <?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_file = '/path/to/font' ;
- $font_size = 50 ;
- $font_color = '#000000' ;
- $background_color = '#ffffff' ;
- $transparent_background = true ;
- $cache_images = true ;
- $cache_folder = '/path/to/cache' ;
- /*
- ---------------------------------------------------------------------------
- For basic usage, you should not need to edit anything below this comment.
- If you need to further customize this script's abilities, make sure you
- are familiar with PHP and its image handling capabilities.
- ---------------------------------------------------------------------------
- */
- $mime_type = 'image/png' ;
- $extension = '.png' ;
- $send_buffer_size = 4096 ;
- // check for GD support
- fatal_error('Error: Server does not support PHP image generation') ;
- // clean up text
- fatal_error('Error: No text specified.') ;
- $text = $_GET['text'] ;
- $text = javascript_to_html($text) ;
- // look for cached copy, send if it exists
- $background_color . $transparent_background . $text) ;
- $cache_filename = $cache_folder . '/' . $hash . $extension ;
- {
- exit ;
- }
- // check font availability
- if(!$font_found)
- {
- fatal_error('Error: The server is missing the specified font.') ;
- }
- // create image
- $background_rgb = hex_to_rgb($background_color) ;
- $font_rgb = hex_to_rgb($font_color) ;
- $dip = get_dip($font_file,$font_size) ;
- if(!$image || !$box)
- {
- fatal_error('Error: The server could not create this heading image.') ;
- }
- // allocate colors and draw text
- $background_rgb['green'],$background_rgb['blue']) ;
- $font_rgb['green'],$font_rgb['blue']) ;
- $font_color,$font_file,$text) ;
- // set transparency
- if($transparent_background)
- // save copy of image for cache
- if($cache_images)
- {
- }
- exit ;
- /*
- try to determine the "dip" (pixels dropped below baseline) of this
- font for this size.
- */
- function get_dip($font,$size)
- {
- $test_chars = 'abcdefghijklmnopqrstuvwxyz' .
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
- '1234567890' .
- '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
- 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
- {
- {
- exit ;
- }
- }
- // send 500 code
- 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 '#'
- // expand short form ('fff') color
- {
- }
- fatal_error('Error: Invalid color "'.$hex.'"') ;
- // convert
- return $rgb ;
- }
- /*
- convert embedded, javascript unicode characters into embedded HTML
- entities. (e.g. '%u2018' => '‘'). returns the converted string.
- */
- function javascript_to_html($text)
- {
- $matches = null ;
- return $text ;
- }
- ?>