GithubHelp home page GithubHelp logo

eleventy-respimg's Introduction

{% respimg %}

What does it do?

It turns config like this:

eleventyConfig.cloudinaryCloudName = 'your-cloud-name-here';
eleventyConfig.srcsetWidths = [ 320, 640, 960, 1280, 1600, 1920, 2240, 2560 ];
eleventyConfig.fallbackWidth = 640;

and shortcodes like this:

{% respimg
	"https://cool.img/here.jpg",
	"A cool image",
	"(min-width: 48em) 48em, 100vw"
%}

into responsive <img> tags, like this:

<img
	srcset="
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_320/https://cool.img/here.jpg 320w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_640/https://cool.img/here.jpg 640w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_960/https://cool.img/here.jpg 960w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_1280/https://cool.img/here.jpg 1280w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_1600/https://cool.img/here.jpg 1600w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_1920/https://cool.img/here.jpg 1920w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_2240/https://cool.img/here.jpg 2240w,
		https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_2560/https://cool.img/here.jpg 2560w"
	sizes="(min-width: 48em) 48em, 100vw"
	src="https://res.cloudinary.com/your-cloud-name-here/image/fetch/q_auto,f_auto,w_640/https://cool.img/here.jpg"
	alt="A cool image"
/>

The resulting <img>s are “responsive” in the following ways:

Installation

The plugin is available on npm.

npm install eleventy-plugin-respimg

After npm installing, open up your Eleventy config file (probably .eleventy.js) and

  1. require it
  2. set the mandatory config parameters, and
  3. use addPlugin.
// ①
const pluginRespimg = require( "eleventy-plugin-respimg" );

module.exports = function( eleventyConfig ) {

	// ②
	eleventyConfig.cloudinaryCloudName = 'your-cloud-name-here';
	eleventyConfig.srcsetWidths = [ 320, 640, 960, 1280, 1600, 1920, 2240, 2560 ];
	eleventyConfig.fallbackWidth = 640;

	// ③
	eleventyConfig.addPlugin( pluginRespimg );
	
};

Also! Make sure that the domains where you’ll be hosting your originals are whitelisted in your Cloudinary settings, under “Security » Allowed fetch domains”. Alternatively, leave the field blank, and Cloudinary will happily fetch from any domain.

Example

Here’s a quick, actual example, that uses a couple of these in a couple of contexts.

TODO

  • limit srcset breakpoints to the intrinsic width of the original (don’t let Cloudinary upscale)
  • allow relative src paths
  • don’t fetch images that are already in your Cloudinary media library
  • smarter srcset breakpoints, using Cloudinary’s automatic responsive image breakpoint features
  • automatic sizes‽ (probably using Puppeteer and @ausi’s logic?)
  • what should it do with animated Gifs?

eleventy-respimg's People

Contributors

eeeps avatar paulrobertlloyd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

eleventy-respimg's Issues

Support variables inside `respimg` tag

Example:

{%- for banner in home_slides.slides -%}
	<a href="{{ banner.link }}" title="{{ banner.title }}">
		{% respimg "https://egv54645.netlify.app/{{ banner.image }}", "{{ banner.title }}", "1200px" %}
	</a>
{%- endfor -%}

error in DEBUG

I have setup this cool script, but when trying to implement

{% respimg
"https://res.cloudinary.com/xxxx/image/upload/v1524743816/FODLER/IMAGE_NAME.jpg",
"Lune Exacte - New Moon",
"(min-width: 48em) 48em, 100vw"
%}

in markdown blog file; I am getting

`File changed: site/blog/as_lune-exacte.md
Problem writing Eleventy templates: (more in DEBUG output)

Having trouble rendering njk (and markdown) template ./site/blog/as_lune-exacte.md (TemplateContentRenderError)
(unknown path) [Line 22, Column 4]
unknown block tag: respimg (Template render error):
Template render error: (unknown path) [Line 22, Column 4]
unknown block tag: respimg
at Object._prettifyError (/Users/gXXX/eleventy/skeleventyStarter/node_modules/nunjucks/src/lib.js:36:11)
at Template.render (/Users/XXX/eleventy/skeleventyStarter/node_modules/nunjucks/src/environment.js:542:21)
at /Users/XXX/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/Engines/Nunjucks.js:133:14
at new Promise ()
at /Users/gXXX/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/Engines/Nunjucks.js:132:14
at /Users/XXXe/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/Engines/Markdown.js:65:47
at Template.render (/Users/XXX/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/TemplateContent.js:167:28)
at processTicksAndRejections (internal/process/task_queues.js:89:5)
at async Template._getContent (/Users/XXX/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/Template.js:450:15)
at async Template.getTertiaryMapEntry (/Users/XXX/eleventy/skeleventyStarter/node_modules/@11ty/eleventy/src/Template.js:622:24)
Copied 2 items and Processed 0 files in 0.55 seconds`

What have I made wrong? Any advice?
I really would like to get this script up and running
Cheers Gerhard

Process for referencing local images before remote deployment?

Hey Eric!

I'm fairly new to Cloudinary and Eleventy, so I'm just trying to get my head around the best process for this stuff. If I'm developing my site locally, and I add a new image to my images folder, that obviously not yet anywhere publicly remote that Cloudinary can fetch or upload from, so until I deploy the site, those image references will be broken, right? Is there any way to get around that at this point?

I noticed you have "allow relative src paths" on the to-do list - will that allow something like referencing local images in a DEV env, and then using the cloudinary URLs when building for a remote/PROD env?

unknown block tag: respimg

@eeeps I found #6 and saw your comment for using #4's merged code, but I'm still getting the same error on my end.

TemplateWriterWriteError` was thrown
> (./_includes/layouts/home.njk) [Line 4, Column 6]
  unknown block tag: respimg

`Template render error` was thrown:
    Template render error: (./_includes/layouts/home.njk) [Line 4, Column 6]
      unknown block tag: respimg
        at Object._prettifyError (D:\repos\spectrumac-website\node_modules\nunjucks\src\lib.js:36:11)
        at Template.render (D:\repos\spectrumac-website\node_modules\nunjucks\src\environment.js:536:21)
        at D:\repos\spectrumac-website\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:236:14
        at new Promise (<anonymous>)
        at D:\repos\spectrumac-website\node_modules\@11ty\eleventy\src\Engines\Nunjucks.js:235:14
        at TemplateLayout.render (D:\repos\spectrumac-website\node_modules\@11ty\eleventy\src\TemplateLayout.js:152:31)
        at process._tickCallback (internal/process/next_tick.js:68:7)
Problem writing Eleventy templates: (more in DEBUG output)
> Having trouble writing template: _site/index.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.