I want to load multiple images very fast on a website, what's the best method?

19,721

Solution 1

If you split large images into smaller parts, they'll load faster on modern browsers due to pipelining.

alt text

Solution 2

ALTERNATIVE: Is there a way to load a compresses file with all imgs and uncompress at the browser?

Image formats are already compressed. You would gain nothing by stitching and trying to further compress them.

You can just stick the images together and use background-position to display different parts of them: this is called ‘spriting’. But spriting's mostly useful for smaller images, to cut down the number of HTTP requests to the server and somewhat reduce latency; for larger images like manga pages the benefit is not so large, possibly outweighed by the need to fetch one giant image all at once even if the user is only going to read the first few pages.

ALTERNATIVE: I was also thinking of saving then as strings and then decode

What would that achieve? Transferring as string would, in most cases, be considerably slower than raw binary. Then to get them from JavaScript strings into images you'd have to use data: URLs, which don't work in IE6-IE7, and are limited to how much data you can put in them. Again, this is meant primarily for small images.

I think all you really want is a bog-standard image preloader.

Solution 3

You could preload the images in javascript using:

var x = new Image();
x.src = "someurl";

This would work like the one you described as "saving the image in strings".

Solution 4

Spriting

Just have a look how facebook does it: http://b.static.ak.fbcdn.net/rsrc.php/z3JQK/hash/11cngjg0.png

One image that loads FASTER than series of small images. To display the icon you simply create a div with fixed dimensions, and move the background inside it. Your div works as a viewport for the big image. You use background-position to move to appropriate part of the image. Everything else is hidden.

Different domains

Something you probably didn't know - Internet Explorer has a limit of connections per server. You can read about it here: http://support.microsoft.com/?scid=kb;en-us;183110&x=17&y=11 (here are exact numbers).

What it means - if user is using IE7, he will be able to load ONLY 4 (or 2) files at the same time from your server regardless his internet connection speed.

To speed things up, you could create few subdomains: server1.mydomain.com, server2.mydomain.com, server3.mydomain.com etc - and then user can download many files a lot quicker, because you use different hosts to serve different files.

Solution 5

Image preloaders have been around for ages. You really do not need to load them all at once, you can do it on demand [when the person loads the next page, you can fetch the image after it]

Share:
19,721
Fabiano Soriani
Author by

Fabiano Soriani

Software engineer, Computer Science Bachelor Works more with: Ethereum, Node.js, SQL, noSQL, Angular.js, React @Axiom Zen @CryptoKitties ____━━____┗┓|::::::^━━━━━━^ ____━━____━┗|:::::|。◕‿‿ ◕。| ____━━____━━╰O-O----O-O

Updated on June 21, 2022

Comments

  • Fabiano Soriani
    Fabiano Soriani almost 2 years

    UPDATE: This question is outdated, please disregard

    So.. my idea is to load a full manga/comics at once, with a progress bar included, and make sort of a stream, like:

    • My page loads the basic (HTML+CSS+JS) (of course)
    • As done, I start loading the imgs(the URLs are stored on JS var) from my server, one a time (or some faster way) so I can make a sort of progress bar.
    • ALTERNATIVE: Is there a way to load a compresses file with all imgs and uncompress at the browser?
    • ALTERNATIVE: I was also thinking of saving then as strings and then decode, they are mostly .jpg
    • The images don't have to show right away, i just need the callback when they are done.

    XTML and HTML5 is acceptable

    What is the fastest way to load a series of images for my website?

    EDIT Since @Oded comment.. the question is truly what is the best tech for loading images and the user don't have to wait everytime is turns the 'page'. Targeting a more similar experience like when you read comics in real life.

    EDIT2 As some people helped me realize, I'm looking for a pre-loader on steroids

    EDIT3 No css techs will do

  • Fabiano Soriani
    Fabiano Soriani about 14 years
    Yep agreed, for small images it is a good tech but not the case, since would overload a lot for no benefit, or even a browser crash since the img would be gigantic
  • Fabiano Soriani
    Fabiano Soriani about 14 years
    I was looking for a preloader on steroids, maybe a SWF? My only concern is that i don't make use of a CDN, but I also don't wanna fall in the image load limit cap.
  • Paolo
    Paolo about 14 years
    This is what I'd go for with a slight twist - load pages 1 and 2 to start with when the person turns to page 2, start loading page 3 in the background. That way you don't have to load the entire comic up front but each page turn will be quick as the next page is already loaded by the time the reader reaches the end of the current page.
  • PP.
    PP. about 14 years
    The example here supposes that 3 pipelined images will be faster than 3 sequentially loaded images. That is true. But to suppose that 3 pipelined images will be faster than 1 image can be, at best, an assumption. In fact multiple 3-way handshakes over a long distance (high latency) link might cause the pipelined approach to take longer.
  • Fabiano Soriani
    Fabiano Soriani about 14 years
    Sprinting: CSS won't do for this. Yes i am aware of that IE cap, my ALTERNATIVE about strings and compactness had that in mind..
  • Fabiano Soriani
    Fabiano Soriani about 14 years
    The tech is the basic i had in mind, alternative to the js Image object. It's likely 2MB (B&W image), and no, I'm trying to find a improved wheel! People likely don't just quit in the middle of a comic.. And if they do, it just won't load it all.
  • user229044
    user229044 about 14 years
    I'm saying that your way will add complexity to no gain. You cannot use Javascript to make already compressed data travel down the pipes any faster.
  • Fabiano Soriani
    Fabiano Soriani about 14 years
    I want to add complexity, yes, but to add gain, otherwise is pointless. See @Chris Dennett answer, that is the kind of stuff I look for, now I need to make it feasible
  • user229044
    user229044 about 14 years
    Correct me if I'm wrong, but splitting a single image for pipelining would only add benefit if he were downloading a single large image? As is, he's already transferring several files so pipelining will occur regardless.
  • Chris Dennett
    Chris Dennett about 14 years
    I think he found a solution via raw throughput, getting the manga images themselves to the user faster :) The problem with this approach however is that if the users want to save the files, they'll be in multiple bits and a pain to download. Anyway, what I'd suggest is combining this with Javascript (see elouai.com/javascript-preload-images.php) and load in the images for next / forward pages, getting the composite image URLs through your server-side comic script while generating the HTML.
  • duhaime
    duhaime about 4 years
    (Preload here just means requesting from the browser before displaying)