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

img-handler

v0.0.1

Published

Node.js Express url image handler using sharp module to resize JPEG, PNG, WebP, AVIF and TIFF images

Downloads

16

Readme

img handler

The use for this Express Node.js module is to handle images by URL.

Example

This needs to have sharp module installed previously.

npm install img-handler
const imgHandler = require('img-handler');

app.use( '/path', imgHandler() );
// https://domain.com/path/(params)/image.jpg

Setting

Images Directory and Extension File

It's recommended always set 'dir' option

'ext' option will set the extension of the image file

app.use( '/path', imgHandler({
  dir: './images/', // default './images'
  ext: '.png' // default '.jpg'
}));

// recommended use of absolute path
app.use( '/path', imgHandler({
  dir: path.resolve('./images/')
}));

If the image extension is not in the url it will use the one set in the 'ext' option or default

Parameters

This option will assign where the parameters to use will be getting from

app.use( '/path', imgHandler({
  dir,
  params: 'route' // default: 'route', opts: ['route', 'query', 'none']
}));

route : domain.com/path/{params}/image.jpg

  • domain.com/path/S100x100Q90Fw/image.jpg
  • domain.com/path/W100H100Q100Fj/image

query : domain.com/path/image.jpg?{params}

  • domain.com/path/image.jpg?s=100x100&Q=90&F=w
  • domain.com/path/image?H=100&W=100&q=90&f=j

none : domain.com/path/image.jpg

Options

Options are simple letters, for 'route' param are uppercase letters, and 'query' param is not case sensitive

  • S (size): resize image to width x height (values >= 10)
  • W (width): pixels wide the resultant image should be. Use null or undefined to auto-scale the width to match the height.
  • H (height): pixels high the resultant image should be. Use null or undefined to auto-scale the height to match the width.
  • F (format): force output to a given format (optional, default: 'jpeg')
  • Q (quality): quality, integer 1-100 (optional)

'route' param can omit key "S" and just put the value.

Setting 'size' and, 'width' or 'height', the setted value will be that of 'size'.

Setting the same option more times, the setted value will be the first.

domain.com/path/100x100Fw/image.jpg

Formats and Qualities

Formats are simple letters too. If not set the 'quality' option, it will take the default value for each one.

  • j: jpeg, default quality: 80
  • p: png, default quality: 100
  • w: webp, default quality: 80
  • t: tiff, default quality: 80
  • a: avif, default quality: 50
  • h: heif, default quality: 50
  • r: raw
  • g: gif

More

Setting default values for each parameter, these will be assigned if no value has been setted for said option in 'route' or 'query', even for 'none'.

app.use( '/path', imgHandler({
  dir,
  ext,
  params,
  defaults: {
    size: {
      width: 100,
      height: 100
    },
    format: {
      type: 'jpeg',
      options: {
        quality: 80
      }
    }
  }
}));

Licensing

Copyright 2021 apvald.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.