Update: I’ve been really tardy about making sure the file’s always available – many apologies. Have migrated it over to Pastebin (and updated the links).
Seems to me that every designer I’ve ever worked with has at one time or another a thing for designing sites with rounded corners everywhere. Reproducing this in HTML is a major PITA on IE, I’ve always ended up with tag spaghetti, and a hacky solution made up with loads of little images that works fine on one browser but not the other.
So, I set about generating them on the fly, and came up with the attached script to do the job for me, using a pair of functions by Ulrich Mierendorff.
Features
- One ore more antialiased corners (see Usage below)
- Background transparency
- Can set background, foreground and stroke colour
- Full control over dimensions
- Server-side caching
- Sends well-formed HTTP headers to promote caching (ETag, Last-Modified, Expires), and returns a “304 Not Modified” header if it’s been cached by the client already.
Todo
- Ability to specify the stroke width dynamically, currently hard coded
- Set a different radius for each corner
- Refactor in to a class to handle everything
- Use the URI to work out whether to serve GIF or PNG (no point in using JPEG for this purpose)
Usage
Firstly, check you’re using PHP 5.1.x (may work on others, as yet untested) and have the GD library installed and working.
Update the cache settings (lines 72-81), make sure your cache folder exists and is writable, or set the CACHE_PATH constant to false.
You should be good to go. The accepted parameters are:
- bg – hex value (without the hash) for the background of the box
- fg – hex value for the foreground of the box
- w – width of the generated image
- h – height of the generated image
- corners – bit array for the corners. Each corner has a numeric value: top left = 2, top right = 4, bottom left = 8, bottom right = 16, add together these values to specify the corners you’d like rounded, so for the top corners only, 2 + 4 = 6. For all four corners, 2 + 4 + 8 + 16 = 30
- style – either “stroke” (coloured line around the outside) or “round” (solid coloured box with rounded corners)
- stroke – the stroke colour (ignored if style == round)
Pass the parameters to the script in the querystring like so:
bg=FFFFFF&fg=990000&w=200&h=100&corners=30&style=stroke&stroke=FF0000
Which will give you an image like this:

I use this script in conjunction with a mod_rewrite rule (linebreaks marked \):
RewriteRule \
^boxes(/bg([0-9a-f]{6}))?/([0-9a-f]{6})/([0-9]+)x([0-9]+) \
/([0-9]{1,2})/(round|stroke)(-([0-9a-f]{6}))?\.png$ \
/background.php?bg=$2&fg=$3&w=$4&h=$5&corners=$6&style=$7&stroke=$9 \
[QSA,L,NC]
Which gives you a nicer looking URI and helps with caching http://www.example.com/boxes/bgFFFFFF/CC0000/400×600/30/stroke-FF0000.png
I use MooTools to look for all elements with a given class, sniff the dimensions, and set the background image dynamically:
window.addEvent('load',
function ()
{
$$('.rounded').each(
function (el)
{
x = el.getSize().x;
y = el.getSize().y;
el.setStyle(
'background',
'transparent url(/boxes/bgFFFFFF/CC0000/'
+x+'x'+y+
'/30/stroke-FF0000.png');
}
);
}
);
Et voila!
Download
One file, available as a gzip’d text file here: http://public.benlancaster.co.uk/background.txt.gz http://pastebin.com/pastebin.php?dl=f6615b455
MD5sum: af6edc069116e6aea90790a682e352f4 009859d57d3677635b15a044cb385de3
License
MIT License:
(c) 2009 Ben Lancaster
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.