Skip to content

Instantly share code, notes, and snippets.

@nhoizey
Last active July 4, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhoizey/224a1c9dfb396a4c7b41ea114f175712 to your computer and use it in GitHub Desktop.
Save nhoizey/224a1c9dfb396a4c7b41ea114f175712 to your computer and use it in GitHub Desktop.
Extracts the url of the largest image from a `srcset-w`
url = srcset.scan(/([^, ][^ ]+)\s+([0-9]+)w/).map{ |url, size| { 'url' => url.strip, 'size' => size.to_i } }.reduce({ 'url' => '', 'size' => 0 }){ |current, new| current = new if new['size'] > current['size'] }['url']

Source:


https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_360/https://nicolas-hoizey.com/2003/10/gasteroprod_v1.png 360w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_670/https://nicolas-hoizey.com/2003/10/gasteroprod_v1.png 670w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_980/https://nicolas-hoizey.com/2003/10/gasteroprod_v1.png 980w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_1024/https://nicolas-hoizey.com/2003/10/gasteroprod_v1.png 1024w

Expected result: https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_1024/https://nicolas-hoizey.com/2003/10/gasteroprod_v1.png


Source:

https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_360/https://nicolas-hoizey.com/2002/10/doc-136.png 360w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_1024/https://nicolas-hoizey.com/2002/10/doc-136.png 1024w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_670/https://nicolas-hoizey.com/2002/10/doc-136.png 670w,
https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_980/https://nicolas-hoizey.com/2002/10/doc-136.png 980w

Expected result: https://res.cloudinary.com/nho/image/fetch/c_limit,f_auto,q_auto,w_1024/https://nicolas-hoizey.com/2002/10/doc-136.png


Source:

https://example.com/some-small-image.jpg 320w, https://example.com/some-larger-image.jpg 640w, https://example.com/some-huge-image.jpg 2000w, https://example.com/some-large-image.jpg 980w

Expected result: https://example.com/some-huge-image.jpg

@nhoizey
Copy link
Author

nhoizey commented Jul 4, 2018

Wow, c'est plus simple, plus clean, et super bien expliqué au point que j'ai tout compris… o_O

MERCI !!!!

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