khalyomede / gulp-sharp-responsive

A gulp plugin to generate responsives images.
https://npmjs.com/package/gulp-sharp-responsive
17 stars 6 forks source link
avif gulp gulpplugin image-processing responsive sharp webp

gulp-sharp-responsive

A gulp plugin to generate responsives images.

Build Status npm NPM

Summary

About

I make web apps and I often need to generate images of multi formats and size from a single image. For example, an image "lion.jpeg", that is declined like this:

Sharp can do this, and since I use Gulp for my everyday tasks, I created a plugin to automatize this task.

Features

Installation

In your terminal:

npm install --save-dev gulp-sharp-responsive

With Yarn:

yarn add --dev gulp-sharp-responsive

Examples

Sidenote: all the following example uses the TS version of gulpfile. This is why you will see ES6 syntaxes like "import ...".

If you are using the "classic" syntax (require), just convert the ES6 to CommonJS like following:

// this
import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

// becomes this
const { src, dest } = require("gulp");
const sharpResponsive = require("gulp-sharp-responsive");

Note that if you are using typescript, don't forget to add the "esModuleInterop" option to true in you tsconfig.json in order for the ES6 syntax mentioned above to work.

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

1. Generate image of different sizes

In this example, we will generate a small and large image size from an image.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    formats: [
      { width: 640, rename: { suffix: "-sm" } },
      { width: 1024, rename: { suffix: "-lg" } },
    ]
  }))
  .pipe(dest("dist/img"));

2. Generate image of different formats

In this example, we will generate modern image format like webp and avif from an image.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    formats: [
      { width: 640, format: "webp" },
      { width: 640, format: "avif" },
    ]
  }))
  .pipe(dest("dist/img"));

3. Include the original file in the output images

In this example, we will tell the plugin to keep the original file to be outputed in the dist folder.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    includeOriginalFile: true,
  }))
  .pipe(dest("dist/img"));

4. Pass format specific options

In this example, we will use JPEG options to customize how we want our image to be generated.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    formats: [
      { width: 640, jpegOptions: { quality: 60, progressive: true } }
    ],
  }))
  .pipe(dest("dist/img"));

You can pass options for various formats. Here is all supported options and their documentation:

5. Pass sharp specific options

In this example, we will pass Sharp options to customize its behavior.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    formats: [
      { width: 640, sharp: { failOnError: false, density: 340 } }
    ],
  }))
  .pipe(dest("dist/img"));

Find all the available options in the Sharp constructor documentation.

6. Use a callback to compute the width

In this example, we will use the file metadata to compute the width dynamically.

import { src, dest } from "gulp";
import sharpResponsive from "gulp-sharp-responsive";

const img = () => src("src/img/**/*.{jpg,png}")
  .pipe(sharpResponsive({
    formats: [
      { width: (metadata) => metadata.width * 0.5 } // divides the original image width by 2
    ]
  }))
  .pipe(dest("dist/img"));

Options

formats

A list of transformations to operate on the file.

format: [
  {
    width: number | ((metadata IFileMetadata) => number),
    format?: "jpeg" | "png" | "webp" | "gif" | "tiff" | "avif" | "heif",
    rename?: {
      dirname?: string,
      prefix?: string,
      basename?: string,
      suffix?: string,
      extname?: string,
    },
    sharp?: {
      // ...
    },
    jpegOptions?: {
      // ...
    },
    pngOptions?: {
      // ...
    },
    webpOptions?: {
      // ...
    },
    gifOptions?: {
      // ...
    },
    tiffOptions?: {
      // ...
    },
    avifOptions?: {
      // ...
    },
    heifOptions?: {
      // ...
    },
  },
];

includeOriginalFile

Wether to include the original transformed file in the output or not. Default to false (not included).

includeOriginalFile?: boolean,

IFileMetadata

interface IFileMetadata {
  width: number;
  height: number;
}

Test

npm run test

Publish

First make sure the version in package.json is the version to publish.

npm login
npm publish