Upgrade to Pro — share decks privately, control downloads, hide ads and more …

StandardsNext: Adaptive Images

StandardsNext: Adaptive Images

How do we handle in Responsive Designs?

Avatar for mattwilcox

mattwilcox

March 05, 2012
Tweet

Other Decks in Technology

Transcript

  1. Adaptive Images HOW DO WE HANDLE <IMG> IN RESPONSIVE DESIGNS?

    Matt Wilcox | @mattwilcox Monday, 5 March 12
  2. A site that changes its design and layout when viewed

    at different widths? WHAT DO WE MEAN BY RESPONSIVE DESIGN? This is only the visible result of responsive design. Monday, 5 March 12
  3. WHAT DO WE MEAN BY RESPONSIVE DESIGN? It’s not just

    a change in layout. It’s not just a use of @media queries. It is an extension of the Progressive Enhancement mindset. It’s part of a process of responsible design. Monday, 5 March 12
  4. WHAT DO WE MEAN BY RESPONSIVE DESIGN? Responsive design encompasses

    any method that allows us, for any given user, to... Present information as clearly as possible. Be as efficient as possible. Which leads us to... Monday, 5 March 12
  5. WHAT DO WE MEAN BY MOBILE FIRST? This is misnamed,

    what we really mean is CONSIDER THE SIMPLEST EXPERIENCE FIRST Often mobile, but not always. Doing this gives the most efficient end results for any given user. Monday, 5 March 12
  6. WHAT DO WE MEAN BY MOBILE FIRST? Design benefits include:

    The ability to target experience for given devices, instead of designing a global compromise. A focus on core design elements: heirarchy, flow, typography. Monday, 5 March 12
  7. WHAT DO WE MEAN BY MOBILE FIRST? Build benefits include:

    Maximum efficiency, because... The less capable the device, the less it’s asked to do. The less capable the device, the less the server and network has to do. Monday, 5 March 12
  8. WHAT DO WE MEAN BY MOBILE FIRST? Plus, it’s just

    a really elegant solution. Monday, 5 March 12
  9. RESPONSIVE CSS? The Fluid Image technique: img { max-width: 100%;

    } Media Queries: Adapt styles based on the browser/device width. Which allows us to do conditional asset loading. Monday, 5 March 12
  10. RESPONSIVE CSS? But we have problems: We adapt to screen

    size because that’s all we know about... ...and screen size is a poor proxy for: bandwidth, processor speed, usage allowance, latency, etc. Monday, 5 March 12
  11. RESPONSIVE HTML? No ability to adapt to the environment. Which

    isn’t normally a problem - semantics shouldn’t change based on the device or browser width. But it is a problem for our goal of clarity for all users: 1) Big devices need big <img>’s 2) Small devices would rather have small ones. 3) Different devices sometimes need different ones. Monday, 5 March 12
  12. SOLUTION REQUIREMENTS Respond to the needs of the user by...

    Presenting information as clearly as possible. Being as efficient as possible (for that user). Monday, 5 March 12
  13. SOLUTION REQUIREMENTS WHAT MUST THE IMPLEMENTATION DO? Be reliable. Be

    selective. Don’t break the web. Be simple to use. Be future friendly. Monday, 5 March 12
  14. Solutions? THIS ISN’T EASY. There are no solutions that fulfil

    all of those requirements. Despite over a year of community effort. Monday, 5 March 12
  15. WEB PAGE BEHAVIOUR HTML is loaded first, and sequentially. Resources

    are requested as they are encountered. Scripts are executed immediately. Cookies are sent with all HTTP requests. Monday, 5 March 12
  16. TACKLING THE PROBLEM: HTML lowsrc: No longer valid HTML. Intended

    to mimic progressive loading (it loaded one low res source whilst waiting for a higher resolution one to replace it). Monday, 5 March 12
  17. TACKLING THE PROBLEM: Use JavaScript to re-write src: Initial value

    is always requested anyway... Doubles HTTP request. You download more data than if you did nothing. Monday, 5 March 12
  18. TACKLING THE PROBLEM: So use a 1px GIF in src

    instead? This ‘minimises’ loading redundant images. But now all your images rely on JavaScript. Monday, 5 March 12
  19. TACKLING THE PROBLEM: Use JavaScript to inject a base tag:

    Has race conditions. Webkit have effectively killed the tag. Monday, 5 March 12
  20. TACKLING THE PROBLEM: Use <noscript> and JavaScript: <noscript> for mobile

    image, JS to swap it out for higher resolution: Doesn’t work in IE. Monday, 5 March 12
  21. TACKLING THE PROBLEM: Workaround the workaround: <script>document.write(‘<‘ + ‘!--’)</script> <noscript>

    <img src=”mobile.jpg” /> </noscript --> Are you serious? Monday, 5 March 12
  22. TACKLING THE PROBLEM: Conclusion: There doesn’t seem to be a

    viable client-side solution. Monday, 5 March 12
  23. INVOLVING THE SERVER: A plan: Have some JavaScript to swap

    out src for -data-highsrc when appropriate: <img src=”mobile.jpg” -data-highsrc=”desktop.jpg” /> Have the JS also set a cookie, storing the browser’s width. If the server finds a cookie, and it indicates a desktop, it sends a 1px image instead of mobile.jpg This was used in the Boston Globe redesign. Monday, 5 March 12
  24. INVOLVING THE SERVER: Flaws: Double HTTP requests for all images.

    Only allows big|small scales. Doesn’t deal with device orientation changes. Breaks badly now browsers pre-fetch images: Sometimes the cookie isn’t set fast enough resulting in desktop devices getting mobile images. Monday, 5 March 12
  25. INVOLVING THE SERVER: Other problems: Cookies aren’t nice when serving

    static files. How are those images getting on the server? How is that HTML being generated? Monday, 5 March 12
  26. GOALS: Be easy to set up and use. As low

    maintenance as possible. Work with existing content; no mark-up edits, no custom CMS, no manual image curation. Allow multiple versions. Work with design breakpoints not devices. Be easily replaced when superior solutions arrive. Monday, 5 March 12
  27. What is Adaptive Images? One .htaccess file. One PHP file.

    One line of JavaScript, in the <head>. Monday, 5 March 12
  28. How it works Assumption: The <img> in the HTML is

    already the highest resolution. JS detects the largest screen dimension, stores it in a Cookie. .htaccess file points certain requests to adaptive-images.php (default: all JPG, GIF, PNG images that are not inside /assets/) PHP file does some smart processing, including... Monday, 5 March 12
  29. How it works ... Reading the Cookie and fitting the

    result into breakpoints that match the CSS breakpoints. Checks it’s own cache directory to see if a version of the requested file exists at that breakpoint size. If it does, compares the modified dates on that and the original to ensure the cache version is not stale. If it doesn’t exist cached; Creates rescaled image only if the source image is larger than the requested breakpoint size. Caches it for future use, and serves it. Monday, 5 March 12
  30. Customisation Set the breakpoints to match your CSS. Specify where

    you want the AI cache folder. Set the quality of generated JPGs, like in Photoshop. Set how long browsers should cache the image for. Subtly sharpen generated images. Alternate JavaScript to detect high DPI devices. Monday, 5 March 12
  31. Limitations This is a PHP solution. Ports exist in ColdFusion,

    .Net, WordPress, and Drupal. Project is Open Source and Creative Commons. Code is heavily commented. CDN’s and Proxies don’t know which asset to store. FAIL: Cache-Control: Vary. FAIL: HTTP redirects. SOLUTION: Images sent Cache-Control: Private This ensures CDNs and Proxies can’t break your site. But it does mean you can’t take advantage of CDNs & Proxies. Monday, 5 March 12
  32. Limitations Performance? Images shouldn’t be served with cookies. PHP is

    instantiated for every request. BUT: The file-size’s can be 80% smaller! Which saves hugely on transfer speed, and means the client device performs better. JavaScript and Cookie reliant? No cookie, for whatever reason? Then Adaptive Images sniffs the UA string: If “mobile” is found, sends lowest breakpoint image, otherwise sends the highest breakpoint image. Monday, 5 March 12
  33. Limitations Uses device size, not browser size? Because of browser

    caching. Not a big issue most of the time because most designs have a max-width, and AI uses that at most. Monday, 5 March 12
  34. Assessing Adaptive Images Is it easy to set up and

    use? Yes. Is it low maintenance? Yes. Does it allows numerous image sizes? Yes. Does it work with the design, not the device? Yes. Can it be applied to existing content? Yes. Is it robust and reliable? Mostly, with decent fallbacks. Can it be easily replaced? Yes, it’s future friendly. Monday, 5 March 12
  35. What does it not achieve? It only resizes assets, so

    it can’t improve semantic clarity. It doesn’t play nice with parts of the web ecosystem (CDNs, Proxies). Monday, 5 March 12
  36. The Future It would be nice to have servers aware

    of client capabilities, to adapt accordingly without relying on Cookies. We need better sensors - screen width is not good enough. HTML needs native methods to address this problem without involving servers. Monday, 5 March 12
  37. Responsive Images Community Group <picture alt="A photo of Joe Bloggs">

    <!-- smallest first - no @media qualifier --> <source src="small.jpg"> <!-- medium - viewport widths 400px wide and up --> <source src="medium.jpg" media="(min-width: 400px)"> <!-- large - viewport widths 800px wide and up --> <source src="large.jpg" media="(min-width: 800px)"> <!-- extra large - viewport widths 1000px wide and up --> <source src="extra-large.jpg" media="(min-width: 1000px)"> <!-- massive - viewport widths 1300px wide and up --> <source src="massive.jpg" media="(min-width: 1200px)"> <!-- Fallback. --> <img src="fallback.jpg" alt="A photo of Joe Bloggs"> </picture> Monday, 5 March 12
  38. Benefits of <picture> It’s based on existing tags; <video> This

    makes it familiar syntactically to existing specifications. It allows a pure HTML way to adapt images for clarity and for efficiency. It has a built-in fall-back so old browsers will still show images. Monday, 5 March 12
  39. Problems with <picture> Complicated. How is this code getting generated?

    By hand? CMS? Massive repetition. This mark-up would have to be repeated for every image on your site. It ties design breakpoints into the HTML. Which is terrible, what happens when you re-design? You have to change all of your mark-up site wide. GENERAL PROBLEMS WE’RE FACING: We’re repeating the same @media tests for the same page in three technologies, independently. Which is inefficient, hard to maintain, and future unfriendly. Monday, 5 March 12
  40. Further Reading Adaptive Images: http://adaptive-images.com Matt Wilcox: http://mattwilcox.net Something, anything,

    with pictures: http://www.flickr.com/photos/mattwilcox/ (See, I do know what they are!) Damn, my head’s mashed, give me something different! http://www.youtube.com/watch?v=iG9CE55wbtY Monday, 5 March 12