开发者

Ajax request to SQL returns hundreds of images to JSON -> CSS Sprites? Base64?

开发者 https://www.devze.com 2023-02-15 04:59 出处:网络
We are writing a Web-based events calendar with thousands of theatre shows, festivals, and concerts in a SQL database.

We are writing a Web-based events calendar with thousands of theatre shows, festivals, and concerts in a SQL database.

The user goes to the Website, performs a search, the SQL server returns JSON, and jQuery code on the client side displays the 200+ events.

Our problem is that each event has an image. If I return URLs, the browser has to make 200+ HTTP GET requests for these tiny (2-4Kb) images.

It is tempting to pack all the images into one CSS sp开发者_运维技巧rite, but since the user searches can return any combination of images, this would have to be done dynamically and we'd lose control over caching. Every search would have to return every image in a brand new CSS sprite. Also, we want to avoid any solution that requires dynamically reading or writing from a filesystem: that would be way too slow.

I'd rather return binary images as part of the JSON, and keep track of which images we already have cached and which we really need. But I can't figure out how. It seems to require converting to Base64? Is there any example code on this?

Thanks for any help you can give! :)


The web site you are using (StackOverflow) has to provide 50 avatars for 50 questions shown in the questions page. How does it do it? Browser makes 50 requests.

I would say, you had better implement pagination so that the list does not get too big. Also you can load images on the background or when the user scrolls and gets to the image.

Keeping track of images downloaded is not our job, it is browser's. Our job is to make sure the URL is unique and consistent and response headers allow caching.

Also base64 will make the stream at least 33% longer while it's processing on the client side is not trivial - I have never seen an implementation but probably someone has done some javascript for it.

I believe all you need is just pagination.


It looks like the original poster has proceeded with essentially this solution on their own, however based on their comment about 33% space overhead, I don't think they observed an unexpected phenomenon that you get when base64-ing a bunch of images into one response entity and then gzipping it...believe it or not, this can easily produce an overall transfer size that is smaller than the total of the unencoded image files, even if each unencoded image was also separately gzipped.

I've covered this JSON technique for accelerated image presentation in depth here as an alternative to CSS sprites, complete with a couple live samples. It aims show that it is often a superior technique to CSS sprites.


Data is never random. For example you could name your sprites

date_genre_location.jpg

or however you organise your searches. This might be worth it. Maybe.

You'll need to do the math


Here is what we decided.

Creating sprites has some downsides:

  • To reduce image loss, we need to store the originals as PNGs instead of JPEGs on the server. This is going to add to the slowness, and there's already quite some slowness in creating dynamic CSS sprites with ImageMagick.

  • To reduce image size, the final CSS sprite must be a JPG, but JPGs are lossy and with a grid of images, things get weird at the edges as JPG tries to blend and compress. We can fix that by putting a blank border around all the images but even so, this adds complexity.

  • with CSS sprites, it becomes harder to do intelligent caching; we have to pack up all the images regardless of what we've sent in the past. (or we have to keep all the old CSS sprites regardless of whether they contain many or few images we still need).

  • I'm afraid we have too many combinations of date-category-location for precomputing CSS sprites, although surely we could handle part of the data this way

Thus our solution is to Base64 it, to actually send each image one by one. Despite the 33% overhead, it is far less complex to code and manage and when you take caching issues into account, it may even be less data transfer.

Thank you all for your advice on this!

0

精彩评论

暂无评论...
验证码 换一张
取 消