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

reweight-image

v0.1.2

Published

Resize image files in browser by limiting its **size in megabytes** (its 'weight'), its **size in pixels** or its **jpeg quality**.

Downloads

2

Readme

reweight-image

Resize image files in browser by limiting its size in megabytes (its 'weight'), its size in pixels or its jpeg quality.

File size is reduced by progressively resizing image dimensions in pixels and/or by progressively reducing jpeg quality, until output is under given limits. It means that iterative steps will take the image and apply a factor to its size (starting from the input image dimensions or from the maximum specified size - the lower one) and/or to its jpeg quality (starting from the maximum specified quality or from 1 is none is specified).

Install

npm i reweight-image --save

Usage

Typical use is reduce a loaded image file size prior to upload it to server. You may want, also, showing the file in an HTML <img> element.

Typescript

import { ReweightImage, Convert } from 'reweight-image';

function reduceFile(image: File, imageElement: HTMLImageElement) {
  const reweight = new ReweightImage({fileSizeMb: 0.5}, {coverMaxImageSize: true}),
        convert = new Convert();
  reweight.compressImageFile(image).pipe(
    switchMap((reducedFile: File)=>{
      return convert.getBase64FromBlob(reducedFile);
    })
  ).subscribe({
    next: (base64image: string)=> {
      imageElement.src = base64image;
    }
  });
}

new ReweightImage(limits, options)

Creates a 'reweighter' to resize an image. Options is optional.

limits can hold the next properties (all are optional):

  • {number} fileSizeMb: maximum output file size, in megabytes
  • {number} imageSizePx: maximum output image dimensions, in pixels
  • {number} jpegQuality: output jpeg quality factor

File size and image size of the original file will be kept if passed values are bigger than the ones of original image. Jpeg quality will be 1 if omitted, as it is not read from original file.

options can hold the next properties (all are optional):

  • {boolean} coverMaxImageSize: if true and imageSizePx is given in limits, output image size will adjust to completely cover an square of imageSizePx. Default is set to false.
  • {number} imageSizeRatio: factor to apply in each
  • {number} jpegQualityRatio: number

[ReweightImage].compressImageFile(image)

Resizes an image.

Input must be a File object holding an image, usually taken from an <input type="file"> element. It will work with any valid image format, but it will not verify that input file is an actual valid image.

Returns an RxJs observable. Subscription will output a File object containing an image, according to parameters passed to ReweightImage constructor. Output file will hold, always, a jpeg image.

[Convert].getBase64FromBlob(image)

Converts an image in base64 format, a string, into a Blob object. Input must be a Blob object.

Returns an RxJs observable. Subscription will output a string which can be used as an image source for an HTML <img> tag.

[Convert].getBlobFromBase64(image)

Converts a Blob into an image in base64 format, a string.Input must be an image in base64 format, a string.

Returns the Blob object.