For our website we use custom fonts and we want to be sure the end-user sees the content like we designed it. We can't rely on them having installed that same font. It is an old "problem" in webdesign-land. Common solutions are using a Flash SWF to draw the text (SIFR), or running some JavaScript/PHP that invokes the graphics library to draw the text with FreeType.
This made me think: why not use ImageCache to draw the text? It has all the features that we need for that already... except text wrapping.
So I have added a "width" and "lineheight" parameter. If the width of the text is set to zero (default), no text wrapping will take place and lineheight will be ignored. If a width is specified, I wrap the text to the specified width using the code found here:
http://kore-nordmann.de/blog/image_creation_with_php_texts.html
This really is a quick and easy way to generate bitmaps of texts in a custom font. Together with the action to create a transparent canvas it becomes a powerful tool for custom fonts. I would love to see an implementation of this in the Imagecache Actions module. I have attached the file containing my alterations for your reference.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | textactions_wrap-20090217.patch | 4.27 KB | dman |
| textactions.inc_.zip | 7.31 KB | droozle |
Comments
Comment #1
dman commentedSounds like a reasonable request. And it looks like it will do the job.
Visually, the code looks OK - tokenizing is better than some methods I'd seen which were a bit laborious.
Seeing as it was a big hassle to see just what had changed in the file, we like to see patches instead. Don't want to be an ogre, but the reason is because the code can be visually inspected a lot easier. Instead I had to download, extract, move and diff the files myself.
So Here's the patch I ended up with.
It doesn't look dangerous. Next time I'm in a testing mood I may throw it in. I've not actually tried it, just looked at it.
Or someone else can try testing and give it a +1 or RTBC and I'll roll it. I'll flag it as 'needs work' but by that I only mean 'needs testing on a few different systems'. Test results (and GD library details) appreciated.
In other news, I'm hoping for a convergence with signwriter.module (or something) as a place to really handle all these text effects, I don't want to keep building in extras here if I could just use a more common text-rendering API..
Comment #2
droozle commentedThanks for considering it, dman. In all honesty, I only started using Drupal 2 weeks ago, so forgive me for not knowing anything about patches. I will look into them.
I should mention that the wrapping does not completely support rotated ($angle <> 0) text yet. The reason for this is that I simply increase $y_ins with the line height, instead of calculating the x,y for the next line based on the angle. Shouldn't be a very big problem to fix that, though. At least the GD function that returns the bounding box supports the angle and that is a big help. Anyway, I did not test for angles other than 0, just so you know.
Comment #3
dman commentedWell, you found the right place to push and make the change, so that's a big plus!
As well as patches, we also like double spaces instead of tabs and lots of other things.
But what you've provided here is certainly a useful, usable contribution.
I just need to take a little care on testing, rather than roll a partially-working feature. I get in trouble trying to push features upstream to imageAPI if I can't also do the same effect in imageMagick :-/
And it's only when I need/want/get-paid-for the feature that I can test in platforms I'm not using in real life.
Comment #4
brunodboTested this today using Imagecache Actions v6.x-1.6, with PHP5-2 & GD2, on a Ubuntu system. Works fine for my needs, however, 'bottom-up' text wrapping doesn't work, meaning:
- when you position your text to be at the bottom of the image, the first line is always positioned according to the setting you use for the imagecache action
- if there isn't enough space to place wrapped text in the image (since you're using bottom positioning), the wrapped is rendered 'outside' the image, and is not shown.
Comment #5
dman commentedThis code - or a much-mutated variation - is going into the 6.x-2.x-dev new text_render action or rather an imageapi_text library.
Closing this for now, although you won't see the actual results 'till 6.x-2.x-dev is stable