How it works
Adaptive Images does a number of things depending on the scenario the script has to handle but here's a basic overview of what happens when you load a page (assuming Apache, though nginx isn't much different):
- The HTML starts to load in the browser and a snippet of JS in the
<head>
writes a session cookie, storing the visitor's screen size in pixels. - The browser then encounters an
<img>
tag and sends a request to the server for that image. It also sends the cookie, because that’s how browsers work. - Apache receives the request for the image and immediately has a look in the website's
.htaccess
file, to see if there are any special instructions for serving files. - There are! The
.htaccess
says "Dear server, any request you get for a JPG, GIF, or PNG file please send to theadaptive-images.php
file instead."
The PHP file then does some intelligent thinking which can cover a number of scenario's but I'll illustrate one path that can happen:
- The PHP file looks for a cookie and finds that the user has a maximum screen size of 480px.
- It compares the cookie value with all
$resolution
sizes that were configured, and decides which matches best. In this case, an image maxing out at 480px wide. - It then has a look inside the
/ai-cache/480/
folder to see if a rescaled image already exists. - We'll pretend it doesn’t - the PHP then goes to the actual requested URI to find the original file.
- It checks the image width. If that's smaller than the user's screen width it sends the image.
- If it's larger, the PHP creates a down-scaled copy and saves that into the
/ai-cache/480/
folder ready for the next time it's needed, and sends it to the user.
It also does a few other things when needs arise, for example:
- Detects Retina displays. You can choose to serve high DPI images to those displays if you want, by using an alternate line of JavaScript.
- It sends images with a cache header that tells proxies not to cache the image whilst telling browsers they should. This avoids problems with proxy servers and network caching systems grabbing the wrong image and storing it.
- It handles cases where there isn't a cookie set; mobile devices will be supplied the smallest image, and desktop devices will get the largest.
- It compares time-stamps between the source image and the generated cache image - to ensure that if the source image gets updated, the old cached file won’t be sent.
Common Questions
There are a couple of very common things people ask about, so to be perfectly clear:
- Adaptive Images does NOT work on your browser window size, resizing your browser will have no effect on the size of images that are downloaded. It works on the screen size. The reason for this is to avoid poor caching behaviour; if you're on a large screen with a small window and AI were to deliver the small image, that image would be cached by your browser. As soon as you maximised the window the image would obviously be too small, and the browser doesn't know it needs to re-request the image.
- To support Retina displays all you need to do is use the alternate JS. Nothing more.
- AI works perfectly fine with WordPress; but you'll have to configure your
.htaccess
file yourself, and that means you need to know a little about how WordPress works. Try following this guide to getting Adaptive Images working with WordPress
Installation
For Apache:
One important thing: do not over-write any existing .htaccess
file. If you have one already, back it up. Feeling up to it? Excellent:
- Download the latest version of Adaptive Images either from the website or from the GitHub repository.
- Upload the included
.htaccess
andadaptive-images.php
files to the server document-root. - Add this line of JavaScript as high in the
<head>
of your site as possible, before any other JS:<script>document.cookie='resolution='+Math.max(screen.width,screen.height)+'; path=/';</script>
- Configure the
$resolutions
variable at the top of theadaptive-images.php
file to match the breakpoints you are using for your designs.
If you already have an .htaccess file, open it in a text editor and add the code from the supplied file into your existing one.
For nginx:
nginx does not use .htaccess
files, so do as above but replace Step 2 with the following:
In your virtual-host config file, inside the server block, ensure you've got the following:
location assets {
}
location ai-cache {
}
location / {
rewrite \.(?:jpe?g|gif|png)$ /adaptive-images.php;
}
You'll need to ensure you've got PHP running with nginx too, but that's outside of the scope of this site. Embrace your GoogleFu.