views:

95

answers:

4

I have an ASHX handler that I am using to display images that are stored in a database. I display the images as thumbnails and then full size if the user mouses over them.

How can I combine the images at runtime to produce CSS sprites for use in this situation?

If it can be done does anyone have suggestions on where to start?

UPATE

It seems like most people are saying this is not a good situation to use sprites in. I'm new to the sprite concept so please bear with me.

If I am going to be loading 30 thumbnails on a page from my database everytime why would it not make sense to pass them from the server to the client as one large image instead of passing 30 individual images? Wouldn't this be faster? Isn't this the purpose of CSS Sprites?

+2  A: 

As far as the browser is concerned, an HTTP resource is an HTTP resource and it is irrelevant if the server produced it by reading a file from a hard disk, taking data out of a database, or spewing the content of a random number generator through an algorithm that would output valid PNG data.

You just need to generate your images from the data as normal.

That said, since the images are content, CSS would be an inappropriate tool to include them in the document. You should use an <img> element.

David Dorward
Not sure where you got the idea that CSS is an inappropriate mechanism for including images... Especially hover versions...
Chris Lively
Semantically, if the image is considered content, it should be in an img tag. If it's part of the layout, then it should be CSS.
VirtuosiMedia
The clues are in the names: Cascading STYLE sheets and BACKGROUND-image.
David Dorward
+1 for the right way to solve the problem, even though it's not the answer to the question.
rockinthesixstring
Interesting, I thought this would be a good situation to use sprites. Could you take a look at my update and help me understand?
Abe Miessler
@David Dorward: I'm going to ignore the part where a STYLE sheet applies formatting by changing the BACKGROUND-image... And instead say that I believe I have misinterpreted what the OP was trying to accomplish. I thought he was trying to combine the thumbnail with the fullblown image. That could certainly be handled via a style sheet to define which part is displayed. However, that doesn't appear to be the case and I've updated my answer accordingly.
Chris Lively
CSS is like wrapping paper. When you take it away, the important stuff needs to remain behind. The images are important stuff, they are the content.
David Dorward
A: 

I completely agree with David. Just a quick note regarding David's last point: That's only if the images are content. However, if they were part of the layout, then CSS would be appropriate.

That said, with this use case, sprites aren't a good choice. One of the purposes of thumbnails is to cut down loading time, which a sprite would make worse for a gallery. A better pattern might be using a lightbox or something similar with two images rather than one, with the larger being requested on demand.

VirtuosiMedia
I agree that sprite usage in this case is a bad idea. If sprites are used, then you have a high possibility that MOST of the data which is sent to the browser is ignored. Which would be a complete waste of network resources.
Chris Lively
Good point, Chris.
VirtuosiMedia
Why would using a sprite to display thumbnails make things worse?
Abe Miessler
@Abe, It would make things worse because this means loading the larger view for all the images which might not even be needed (they are hidden until the user hovers on them). The smaller previews would also be prevented from showing until the whole sprite is loaded. This defeats any optimization for pre-loading the smaller thumbnails, and THEN the larger images as either late-loading (window.onload) or lazy-loading (on hover).
Andrew Vit
+1  A: 

You have a couple options.

  1. Your handler can combine the images on the fly that it gets from the database and send the whole thing down to the browser.

OR (and I like this one better)

  1. You create the merged image at the time the images are uploaded to your site.

The second is better as the conversion only has to happen once and therefore means that you only have to spend those resources once. It does mean you are essentially storing 2 copies of the image, but that's fine.

UPDATE

I believe I misinterpreted what you were trying to do. I thought you were trying to combine the thumbnail with the full blown image. Instead, you appear to be really asking how to combine all of the thumbnail images.

In that case, it's even more of a bad idea. As David Dorward stated CSS is used to control layout. You're talking about content. However, the semantic issue aside, in the event you want to make tweaks to the layout your going to have to modify your code which creates the sprites to begin with. What if you decide to do 35 images? Or, change that to do 18?

By going the sprite route your pretty well screwed by being forced to modify code for any layout change which is NOT good style.

To cover that last question: wouldn't it be faster? Probably not. Under this scenario you would have to create the sprite on the fly, which introduces server overhead, which slows everything down. At most it might be a wash in the delivery time. At worst, you incur a large server and development performance negative impact.

Chris Lively
As an answer to the question, I think this is the right answer - +1. However, in a fundamental practice, I think sprites are the wrong way of going about the problem.
rockinthesixstring
@rockinthesixstring: See my comment to VirtuosiMedia's post. I agree. Sprites in this usage pattern are a bad idea.
Chris Lively
Agreed the second option is better, because if this is for performance optimization, you don't want to be generating images on the fly. Still, I think this is not a good use case for sprites.
Andrew Vit
A: 

Sprites are not a good solution here.

To answer your update, sprites are ideal for many small images, where the overhead of a new HTTP request outweighs the few bytes being sent for a small png or gif (e.g. 16x16 icons, etc). For larger images the time of the HTTP request becomes less important overall as the download time increases.

Packing images into a sprite also means that they will execute one longer request and other requests will have to queue behind it. If the important thing is to get the thumbnails showing quickly, then make sure those get loaded first before starting to load any larger views of the same images.

Any larger files that don't display at the initial page load should be late-loaded (window.onload) or lazy-loaded (as needed by click or hover actions).

Andrew Vit