npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-sharp-responsive

v0.4.1

Published

A gulp plugin to generate responsives images.

Downloads

3,274

Readme

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:

  • lion-sm.jpeg
  • lion-sm.webp
  • lion-sm.avif
  • lion-lg.jpeg
  • lion-lg.webp
  • lion-lg.avif

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

Features

  • Based on Sharp
  • Takes options to generate images by sizes and format
  • Supports theses formats:
    • jpeg
    • png
    • gif
    • webp
    • avif
    • heif
    • tiff
  • Can pass Sharp specific options to customize even more the image generation
  • Written in TypeScript, so you get type hints for the options

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("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