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

@misskey-dev/browser-image-resizer

v2024.1.0

Published

A browser-based utility to downscale and resize images using OffscreenCanvas

Downloads

10,775

Readme

browser-image-resizer

Introduction

This library allows for cross-browser image downscaling utilizing OffscreenCanvas.

Note

  • This is browser-only utility and will not work in Node.js.
  • Safari 16.4 or later is required due to the use of OffscreenCanvas.
    https://caniuse.com/offscreencanvas

Installation

NPM/Yarn/pnpm

  • npm install @misskey-dev/browser-image-resizer
  • yarn add @misskey-dev/browser-image-resizer
  • pnpm add @misskey-dev/browser-image-resizer

Usage

In the main thread

import { readAndCompressImage } from 'browser-image-resizer';

const config = {
  quality: 0.7,
  width: 800,
  height: 600
};

// Note: A single file comes from event.target.files on <input>
async function uploadImage(file) {
  try {
    let resizedImage = await readAndCompressImage(file, config);

    const url = `http://localhost:3001/upload`;
    const formData = new FormData();
    formData.append('images', resizedImage);
    const options = {
      method: 'POST',
      body: formData
    };

    let result = await fetch(url, options);

    // TODO: Handle the result
    console.log(result);
    return result;
  } catch (error) {
    console.error(error);
    throw(error);
  }
}

In worker

Even large images can be processed in a separate thread using a worker.

worker.js

import { readAndCompressImage } from "browser-image-resizer";

onmessage = async (e) => {
    const converted = await readAndCompressImage(e.data, { maxWidth: 300 });
    postMessage(converted, [converted]);
}

Main Thread

const worker = new Worker('worker.js');

const img = document.getElementById('viewer_img');
worker.onmessage = (e) => {
  img.src = URL.createObjectURL(e.data);
};

async function convert(file: File) {
  const bmp = await createImageBitmap(file);
  worker.postMessage(bmp, [bmp]);
}

API

readAndCompressImage(file, config) => Promise<Blob | OffscreenCanvas>

Inputs

  • file: An image source that createImageBitmap can read.
    See https://developer.mozilla.org/en-US/docs/Web/API/createImageBitmap
  • config: See below

| Property Name | Purpose | Default Value | | ------------- |-------------| -----:| | argorithm | Algorithm used for downscaling (see below) | 'null' | | processByHalf | Whether to process downscaling by drawImage(source, 0, 0, source.width / 2, source.height / 2) until the size is smaller than twice the target size. | true | | quality | The quality of jpeg (or webp) | 0.5 | | maxWidth | The maximum width for the downscaled image | 800 | | maxHeight | The maximum height for the downscaled image | 600 | | debug | console.log image update operations | false | | mimeType | specify image output type other than jpeg / If set null, function returns OffscreenCanvas | 'image/jpeg' |

argorithm
  • null: Just resize with drawImage(). The best quality and fastest.
  • bilinear: Better quality, slower. Comes from upstream (ericnogralesbrowser-image-resizer).
  • hermite: Worse quality, faster. Comes from viliusle/Hermite-resize. Will dispatch workers for better performance.
  • hermite_single: Worse quality, faster. Single-threaded.

Outputs

A Promise that yields an Image Blob or OffscreenCanvas

calculateSize(src, config)

calculateSize(src: { width: number; height: number; }, config) => { width: number; height: number; }

With this function you can get the pre-calculated width and height of the resulting image.

Output Image Specification

The output image is derived from OffscreenCanvas.convertToBlob.
https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/convertToBlob

  • EXIF and other metadata will be erased.
  • Rotation will be automatically corrected.
    • It is based on the specifications of recent versions of modern browsers and may not work with older browsers.
    • See https://github.com/w3c/csswg-drafts/issues/4666#issuecomment-610962845
    • Firefox support seems to be available from version 78. by mei23
  • Color profile is srgb. Firefox 97 does not attach the ICC profile, but Chrome does.
  • You can specify image/webp as the mimeType but Safari does not support..