Linux.com

Feature: Tools & Utilities

Float irregular images on your Web pages with pngslice

By Ben Martin on October 15, 2008 (9:00:00 AM)

Share    Print    Comments   

Web sites that run text squarely around images even when the images don't have even borders look a little lazy. pngslice slices an image into thin vertical images and generates a small chunk of HTML to align these slices so that the original image can be seen in a Web browser. This lets you place non-rectangular floating images on Web pages and align the surrounding text to the uneven borders of the image for a professional-looking layout.

While you may be able to achieve a similar effect in other ways, if you want to have your Web site viewable in both a wide variety of browsers and versions of those browsers, using image slices is an effective technique.

pngslice 0.65 is not packaged for Ubuntu Hardy, openSUSE, or Fedora, but it's easy to build with the normal ./configure && make && sudo make install process.

To show how it works, I'll use this image, example-image.png. If you invoke pngslice on the image file directly without any other arguments, the utility will create 17 vertical slices called example-image.01.png through example-image.17.png, and an HTML file called example-image.sliced.html that contains the HTML to include all the vertical slices and recreate the original image in your Web browser.

Images are included without any paths or URLs; it is expected that they are in the current directory if you are serving this sliced image from your Web site. The command shown below will prepend the foo.example.com/images URL to the path of each image in the generated example-image.sliced.html file.

pngslice -i 'http://foo.example.com/images/' example-image.png

An alternative to the -i option is to use the -b option to specify the basename for your images and sliced HTML file. If you use -b then the paths to the images also contain the basename exactly as you supplied it on the command line. The -b option might not be as useful as it might seem, however, because the generated HTML file will use the basename you supply when it includes the sliced images. For example, the below invocation will create an HTML file at /tmp/images/foo.sliced.html that references the slice images using image paths like src="http://bar.example.com//tmp/images/foo.05.png". As you can see, you can have conflicts when you use the basename to specify both where output files are written and as part of the image path in the generated HTML.

$ mkdir -p /tmp/images $ pngslice \ -b /tmp/images/foo \ -i http://bar.example.com \ example-image.png

Here's another potential problem. If you use pngslice on a PNG image file that contains transparency, pngslice will treat any pixels that are totally transparent as being expendable. If your image has no transparency information in it, pngslice will treat fully white as expendable. In the example image of balls shown above, the middle of the image on the right side is totally transparent, so pngslice will not include them in the vertical image slices for the middle of the image. This will make the image slices for the middle not as wide as for the top and bottom, and thus text can start more toward the left of the page to fill in the space between the top and bottom balls. You can chose what color(s) or transparency intensities are expendable using the -t option to pngslice. The argument takes the form r/g/b(/a), where each component is a integer between 0 and 255, a range in the form min-max, or a full stop (.) to indicate any value.

If you find that your text gets too close to the image slices, you can tell pngslice to include a margin around the image using the -m parameter. Its arguments are either a single digit specifying both margins, or a digit/digit combination specifying the left/right margins. The digits specify how many units of em to make the margin. An em is roughly the width of the letter m.

You can also choose to right-justify the image, so that the image will appear on the right side of the Web page and text will be on the left, flowing into the image where it is transparent. The default is for a left-justified image.

To use the image slices, simply include the generated HTML file into your PHP page where you would like the image to appear, as shown in the below example and following screenshot.

... <h1>linux.com test of pngslice</h1> <?php include('example-image.sliced.html'); ?> <p> pngslice is not packaged for Hardy, ...

Each time you run pngslice, the HTML file that includes all the image slices is rewritten. It makes sense to include this HTML file on the server side so that changes are automatically propagated to the Web site, rather than copying and pasting the slice HTML directly into your site.

Creating Web sites that render properly in all modern browsers is a difficult enough task -- creating a site that also renders reasonably well in older generation browsers is a chore. pngslice helps you have text routed around the rough edges of an image even when your site has to render with older browsers.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   

Comments

on Float irregular images on your Web pages with pngslice

Note: Comments are owned by the poster. We are not responsible for their content.

Lousy Accessibilty

Posted by: Anonymous [ip: 72.179.57.182] on October 15, 2008 01:21 PM
Using this technique without due caution will decrease the accessibility of your site for blind visitors. The program does specify alt text for each of the images it produces, but the alt text is useless -- it consists of "image slice 1", "image slice 2", and so on. Anyone who is listening to this page read aloud via a screen reader will have to sit through multiple repetitions of that. It doesn't convey what the image *is*, or why it's in the web page, or any useful information about the image at all.

The same objection holds for Google Images. Google's spider is blind. It can't see the images in your pages; it needs meaningful ALT text in order to index the image properly in its image search.

The HTML could of course be modified to provide more meaningful alt text. Since the images are actually a single image which has been sliced up, it would probably make sense to specify meaningful alt text on the FIRST of the images, and then blank alt text on any subsequent images. So you would have alt text like this:

<img src="slice1.png" alt="A chemical diagram of coffee." />
<img src="slice2.png" alt ="" />
<img src="slice3.png" alt="" />

Screen readers will then read the alt text on the first image, and ignore it on any subsequent images. Keep that in mind if you're planning to use this tool.

#

Re: Lousy Accessibilty

Posted by: Anonymous [ip: 143.167.4.89] on October 20, 2008 01:33 PM
Hi, I'm the author of pngslice. Thanks for the comments -- I've implemented your suggestions in the latest version
Cheers!

Jim

#

Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 81.178.65.1] on October 15, 2008 05:00 PM
It's a nice idea, but I'm not sure it is the right way to deal with this problem. PNGs are not well supported in IE6 so it doesn't actually fix that problem.

I prefer to use an overlay of sandbag divs. It works cross-browser and you still have just one image which avoids the problem the previous commenter made.

#

Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 70.50.227.202] on October 15, 2008 05:16 PM
Most serious web designers avoid this kind of stupefying frippery like the plague...

#

Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 70.77.17.189] on October 15, 2008 11:56 PM

Most serious web designers avoid this kind of stupefying frippery like the plague...


You might be right, but no one will ever know since you don't explain yourself. How about a useful comment rather than a vacuous snide remark?

#

Re: Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 72.26.142.218] on October 19, 2008 02:54 AM
Even if he explained himself, only serious web developers would understand.

#

Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 88.72.167.142] on October 16, 2008 08:31 AM
it's a good idea, i just wonder about getting it to play well with templating engines.

#

Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 162.99.35.71] on October 16, 2008 06:48 PM
Instead of slicing up images, why not put each line of text inside a different tag, and set the background-image of that tag to the image with a different offset and left margin?
That way you only load one image, and is easier to make changes to it.

#

Re: Float irregular images on your Web pages with pngslice

Posted by: Anonymous [ip: 162.99.35.71] on October 16, 2008 06:50 PM
Er. Left padding, not margin. Sorry.
And, yeah, image slicing does allow the text to be more easily changed.
One the other hand, not the image. Trade-offs....
One judgement might be the granularity of the text to the image I suppose.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya