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-html-img-to-picture

v1.0.3

Published

Gulp plugin to wrap tag <img> with <picture> adding <source> including .avif and .webp and sort sources inside <picture> by size

Downloads

40

Readme

alt text

gulp-html-img-to-picture

Gulp plugin to wrap tag <img> with <picture> adding <source> including .avif and .webp and sort sources inside <picture> by size

The Idea of Plugin

I checked all popular packages that wrapping <img> with <picture> and combined them all in one with extra options & parameters like: sorting by size, adding custom image extenstions, ignoring certain image(s)

A list of packages that inspired me. You can check them out if you need more similar / simpler solutions

Features

  • Works with any format if <img> tag (no need to write tag in one line, it can be tabulated)
  • Adds <source> with .avif & .webp by default ( can be adjusted in parameters ) and possibility to add custom image extensions
  • Ignore <img> inside HTML Comments
  • Ignore <img> inside <script></script> that inlined into html
  • Ignore existing <picture> tags
  • Option to add a class for the <picture> tag after building the project using a special attribute <img data-picture-class="picture-wrapper"> in the source code
  • Option to sort sources inside <picture> by their size to force browser download the smallest one
  • Option to remove <sources> with incorrect or unexisted src attribute
  • Possibility not to wrap certain image(s) with special attribute or class name <img data-ignore class="img-ignore" src="" alt="">

Install

$ npm i --save-dev gulp-html-img-to-picture

Usage

Use this into your gulpfile.js:

const gulp = require('gulp')
const imgToPicture = require("gulp-html-img-to-picture")

function html() {
    return src('src/*.html')
        .pipe(imgToPicture({
            imgFolder: './dist',
        }))
        .pipe(gulp.dest('dist/'))
}

exports.html = html;

with modern ES6 modules syntax:

import gulp from "gulp"
import imgToPicture from "gulp-html-img-to-picture"

const html = () => {
    return src('src/*.html')
        .pipe(imgToPicture({
            imgFolder: './dist',
        }))
        .pipe(gulp.dest('dist/'))
}

exports.html = html;

Recommended to use with gulp-plumber :

import gulp from "gulp"
import plumber from "gulp-plumber"
import imgToPicture from "gulp-html-img-to-picture"

const html = () => {
    return src('src/*.html')
        .pipe(plumber({
            errorHandler: function (err) {
                notify.onError({
                    title: "HTML Error",
                    message: "<%= error.message %>"
                })(err)
            }
        }))
        .pipe(imgToPicture({
            imgFolder: './dist',
        }))
        .pipe(gulp.dest('dist/'))
}

exports.html = html;

Parameters | Options

imgToPicture({
    imgFolder: './dist', // required for sorting by size
    extensions: ['.jpg', '.jpeg', '.png'],
    ignoreClassname: 'img-ignore',
    ignoreAttribute: 'data-ignore',
    pictureClassAttribute: 'data-picture-class',
    logger: true,
    sortBySize: true,
    ignoreScripts: true,
    ignoreComments: true,
    filterUnexistedImages: true,
    sourceExtensions: [
        {
            extension: 'avif',
            mimetype: 'image/avif',
        },
        {
            extension: 'webp',
            mimetype: 'image/webp',
        },
    ],
})

Options


imgFolder

Type: string Default: false

Pass an relative path to build folder where images are located. It's required only parameter in case you want to use sorting by image size In my Gulp Boilerplate it's './dist'


extensions

Type: array[] Default: ['.jpg', '.jpeg', '.png']

Pass an array of image extensions you want to wrap with <picture>


ignoreClassname

Type: string Default: 'img-ignore'

The images with this class name won't be wrapped with <picture>


ignoreAttribute

Type: string Default: 'data-ignore'

The images with this attribute won't be wrapped with <picture>


pictureClassAttribute

Type: string Default: 'data-picture-class'

The attribute for adding CSS class name to <picture> that img will be wrapped with


logger

Type: boolean Default: true

If true, you will see the count of converted & ignored images in console logger


sortBySize

Type: boolean Default: true

If true, <img> and <source> in the <picture> will be sorted by their size

imgFolder parameter is required for this option


ignoreScripts

Type: boolean Default: true

If true, <img> inside <script></script> will be ignored


ignoreComments

Type: boolean Default: true

If true, <img> inside HTML comments will be ignored


filterUnexistedImages

Type: boolean Default: true

If true, plugin will check sources path. <sources> with incorrect or unexisted src attribute will be removed from <picture>


sourceExtensions

Type: array of ojects,json Default:

[
   {
       extension: 'avif',
       mimetype: 'image/avif',
   },
   {
       extension: 'webp',
       mimetype: 'image/webp',
   },
]

An array of image extensions which will be existes as <source> inside <picture>. You can adjust their order in case if sortBySize is set to false.

Examples

// Input
<img src="img/test.jpg" alt="image"> 
// Output
<picture>
    <source srcset="img/test.avif" type="image/avif"><!-- 1 KB  -->
    <source srcset="img/test.webp" type="image/webp"><!-- 1.1 KB -->
    <img src="img/test.jpg" alt="image"><!-- 1.3 KB -->
</picture>

Sorting

In specific cases WebP will be smaller then Avif, it will be automatically sorted if sortBySize is set to true.

// Output
<picture>
    <source srcset="img/test.webp" type="image/webp"><!-- 0.9 KB -->
    <source srcset="img/test.avif" type="image/avif"><!-- 1 KB  -->
    <img src="img/test.jpg" alt="image"><!-- 1.3 KB -->
</picture>

Image ignoring / Excluding from wrapping

To ignore an image from being converted to picture you have to add data-ignore attribute or img-ignore class name. also can be adjusted with these parameters

ignoreClassname, ignoreAttribute
// Input
<img src="assets/img/test.jpg" alt="">

<img data-ignore class="img-ignore" src="assets/img/test.jpg" alt="">

<img class="img-ignore" src="assets/img/test.jpg" alt="">
// Output
<picture>
    <source srcset="assets/img/test.webp" type="image/webp">
    <img src="assets/img/test.jpg" alt="">
    <source srcset="assets/img/test.avif" type="image/avif">
</picture>

<img data-ignore class="img-ignore" src="assets/img/test.jpg" alt="">

<img class="img-ignore" src="assets/img/test.jpg" alt="">

Browser support