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

web-image-gen

v2.0.2

Published

Modern responsive web image generation and tooling

Downloads

33

Readme

web-image-gen

Modern responsive web image generation and tooling.

About

Functions

  • Generates modern optimized responsive web image sets in specified formats and sizes, based on your high-resolution original source images (both from under your static folder).
    • Input formats: avif, gif, jpg, jpeg, png, svg, tif, tiff, webp
    • Output formats: avif, gif, jpg, jpeg, png, webp
  • Creates importable manifests of generated imagesets (in your src/lib folder).
    • Output formats: js, json, ts
  • Tooling to manage original source images and any generated files.
  • Easily display imagesets with compatible UI components:

Features

  • Dynamic image names.
  • No import glob mess.
  • Keep images in static/ instead of src/.
  • Low-quality image placeholders (LQIP).
  • Cache-busting.
  • Image metadata (optional) for non-visual descriptions, credits, licenses, etc.
  • Web framework support:
    • Should work with most anything.
    • Author is using alongside SvelteKit and Vite.

Usage

Install

npm install --save-dev web-image-gen

Input

Imagine a web app with the following directory structure:

.
├── package.json
├── src
│   ├── ...
│   └── lib
│       └── ...
└── static
    └── images
        ├── countries
        │   ├── italy.jpg
        │   └── italy.json
        └── fruits
            ├── pear.jpg
            └── pear.json

In static/images we have subdirectories for different groups of images (countries, fruits). Under those subdirs we have original high-resolution source images named for each country or fruit (italy.jpg, pear.jpg). Alongside each original image, we have optional extra metadata in a json file.

Run

We'll run the command-line script to generate our images and manifests:

npx web-image-gen generate --verbose

Output

The directory structure of our web app will now look like this:

.
├── src
│   ├── ...
│   └── lib
│       └── assets
│           └── images
│               └── _gen
│                   ├── countries.json
│                   └── fruits.json
└── static
    └── images
        ├── countries
        │   ├── _gen
        │   │   ├── italy-400.avif
        │   │   ├── italy-400.jpg
        │   │   ├── italy-400.webp
        │   │   ├── italy-800.avif
        │   │   ├── italy-800.jpg
        │   │   └── italy-800.webp
        │   ├── italy.jpg
        │   └── italy.json
        └── fruits
            ├── _gen
            │   ├── pear-400.avif
            │   ├── pear-400.jpg
            │   ├── pear-400.webp
            │   ├── pear-800.avif
            │   ├── pear-800.jpg
            │   └── pear-800.webp
            ├── pear.jpg
            └── pear.json

The new _gen folders contain our generated images and manifests.

Import

We can now import the generated manifest files to load imageset data in our web app:

import fruitImageSets from 'src/lib/assets/images/_gen/fruits.json'

console.dir(fruitImageSets['pear'])

Outputs:

{
  default: '/images/fruits/_gen/pear-800.jpg?v=54321',
  formats: {
    avif: {
      400: '/images/fruits/_gen/pear-400.avif?v=54321',
      800: '/images/fruits/_gen/pear-800.avif?v=54321'
    },
    webp: {
      400: '/images/fruits/_gen/pear-400.webp?v=54321',
      800: '/images/fruits/_gen/pear-800.webp?v=54321'
    },
    jpg: {
      400: '/images/fruits/_gen/pear-400.jpg?v=54321',
      800: '/images/fruits/_gen/pear-800.jpg?v=54321'
    }
  },
  meta: {
    author: 'PhotographerPerson',
    authorLink: 'https://pixabay.com/users/PhotographerPerson/',
    description: 'Sliced apples and other fruits.',
    link: 'https://pixabay.com/photos/fruit-pear-pear/',
    title: 'Pear Basket',
    license: 'Pixabay',
    licenseLink: 'https://pixabay.com/service/license/'
  },
  placeholder: 'data:image/webp;base64,UklGASDFRngA...LowrlqAAA=',
  sizes: {
    400: {
      avif: '/images/fruits/_gen/pear-400.avif?v=54321',
      webp: '/images/fruits/_gen/pear-400.webp?v=54321',
      jpg: '/images/fruits/_gen/pear-400.jpg?v=54321'
    },
    800: {
      avif: '/images/fruits/_gen/pear-800.avif?v=54321',
      webp: '/images/fruits/_gen/pear-800.webp?v=54321',
      jpg: '/images/fruits/_gen/pear-800.jpg?v=54321'
    }
  }
}

Display

Compatible UI components:

Config

Default Config

/** @type {import('web-image-gen').Config} */
{
  // Images config.
  images: {
    // Output formats - Ordered!
    //  First entry should be newest modern format, last entry should
    //  be most widely supported older fallback format.
    formats: ['avif', 'webp', 'jpg'],

    // Output width sizes in pixels.
    sizes: [400, 800],

    // Default single generated image to fallback on or use outside imageset.
    //  Must refer to a format and size defined above.
    default: {
      format: 'jpg',
      size: 800,
    },

    // Static assets folder.
    static: resolve('static'),

    // Images subdir underneath the <static> folder above.
    //  Original source subdirs and images will be read from within.
    //  Generated image files will be created within (under <slug> dir).
    //  Root of web-serving path in browser.
    images: 'images',

    // Subdir to generate images in (under `<static>/<images>/subdir/` above).
    slug: '_gen',

    // Version update for browser cache-busting
    version: Date.now().toString(),
  },

  // Manifests config
  manifests: {
    // Manifest output format. One of `json`, `js`, or `ts`.
    format: 'json',

    // Web app source code path for code dealing with static image assets.
    //  Where manifests and subdir will be generated.
    src: resolve('src/lib/assets/images'),

    // Subdir to put generated manifests in (under <src> above).
    slug: '_gen',
  },
}

Config File

Standard File

If either exists, config will be read from the file and merged against the defaults:

  • .web-image-gen.js
  • .web-image-gen.json
Custom File

A custom configuration file can be used with the --config command-line option (js or json). It will be merged against the default configuration.

Help

npx web-image-gen help            # or --help
npx web-image-gen help <command>  # or <command> --help

Repository

To keep generated files out of your source control repository, add to something like a .gitignore file (based on our default configuration above):

src/lib/assets/images/_gen/
static/images/*/_gen/

Make sure to trigger generation in your npm scripts.

Development

git checkout https://github.com/brev/web-image-gen.git
cd web-image-gen/packages/cli
npm install -g pnpm
pnpm install
pnpm run clean
pnpm run format
pnpm run lint
pnpm run test:cover
SNAPSHOT_UPDATE=1 pnpm run test
pnpm run build

License

MIT