项目作者: luwes

项目描述 :
Eleventy plugin wielding Sharp's image processing power to the fullest.
高级语言: JavaScript
项目地址: git://github.com/luwes/eleventy-plugin-sharp.git
创建时间: 2020-08-30T12:30:02Z
项目社区:https://github.com/luwes/eleventy-plugin-sharp

开源协议:

下载


eleventy-plugin-sharp

npm: npm i eleventy-plugin-sharp

Inspired by the Craft CMS image transform API.
This plugin gives you the full power of the awesome sharp library in your 11ty templates.

Usage

  1. // .eleventy.js
  2. const sharpPlugin = require('eleventy-plugin-sharp');
  3. module.exports = function (eleventyConfig) {
  4. eleventyConfig.addPlugin(sharpPlugin({
  5. urlPath: '/images',
  6. outputDir: 'public/images'
  7. }));
  8. };

Filters

Filters are used to build up the Sharp instance. Pretty much all the methods that the Sharp API provides can be called. output options, resizing, operations, colour, etc.

Shortcodes

In addition shortcodes are used to execute the async functions of Sharp, something filters don’t support.

  • getUrl(instanceOrFilepath) Saves the image to disk and gets the url.
  • getWidth(instanceOrFilepath) Gets the width of an image.
  • getHeight(instanceOrFilepath) Gets the height of an image.
  • getMetadata(instanceOrFilepath) Gets the metadata of an image.
  • getStats(instanceOrFilepath) Gets the stats of an image.

Responsive images using <picture>

The sharp filter is optional if the input file is followed by any Sharp transform.

  1. {% set image = "src/images/zen-pond.jpg" | sharp %}
  2. <picture>
  3. <source srcset="{% getUrl image | resize({ width: 1440, height: 460 }) | webp %} 1x, {% getUrl image | resize({ width: 2560, height: 800 }) | webp %} 2x" media="(min-width: 640px)">
  4. <source srcset="{% getUrl image | resize({ width: 640, height: 320 }) | webp %} 1x, {% getUrl image | resize({ width: 1280, height: 640 }) | webp %} 2x">
  5. <source srcset="{% getUrl image | resize({ width: 1440, height: 460 }) %} 1x, {% getUrl image | resize({ width: 2560, height: 800 }) %} 2x" media="(min-width: 640px)">
  6. <img class="articleHero-image" srcset="{% getUrl image | resize({ width: 640, height: 320 }) %} 1x, {% getUrl image | resize({ width: 1280, height: 640 }) %} 2x" alt="{{ image.title }}">
  7. </picture>

Get the dimensions of a saved image w/ custom output filepath

  1. {% set bannerImage = "src/images/zen-pond.jpg" | resize(1440, 460) | toFile("public/images/custom-name.webp") %}
  2. {% getUrl bannerImage %}
  3. {% getWidth bannerImage.fileOut %}
  4. {% getHeight bannerImage.fileOut %}