Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Created November 16, 2016 21:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyletaylored/9259df32a90afc3bc41286af8a49af52 to your computer and use it in GitHub Desktop.
Save kyletaylored/9259df32a90afc3bc41286af8a49af52 to your computer and use it in GitHub Desktop.
Jekyll: HD images with Retina.js
{% comment %}
We're splitting off the file extension in order to add "@2x"
if the image is retina ready, else use a regular image with "data-no-retina".
Image extension is assumed 3 characters. PNG, JPG, GIF (no JPEG).
{% endcomment %}
{% capture img_length %}{{ include.src | size | minus:4 }}{% endcapture%}
{% assign img_ext = include.src | slice: -4,4 %}
{% assign img_name = include.src | slice: 0,img_length %}
{% capture img_url %}{{ img_name }}{{ img_ext }}{% endcapture %}
{% capture img_url_hd %}{{ img_name }}@2x{{ img_ext }}{% endcapture %}
{% if include.class %}
{% capture img_class %}{{ include.class }}{% endcapture %}
{% else %}
{% capture img_class %} {% endcapture %}
{% endif %}
{% if include.retina == 1 %}
<img src="{{ img_url | prepend: site.baseurl }}" at2x="{{ img_url_hd | prepend: site.baseurl }}" alt="{{ include.alt }}" class="{{ img_class }}">
{% else %}
<img src="{{ img_url | prepend: site.baseurl }}" alt="{{ include.alt }}" class="{{ img_class }}" data-no-retina>
{% endif %}
{% include image.html src="/path/to/image.jpg" alt="ALT" class="CLASS NAME" retina=1 %}
@kyletaylored
Copy link
Author

This example lets you include retina ready images using Retina.js using a simple include statement and passing in some variables, one being if there's a retina version of that image or not.

@fatihturan
Copy link

Thank you for sharing this! 👍

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