Source of: image-headlines.php | remove line numbers | download


Source Display
1
<?php 
2
/* 
3
Plugin Name: Header Image Generator 
4
Version: 1.5 
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. 
7
Author: Joel Bennett 
8
Author URI: http://www.HuddledMasses.org 
9
 
10
    Dynamic Heading Generator 
11
    By Stewart Rosenberger 
12
    http://www.stewartspeak.com/headings/ 
13
    http://www.alistapart.com/articles/dynatext/ 
14
 
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. 
20
 
21
    Additional documentation on PHP's image handling capabilities can 
22
    be found at http://www.php.net/image/ 
23
*/ 
24
 
25
 
26
function imagine($text) { 
27
 
28
   global $font_file,$font_size,$left_padding
29
   global $font_color,$background_color,$background_image,$transparent_background
30
   global $cache_url,$cache_folder
31
   global $DebugImgHead
32
   global $shadow_count$shadow_offset$shadow_color_1$shadow_color_2
33
 
34
/* 
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
  --------------------------------------------------------------------------- 
40
*/ 
41
 
42
   $mime_type 'image/png' 
43
   $extension '.png' 
44
   $send_buffer_size 4096 
45
 
46
   $retVal $text
47
 
48
   // check for GD support 
49
   if(!function_exists('ImageCreate') ) { 
50
      if( $DebugImgHead ) echo('Error: Server does not support PHP image generation') ; 
51
   } else { 
52
      if(get_magic_quotes_gpc()) 
53
          $text stripslashes($text) ; 
54
 
55
      // look for cached copy, send if it exists 
56
      $hash md5(basename($font_file) . $font_size $font_color $background_color $transparent_background $text) ; 
57
 
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_folder0755 ) ) { 
61
            if($DebugImgHead) echo( "Cache Folder doesn't exist, and I can't create it." ); 
62
         } 
63
      } 
64
 
65
      $cache_filename $cache_folder '/' $hash $extension 
66
      $cache_url $cache_url '/' $hash $extension 
67
 
68
      if( file_exists$cache_filename ) ) 
69
      { 
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 ) ){ 
73
 
74
         // check font availability 
75
         $font_found is_readable($font_file) ; 
76
         if( !$font_found ) { 
77
             if( $DebugImgHead ) echo('Error: The server is missing the specified font.') ; 
78
             $retVal $text
79
         } else { 
80
 
81
            if( empty( $left_padding ) ) { 
82
               $left_padding 0
83
            } 
84
            // create image 
85
            $background_rgb hex_to_rgb($background_color$DebugImgHead) ; 
86
            $font_rgb hex_to_rgb($font_color$DebugImgHead) ; 
87
 
88
            $box = @ImageTTFBBox($font_size,0,$font_file,$text) ; 
89
            $dip get_dip($font_file,$font_size); 
90
 
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
93
            // $box[3] = $dip; 
94
            $box["height"] = max($box[1],$dip,$box[5],$box[7]) - min($box[1],$dip,$box[5],$box[7]); 
95
 
96
            $image = @ImageCreate($box["width"] + (($shadow) ? ($shadow_offset $shadow_count) : 0),$box["height"] + (($shadow) ? ($shadow_offset $shadow_count) : 0)); 
97
 
98
            if( !$image || !$box ) { 
99
                if( $DebugImgHead ) echo('Error: The server could not create this heading image.') ; 
100
                $retVal $text
101
            } else { 
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']) ; 
105
 
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); 
110
 
111
                  // merge the two together with alphablending on! 
112
                  ImageAlphaBlending($backgroundimagetrue); 
113
 
114
                  ImageCopyMerge $image$backgroundimage0000$widthi$heighti99); 
115
               } else { 
116
                  if( $debug ) echo "Image not found: $background_image is apparently missing."
117
               } 
118
 
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_size0, ($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) ; 
124
                  } 
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_size0, ($left_padding $box[0]) + $shadow_offset, (abs($box[5]-$box[3]) - $box[1]) + $shadow_offset$shadow_color_1$font_file$text) ; 
128
               } 
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_size0$left_padding $box[0], abs($box[5]-$box[3])-$box[1], $font_color$font_file$text) ; 
131
 
132
               // set transparency 
133
               if($transparent_background
134
                   ImageColorTransparent($image,$background_color) ; 
135
 
136
               @ImagePNG($image,$cache_filename) ; 
137
               ImageDestroy($image) ; 
138
               if( file_exists$cache_filename ) ) 
139
               { 
140
                  $retVal "<img src='$cache_url' alt='$text' width='".$box['width']."' height='".$box['height']."' />" 
141
               } else { 
142
                  if( $DebugImgHead ) echo( "Unknown Error creating file." ); 
143
                  $retVal $text
144
               } 
145
            } 
146
         } 
147
      } else { 
148
         if( $DebugImgHead ) echo( "Error: The Folder [".dirname$cache_filename )."] is not writeable." ); 
149
         $retVal $text
150
      } 
151
   } 
152
   return $retVal
153
154
 
155
/* 
156
    try to determine the "dip" (pixels dropped below baseline) of this 
157
    font for this size. 
158
*/ 
159
function get_dip($font,$size
160
161
    $test_chars 'abcdefghijklmnopqrstuvwxyz' 
162
                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
163
                  '1234567890' 
164
                  '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' 
165
    $box = @ImageTTFBBox($size,0,$font,$test_chars) ; 
166
    return $box[3] ; 
167
168
 
169
 
170
/* 
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 
173
*/ 
174
function hex_to_rgb($hex$DebugImgHead
175
176
    // remove '#' 
177
    if(substr($hex,0,1) == '#'
178
        $hex substr($hex,1) ; 
179
 
180
    // expand short form ('fff') color 
181
    if(strlen($hex) == 3
182
    { 
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) ; 
186
    } 
187
 
188
   if(strlen($hex) != && $DebugImgHead ) { 
189
      echo('Error: Invalid color "'.$hex.'"') ; 
190
   } 
191
 
192
    // convert 
193
    $rgb['red'] = hexdec(substr($hex,0,2)) ; 
194
    $rgb['green'] = hexdec(substr($hex,2,2)) ; 
195
    $rgb['blue'] = hexdec(substr($hex,4,2)) ; 
196
 
197
    return $rgb 
198
199
 
200
// *********************************************************** 
201
// *******  EDIT BELOW HERE ONLY 
202
 
203
 
204
 
205
function imagecats($text) { 
206
 
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
209
 
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 ''
214
 
215
   // If you have problems, set this to TRUE and see what pops up ;-) 
216
   $DebugImgHead true
217
 
218
   // the settings we need... 
219
   $font_file  $_SERVER['DOCUMENT_ROOT'] . '/styles/fonts/bell.ttf' 
220
   $font_size  14 
221
   $font_color '#ffffff' 
222
   $background_color '#336699' 
223
   $left_padding 0
224
   $background_image ''//$_SERVER['DOCUMENT_ROOT'] . '/images/HeadlineSplash.png' ; 
225
   $transparent_background  true 
226
 
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 
231
 
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 ) > 
236
      return $text
237
   else 
238
      return imagine$text ); 
239
240
 
241
 
242
 
243
 
244
 
245
function imageheadlines($text) { 
246
 
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
249
 
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-"); 
256
   $before "-image-"
257
   // If you have $before set, and it doesn't match, we're done. 
258
   if( !empty($before) && ( strpos($text$before) === FALSE ) ) { 
259
      return $text
260
   } else { 
261
      if( !empty($before) ) { 
262
         $text substr$textstrlen($before) ); 
263
      } 
264
 
265
      // If you have problems, set this to TRUE and see what pops up ;-) 
266
      $DebugImgHead false
267
 
268
      // TURNS SHADOWS ON! 
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'
273
 
274
      // the settings we need... 
275
      $font_file  $_SERVER['DOCUMENT_ROOT'] . '/styles/fonts/pristina.ttf' 
276
      $font_size  28 
277
      $font_color '#336699' 
278
      $background_color '#ffffff' 
279
      // MUST be png! 
280
      $left_padding 10
281
      $background_image $_SERVER['DOCUMENT_ROOT'] . '/images/HeadlineSplash.png' 
282
      $transparent_background  true 
283
 
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 
288
 
289
      // get/make an image for this text. 
290
      return imagine$text ); 
291
   } 
292
293
 
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); 
300
 
301
?> 
302