Skip to content

Instantly share code, notes, and snippets.

@jasoncartwright
Created September 3, 2012 09:22
Show Gist options
  • Save jasoncartwright/3608074 to your computer and use it in GitHub Desktop.
Save jasoncartwright/3608074 to your computer and use it in GitHub Desktop.
Retina image swap-out for Google-hosted images
if (window.devicePixelRatio > 1) {
img_re = new RegExp(/=(s|w)(\d*)(\-c)?$/)
$("img").each(function(id, el){
img_src = $(el).attr("src")
if (img_re.test(img_src)) {
img_size = parseInt(img_src.match(img_re)[2])
dimension = img_src.match(img_re)[1]
new_img_size = img_size*2
new_src = img_src.replace(img_re,"=" + dimension + new_img_size)
$(el).attr("src",new_src)
}
})
}
@jasoncartwright
Copy link
Author

Google App Engine uses get_serving_url [1] to produce an image URL [2] that can be resized by changing the last digits. This javascript detects is its running on a device capable of displaying 'retina' images and swaps out the img src to one double the size. You can see this running on the Potato website [3].

[1] https://developers.google.com/appengine/docs/python/images/functions
[2] E.g. http://lh5.ggpht.com/IFPH6YSiPKtCp1HZtIFr3R21i_Cgjmqfq-9sBsY03QvQZEcnvu-xYPvi8rINATms6Rdlrt3Jjn1az4R8EkbsdQ=s220
[3] http://p.ota.to/work/google-art-project/

@jasoncartwright
Copy link
Author

jasoncartwright commented Nov 26, 2014

Worth noting that we don't use this technique anymore - it's all picture element and http://scottjehl.github.io/picturefill/ now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment