Source Display
3
Plugin Name: Header Image Generator
5
Plugin URI: http://www.huddledmasses.org/
6
Description: Replaces Post headlines with PNG images of text, from ALA's <a href="http://www.alistapart.com/articles/dynatext/">Dynamic Text Replacement</a>. Now multi-configable.
8
Author URI: http://www.HuddledMasses.org
10
Dynamic Heading Generator
11
By Stewart Rosenberger
12
http://www.stewartspeak.com/headings/
13
http://www.alistapart.com/articles/dynatext/
15
This script generates PNG images of text, written in
16
the font/size that you specify. These PNG images are passed
17
back to the browser. Optionally, they can be cached for later use.
18
If a cached image is found, a new image will not be generated,
19
and the existing copy will be sent to the browser.
21
Additional documentation on PHP's image handling capabilities can
22
be found at http://www.php.net/image/
26
function imagine($text) {
28
global $font_file,$font_size,$left_padding;
29
global $font_color,$background_color,$background_image,$transparent_background;
30
global $cache_url,$cache_folder;
32
global $shadow_count, $shadow_offset, $shadow_color_1, $shadow_color_2;
35
---------------------------------------------------------------------------
36
For basic usage, you should not need to edit anything below this comment.
37
If you need to further customize this script's abilities, make sure you
38
are familiar with PHP and its image handling capabilities.
39
---------------------------------------------------------------------------
42
$mime_type = 'image/png' ;
44
$send_buffer_size = 4096 ;
48
// check for GD support
49
if(!function_exists('ImageCreate') ) {
50
if( $DebugImgHead ) echo('Error: Server does not support PHP image generation') ;
52
if(get_magic_quotes_gpc())
53
$text = stripslashes($text) ;
55
// look for cached copy, send if it exists
56
$hash = md5(basename($font_file) . $font_size . $font_color . $background_color . $transparent_background . $text) ;
58
// try making the folder, if it doesn't exist (just trying to be helpful)
59
if( !file_exists( $cache_folder ) ) {
60
if( !mkdir ( $cache_folder, 0755 ) ) {
61
if($DebugImgHead) echo( "Cache Folder doesn't exist, and I can't create it." );
65
$cache_filename = $cache_folder . '/' . $hash . $extension ;
66
$cache_url = $cache_url . '/' . $hash . $extension ;
68
if( file_exists( $cache_filename ) )
70
list($width, $height, $type, $attr) = getimagesize($cache_filename);
71
$retVal = "<img src='$cache_url' alt='$text' width='$width' height='$height' />" ;
72
} elseif( is_writable( $cache_folder ) ){
74
// check font availability
75
$font_found = is_readable($font_file) ;
77
if( $DebugImgHead ) echo('Error: The server is missing the specified font.') ;
81
if( empty( $left_padding ) ) {
85
$background_rgb = hex_to_rgb($background_color, $DebugImgHead) ;
86
$font_rgb = hex_to_rgb($font_color, $DebugImgHead) ;
88
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
89
$dip = get_dip($font_file,$font_size);
91
// Variables which tell the correct width and height
92
$box["width"] = $left_padding + max($box[0],$box[2],$box[4],$box[6]) - min($box[0],$box[2],$box[4],$box[6]) + 1;
94
$box["height"] = max($box[1],$dip,$box[5],$box[7]) - min($box[1],$dip,$box[5],$box[7]);
96
$image = @ImageCreate($box["width"] + (($shadow) ? ($shadow_offset * $shadow_count) : 0),$box["height"] + (($shadow) ? ($shadow_offset * $shadow_count) : 0));
98
if( !$image || !$box ) {
99
if( $DebugImgHead ) echo('Error: The server could not create this heading image.') ;
102
// allocate colors and draw text
103
$background_color = @ImageColorAllocate($image,$background_rgb['red'], $background_rgb['green'], $background_rgb['blue'] ) ;
104
$font_color = @ImageColorAllocate($image,$font_rgb['red'], $font_rgb['green'], $font_rgb['blue']) ;
106
if( !empty( $background_image ) && is_readable( $background_image ) ) {
107
list($widthi, $heighti, $typei, $attri) = getImageSize($background_image);
108
$backgroundimage = ImageCreateFromPNG( $background_image ); //NOTE use png for this
109
ImageColorTransparent($backgroundimage,$background_color);
111
// merge the two together with alphablending on!
112
ImageAlphaBlending($backgroundimage, true);
114
ImageCopyMerge ( $image, $backgroundimage, 0, 0, 0, 0, $widthi, $heighti, 99);
116
if( $debug ) echo "Image not found: $background_image is apparently missing.";
119
if($shadow_count > 0) {
120
if($shadow_count > 1) {
121
$shadow_rgb = hex_to_rgb($shadow_color_2, $DebugImgHead) ;
122
$shadow_color_2 = @ImageColorAllocate($image,$shadow_rgb['red'], $shadow_rgb['green'], $shadow_rgb['blue']) ;
123
ImageTTFText($image, $font_size, 0, ($left_padding - $box[0]) + ($shadow_offset * $shadow_count), (abs($box[5]-$box[3]) - $box[1]) + ($shadow_offset * $shadow_count), $shadow_color_2, $font_file, $text) ;
125
$shadow_rgb = hex_to_rgb($shadow_color_1, $DebugImgHead) ;
126
$shadow_color_1 = @ImageColorAllocate($image,$shadow_rgb['red'], $shadow_rgb['green'], $shadow_rgb['blue']) ;
127
ImageTTFText($image, $font_size, 0, ($left_padding - $box[0]) + $shadow_offset, (abs($box[5]-$box[3]) - $box[1]) + $shadow_offset, $shadow_color_1, $font_file, $text) ;
129
//ImageTTFText($image, $font_size, 0, $left_padding - $box[0], abs($box[5]-$box[3])-$box[1], $font_color,$font_file,$text) ;
130
ImageTTFText($image, $font_size, 0, $left_padding - $box[0], abs($box[5]-$box[3])-$box[1], $font_color, $font_file, $text) ;
133
if($transparent_background)
134
ImageColorTransparent($image,$background_color) ;
136
@ImagePNG($image,$cache_filename) ;
137
ImageDestroy($image) ;
138
if( file_exists( $cache_filename ) )
140
$retVal = "<img src='$cache_url' alt='$text' width='".$box['width']."' height='".$box['height']."' />" ;
142
if( $DebugImgHead ) echo( "Unknown Error creating file." );
148
if( $DebugImgHead ) echo( "Error: The Folder [".dirname( $cache_filename )."] is not writeable." );
156
try to determine the "dip" (pixels dropped below baseline) of this
159
function get_dip($font,$size)
161
$test_chars = 'abcdefghijklmnopqrstuvwxyz' .
162
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
164
'!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
165
$box = @ImageTTFBBox($size,0,$font,$test_chars) ;
171
decode an HTML hex-code into an array of R,G, and B values.
172
accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
174
function hex_to_rgb($hex, $DebugImgHead)
177
if(substr($hex,0,1) == '#')
178
$hex = substr($hex,1) ;
180
// expand short form ('fff') color
181
if(strlen($hex) == 3)
183
$hex = substr($hex,0,1) . substr($hex,0,1) .
184
substr($hex,1,1) . substr($hex,1,1) .
185
substr($hex,2,1) . substr($hex,2,1) ;
188
if(strlen($hex) != 6 && $DebugImgHead ) {
189
echo('Error: Invalid color "'.$hex.'"') ;
193
$rgb['red'] = hexdec(substr($hex,0,2)) ;
194
$rgb['green'] = hexdec(substr($hex,2,2)) ;
195
$rgb['blue'] = hexdec(substr($hex,4,2)) ;
200
// ***********************************************************
201
// ******* EDIT BELOW HERE ONLY
205
function imagecats($text) {
207
global $font_file,$font_size,$left_padding,$font_color,$background_color,$background_image,$transparent_background,$cache_url,$cache_folder,$DebugImgHead;
208
global $shadow_count, $shadow_offset, $shadow_color_1, $shadow_color_2;
210
// THIS TURNS SHADOWS OFF
211
$shadow_count = 0; // zero, one or two
212
$shadow_offset = 0; // offset for ONE shadow.
213
$shadow_color_1 = $shadow_color_2 = '';
215
// If you have problems, set this to TRUE and see what pops up ;-)
216
$DebugImgHead = true;
218
// the settings we need...
219
$font_file = $_SERVER['DOCUMENT_ROOT'] . '/styles/fonts/bell.ttf' ;
221
$font_color = '#ffffff' ;
222
$background_color = '#336699' ;
224
$background_image = ''; //$_SERVER['DOCUMENT_ROOT'] . '/images/HeadlineSplash.png' ;
225
$transparent_background = true ;
227
// If you call imagine more than once, you should use different caches
228
// Otherwise, if the text matches, you could get the wrong image ;-)
229
$cache_url = '/images/categories-cache' ;
230
$cache_folder = $_SERVER['DOCUMENT_ROOT'] . $cache_url ;
232
// get/make an image for this text.
233
// there's a bug in list_cats,
234
// it gets called once for each cat, and then again for ALL of them?
235
if( preg_match ( "<li>", $text ) > 0 )
238
return imagine( $text );
245
function imageheadlines($text) {
247
global $font_file,$font_size,$left_padding,$font_color,$background_color,$background_image,$transparent_background,$cache_url,$cache_folder,$DebugImgHead;
248
global $shadow_count, $shadow_offset, $shadow_color_1, $shadow_color_2;
250
// FOR THE HEADLINES ONLY
251
// It seems important to only convert the title in the actual title.
252
// Otherwise, you could end up with <img> tags in attributes or RSS feeds...
253
// So, put some value in here, and change your "the_title()" call accordingly
254
// That is, if you set $before = "-image-",
255
// your call to the_title should be: the_title("-image-");
257
// If you have $before set, and it doesn't match, we're done.
258
if( !empty($before) && ( strpos($text, $before) === FALSE ) ) {
261
if( !empty($before) ) {
262
$text = substr( $text, strlen($before) );
265
// If you have problems, set this to TRUE and see what pops up ;-)
266
$DebugImgHead = false;
269
$shadow_count = 2; // zero, one or two
270
$shadow_offset = 1; // offset for ONE shadow.
271
$shadow_color_1 = '#cccccc';
272
$shadow_color_2 = '#eeeeee';
274
// the settings we need...
275
$font_file = $_SERVER['DOCUMENT_ROOT'] . '/styles/fonts/pristina.ttf' ;
277
$font_color = '#336699' ;
278
$background_color = '#ffffff' ;
281
$background_image = $_SERVER['DOCUMENT_ROOT'] . '/images/HeadlineSplash.png' ;
282
$transparent_background = true ;
284
// If you call imagine more than once, you should use different caches
285
// Otherwise, if the text matches, you could get the wrong image ;-)
286
$cache_url = '/images/headlines-cache' ;
287
$cache_folder = $_SERVER['DOCUMENT_ROOT'] . $cache_url ;
289
// get/make an image for this text.
290
return imagine( $text );
294
// Notice how simple it is to use this plugin multiple times...
295
// If you wanted to be able to turn it off separately from the control panel ...
296
// You could put the imagecats method, and the second line below
297
// Into a separate plugin file
298
add_filter('the_title', 'imageheadlines', 12);
299
add_filter('list_cats', 'imagecats', 12);