Last active
December 14, 2015 16:49
-
-
Save uniquelau/5117682 to your computer and use it in GitHub Desktop.
1. Detect devicePixelRatio
2. Set Cookie
3. Render correct image for dPR - adjust jpeg quality based on dPR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Fetch devicePixelRatio --> | |
<xsl:variable name="devicePixelRatio" select="normalize-space(umb:RequestCookies('dPR'))" /> | |
<!-- Template for a Media Image --> | |
<xsl:template match="Image" name="GenericImage"> | |
<xsl:param name="class"/> | |
<xsl:param name="crop"/> | |
<xsl:param name="id"/> | |
<xsl:param name="size"/> | |
<xsl:param name="retinafy"/> | |
<img src="{umbracoFile}" width="{umbracoWidth}" height="{umbracoHeight}" alt="{@nodeName}"> | |
<!-- In retina mode use half dimensions --> | |
<!-- REMOVED --> | |
<!-- Let's adjust our dimensions based on FACT ;) --> | |
<xsl:if test="$devicePixelRatio > 1"> | |
<xsl:attribute name="src"> | |
<xsl:value-of select="umbracoFile"/> | |
<xsl:text>?mode=crop-up&width=</xsl:text> | |
<xsl:value-of select="floor(substring-before($size, 'x') * $devicePixelRatio)"/> | |
<xsl:text>&height=</xsl:text> | |
<xsl:value-of select="floor(substring-after($size, 'x') * $devicePixelRatio)"/> | |
<xsl:text>&quality=</xsl:text> | |
<!-- When dPR set Quality to 70, reduce quality as dPR increases --> | |
<xsl:value-of select="70 - (20 * (get:log($devicePixelRatio) div get:log(2)))"/> | |
</xsl:attribute> | |
</xsl:if> | |
<!-- $size can override original + cropped sizes --> | |
<xsl:if test="$size"> | |
<xsl:attribute name="width"> | |
<xsl:value-of select="substring-before($size, 'x')"/> | |
</xsl:attribute> | |
<xsl:attribute name="height"> | |
<xsl:value-of select="substring-after($size, 'x')"/> | |
</xsl:attribute> | |
</xsl:if> | |
<xsl:if test="$class"> | |
<xsl:attribute name="class"> | |
<xsl:value-of select="$class"/> | |
</xsl:attribute> | |
</xsl:if> | |
<xsl:if test="$id"> | |
<xsl:attribute name="id"> | |
<xsl:value-of select="$id"/> | |
</xsl:attribute> | |
</xsl:if> | |
</img> | |
</xsl:template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// drop session cookie containing dPR. | |
document.cookie='dPR=' + window.devicePixelRatio + ';path=/' | |
// ORGINAL VERSION BELOW ;) | |
// var pixelRatio = window.devicePixelRatio; | |
// var retina = pixelRatio > 1; | |
// if (retina) { | |
// the user has a retina display | |
// document.cookie='dPR=' + '1.5' + ';path=/' | |
// } | |
// else { | |
// the user has a non-retina display | |
// } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>devicePixelRatio Tests</h1> | |
<h2>dPR = 1</h2> | |
<img width="1280" height="792" src="/media/5507/studio2_shot1.jpg"> | |
<h2>dPR = 1.5</h2> | |
<img width="1280" height="792" src="/media/5507/studio2_shot1.jpg?mode=crop-up&width=1920&height=1188&quality=58.30074998557688"> | |
<h2>dPR = 2</h2> | |
<img width="1280" height="792" src="/media/5507/studio2_shot1.jpg?mode=crop-up&width=2560&height=1584&quality=50"> | |
<h2>dPR = 4</h2> | |
<img width="1280" height="792" src="/media/5507/studio2_shot1.jpg?mode=crop-up&width=5120&height=3168&quality=30"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment