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

image-blob-reduce

v4.1.0

Published

High quality image resize in browser for blobs (`pica` wrapper with some sugar)

Downloads

143,526

Readme

image-blob-reduce - downscale blobs with images inside

CI NPM version

Wrapper for pica to work with blobs, with some sugar.

This is pica wrapper for convenient work with images from file input fields. While pica works with raw bitmaps, this package operates with "image files". Additional features are:

  • [jpeg] Apply orientation to downscaled result.
  • [jpeg] Keep metadata, but with patched orientation & removed original color profile.
  • Easy to monkey-patch for your needs.

Demo

Install

npm install image-blob-reduce

Known issues

This package is not compaible with some minification options. If you use terser, disable evaluate or all compress options:

  • { compress: { evaluate: false } }
  • { compress: false }

This should not cause notable assets increase

Usage

const reduce = require('image-blob-reduce')();

//...

reduce
  .toBlob(image_blob, { max: 1000 })
  .then(blob => { ... });

If you load prebuild script in browser, use window.ImageBlobReduce

API

new ImageBlobReduce([options])

Create new reducer. Options:

  • pica - instance of pica, if you wish different defaults or shareable webworkers pool.

Short call: require('image_blob_reduce')()

.toBlob(in_blob, options) => Promise(out_blob)

Downscale image to fit into max*max size. If blob contains jpeg, then orientation is applied and metadata from original image reused (with minimal change).

Options:

  • max - max allowed image size.
  • pica .resize() options - alpha, unsharpAmount, unsharpRadius, unsharpThreshold, cancelToken

.toCanvas(in_blob, options) => Promise(out_canvas)

The same as .toBlob(), but with canvas output.

.before(method_name, hook_fn)

Inject your custom handler before specified method. See .init() source code for example.

.after(method_name, hook_fn)

The same as .before(), but handler is injected after specified method.

.use(plugin_init, ...params) => this

Sugar to simplify assign of external plugins. Just calls plugin_init(this, ...params).

.utils

require('./lib/utils'), to simplify modifications.

Reexports

  • ImageBlobReduce.pica => require('pica') - useful to customize pica options.

Customization

Since it's difficult to implement all possible options, this package is specially designed for easy customization. See source code first.

  • You can inherit class & replace existing methods.
  • You can add extra actions before/after existing method.
  • You can override existing methods of instance.

For example, if you wish force output to be always jpeg with some quality:

const reducer = require('image-blob-reduce')();

reducer._create_blob = function (env) {
  return this.pica.toBlob(env.out_canvas, 'image/jpeg', 0.8)
    .then(function (blob) {
      env.out_blob = blob;
      return env;
    });
};

Or rewrite scaling logic, introducing min option instead:

const reducer = require('image-blob-reduce')();

reducer._calculate_size = function (env) {
  const scale_factor = env.opts.min / Math.min(env.image.width, env.image.height);

  if (scale_factor > 1) scale_factor = 1;

  env.transform_width = Math.max(Math.round(env.image.width * scale_factor), 1);
  env.transform_height = Math.max(Math.round(env.image.height * scale_factor), 1);
  
  return env;
};