views:

108

answers:

4

Right now I'm base 64 encoding them and using data uris. The idea was that this will somehow lower the number of requests the browser needs to make. Does this bucket hold any water?

What is the best way of serving images in general? DB, from FS, S3?

I am most interested in python and java based answers, but all are welcome!

+2  A: 

Right now I'm base 64 encoding them and using data uris. The idea was that this will somehow lower the number of requests the browser needs to make. Does this bucket hold any water?

This is most definitely a bad idea: It won't work in IE < 8; it increases the data volume served by 33%; and it makes the images completely uncacheable.

I'd say you should serve images as proper image resources - whether as separate files, or maybe as CSS sprites as @Nick suggests, will depend on their quantity and size.

Pekka
data URIs are cached as part of the css file. I assume you mean you have to re-download all the images whenever the css changes. Also, Base64 compresses well so I doubt the extra data volume is that large.
Gabe Moothart
@Gabe the way I understand it, he is showing the base64 data in the HTML file, not in the style sheet?
Pekka
While I agree with you technically, there are times when reducing the number of server hits is more desirable than saving a little bit on caching, or volume. I admit that especially when paired with browser requirements, the cases will be thin, but still present.
McKay
@McKay then CSS sprites are still the much preferable option IMO - the main reason being browser compatibility. Who can afford to not support IE8? Still, I agree there are cases where this can make sense, no doubt.
Pekka
Yeah, I agree that CSS Sprites are a good idea. A great one in fact. But Data URLs aren't universally a bad idea. Also Data URLs (for images) are supported in IE8, but aren't supported in previous browsers. And people who can afford to not support it? that might include a very technical audience, internal applications with browser choice controlled by the corporation, a mac website...
McKay
@McKay you're right, I meant IE7 of course. Yes, all valid points.
Pekka
+13  A: 

I would definitely take a look at CSS Image Sprites, decent write-ups here and here.

The concept is pretty simple, combine your images into one, show only the slice you need as a CSS background. This lets you lower the number of HTTP requests from many images into one or more (group your small images in sprite maps as appropriate) and have fewer images to maintain, the CSS just has some background coordinates in there.

Also, as with any static resource, make sure your cache headers are set correctly, so the client isn't fetching it needlessly.

Nick Craver
+1 i'd do that... although sprites are hard to maintain, its probablly the best solution
pleasedontbelong
@pleasedontbelong - It depends on your image editor I suppose, I personally use Fireworks and find them very easy to maintain, I just export a flattened PNG for the final sprite...have everything editable in the original/source file.
Nick Craver
More info from my answer to another CSS sprite question: "Base64-encoded data is about 1/3 larger than raw bytes, so on pages where downloading all the image data takes more than three times as long as making a request, CSS sprites are superior from a performance standpoint." http://stackoverflow.com/questions/3525663/css-sprites-vs-data-uris/3525961#3525961
Chuck
+1  A: 

Data urls will definitely reduce the number of requests to the server, since the browser doesn't have to ask for the pixels in a separate request. But they are not supported in all browsers. You'll have to make the tradeoff.

Ned Batchelder
+2  A: 

Nicholas C. Zakas has written a tool that makes it easier to use data URIs in css, and also contains an IE6/7 compatible fix:

CSSEmbed also supports an MHTML mode to make IE6 and IE7 compatible stylesheets that use internal images similar to data URIs.

Gabe Moothart