Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Website Optimization for Retina Displays How-to

You're reading from   Instant Website Optimization for Retina Displays How-to Learning simple techniques which will make your website look stunning on high-definition Retina Displays

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849695121
Length 56 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Kyle Larson Kyle Larson
Author Profile Icon Kyle Larson
Kyle Larson
Arrow right icon
View More author details
Toc

Server-side Retina solutions (Become an expert)


Instead of using JavaScript to display Retina images, you could implement a server-side Retina image solution. These solutions use JavaScript to determine if the user is on a Retina device and then use PHP to send a high-definition image instead of a normal one. The benefit of using one of these solutions is that the server automatically sends the correct version once the cookie is set, instead of needing to request it from the browser.

There are several open source solutions that have already been written to accomplish this task. In this recipe we will cover one such solution to see how it is implemented.

Getting ready

To get started, download Jeremy Worboys' Retina Images project at http://retina-images.complexcompulsions.com/. For this project to work you'll need a server that is running PHP. If you're using another language you may be able to find a similar script or use Jeremy's code as a reference to create your own solution.

We'll also need an image to test. You can use the myImage.jpg and myImage@2x.jpg images we used in earlier recipes. If you don't have these images use a large photo at 1400 x 800 pixels for myImage@2x.jpg and resize to 50 percent for myImage.jpg. Save them both inside the images folder within the /retina/ folder.

How to do it...

  1. Extract the files from the Retina Images project and add retinaimages.php and the .htaccess file to the root directory of your web server. If you already have an existing .htaccess file you'll want to add the new code to it rather than replacing it.

  2. Create a new HTML document called serverSide.html inside the /retina/ folder. Inside the <head> tag of our HTML structure we'll add some JavaScript to set a cookie.

    <head>
      <script>
    (function(w){
      var dpr=((w.devicePixelRatio===undefined)?1:w.devicePixelRatio);
      if(!!w.navigator.standalone){
        var r=new XMLHttpRequest();
        r.open('GET','/retinaimages.php?devicePixelRatio=' + dpr,false);
        r.send()
      } else {
        document.cookie='devicePixelRatio='+dpr+';
        path=/'
      }
    })(window)
      </script>
    </head>
  3. Then inside the <body> tag of our HTML we'll add some CSS in case the user has JavaScript disabled.

    <body>
      <noscript>
      <style id="devicePixelRatio" media="only screen and (-moz-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)">
      #devicePixelRatio {
        background-image:url("/retinaimages.php?devicePixelRatio=2")
      }
      </style>
    </noscript>
  4. Then we'll add an image to test the code with.

      <img src="images/myImage.jpg" width="700" height="400" />
    </body>
  5. Now you can open this page from your web server on your Retina device to test it out.

How it works...

Jeremy's code works by detecting the devicePixelRatio in JavaScript to determine if the user has a Retina Display. Then it sets a cookie with this data so that the PHP code can reference it. If the user has JavaScript disabled, the CSS code within the <noscript> tag creates a fallback reference. Make sure this code is above the stylesheets in your page so that any images that they contain are processed correctly after the cookie has been set.

We made sure to set the height and width attribute of the non-Retina image in our HTML <img> tag so that it is sized correctly when our high-definition image is served. You'll need to ensure this is set for all images and that CSS images have a background-size set. Any image you want served in high-definition will need to have the same filename as the original image with the addition of @2x as we've done here. They will also need to be in the same folder so the script can find them.

When the image is requested from the server the .htaccess file we added (or modified) will use the retinaimages.php file instead of serving it directly. This PHP code checks if the cookie for a Retina device is set and that there is an @2x version of the image. If these are both true, the high-definition image will be served. If it is not true, then the regular version is served.

lock icon The rest of the chapter is locked
arrow left Previous Section
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image