NOTE: Hello recent visitors! Please note that the current title images that you see here are the result of completely revamped version of the plugin that supports soft shadows among other things. Please visit this page for more information.
As you have probably noticed, I decided to waste even more of the precious Internet bandwidth on vanity by moving to auto-generated images for entry title text. I’ve always liked the look of those people who have done it — Matt is one — but never jumped on the bandwagon.
Now I find a rather easy-to-use WordPress plugin by Joel Bennett and figured “why not?” It really was just drop-in simple. However, my site is relatively narrow in design, and some of my longer titles didn’t fit. There was no word-wrap support for longer titles.
So I added it! I tell you, there’s a lot of power there with PHP. I found some examples that people had made for word-wrapping based on the bounding box of a rendered text, but none of them were “nice enough” for me. So I did it my way, with support for indenting subsequent lines and specifying the vertical space between lines.
How was this done?
Let’s see if my code handling is up for this. Here’s how I split the single text title into an array of lines that fit within the stated width:
function break_text_into_lines( $text )
{
global $font_file,$font_size,$left_padding;
global $max_width, $space_between_lines, $line_indent;
// the returned array of strings to be on separate lines.
$text_array = array();
// Figure out how big a space is. Yes, I'm being anal.
$bbox = imagettfbbox($font_size,0, $font_file, ' ');
$space_width = max($bbox[2],$bbox[4]) - min($bbox[0],
$bbox[6]);
// Split the array into word components.
$words = explode( ' ', $text );
$current_line = '';
$current_width = $left_padding;
foreach( $words as $word )
{
$bbox = imagettfbbox($font_size, 0, $font_file, $word );
$word_width = max($bbox[2],$bbox[4]) - min($bbox[0],
$bbox[6]);
// See if the current word will fit on the line.
if( $word_width + $current_width + $space_width >
$max_width )
{
// It won't. Check the border case where we have a
// friggin' huge first word. If so, it'll have to
// be rendered on the line regardless.
if( $current_line != '' )
{
$text_array[] = $current_line;
$current_width = $word_width + $left_padding +
$line_indent;
$current_line = $word;
}
else
{
$text_array[] = $word;
$current_width = $left_padding + $line_indent;
$current_line = '';
}
continue;
}
// Word fits, so append it.
if( $current_line != '' )
{
$current_line .= ' ';
}
$current_line .= $word;
$current_width += $word_width + $space_width;
}
if( $current_line != '' )
{
$text_array[] = $current_line;
}
return $text_array;
}I’m really happy with the results. The full plugin is available here. I told the author of the original plugin that he’s welcome to roll the changes into his plugin, so it may show up there in the future. For those who want my version, feel free to grab it.




苟世录 Says:April 8th, 2005 at 9:15 pm
自动生成图片标题的WP插件
无意中发现的,http://www.coldforged.org/image-headlines-plugin-for-wordpress-15/
考虑流量不够大,装了可能会消耗很多流量就放弃了。
PS:好像插件需要支持gd。…

Belinda Says:March 15th, 2005 at 12:44 pm
AH….Okay doeky…thanks!

ColdForged Says:March 14th, 2005 at 5:14 pm
Nice site, Belinda. That’s a known problem for which I’m currently trying to come up with a solution.

Belinda Says:March 14th, 2005 at 4:38 pm
Hi there–Can you take a look at my blog? www.belindasplace.org I am using your image replacement plug in and I really like it–but do you see how some of the titles get cut off at the bottom? Any help would be GREATLY appreciated!

Zonekiller Says:March 11th, 2005 at 8:20 pm
Thanks!

ColdForged Says:March 7th, 2005 at 8:44 pm
Don’t know how I missed it but thanks for the browser tips, torgeir!
Thanks! Probably the easiest way is looking at the code for the comments section as well as the CSS file. From there you should be able to get it
.

Zonekiller Says:March 7th, 2005 at 6:28 pm
Hi, great plugin! I’ll add it to my blog ‘to do’ list.
Anyway I’d like to know how to make my gravatars look like yours… they’re very great! Could you please help me?

torgeir Says:February 13th, 2005 at 3:08 am
i rally love this plugin!
you can use http://www.danvine.com/icapture/ to see what your site looks like in safari & then theres http://browsercam.com for all the rest… (but it’s a pay site…)
:torgeir

coldforged.org » Soft-shadowed headline images Says:February 8th, 2005 at 6:32 pm
[…] may swoon, it is allowed. I’ve had the images up there for quite a while, in fact I tweaked an existing plugin in order to get it to work to my lik […]

ColdForged Says:January 14th, 2005 at 4:15 pm
Thanks, Lee. It requires some work, but it is essentially stuff from this plugin and the Beautifier files mentioned therein, but tweaked almost to death. Really, I couldn’t tell you what all I’ve changed to get it to work even as well as it does, but it requires some diddling. Kind of frustrating, really.
Note also that I’m on a fixed-column format here which doesn’t help. In order to have “pretty” code I have to be careful of line lengths. Sometimes that’s a pain
.
Good luck, hope this helps.