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-blobber

v1.2.0

Published

A small module for reading HTML5 image file blobs.

Downloads

12

Readme

image-blobber

A small module for reading image file base64/dimensions, and scaling them to a max height or width.

Installation

image-blobber can be installed from npm.

npm install --save image-blobber
//ES6
import * as imageblobber from "image-blobber";

//Node-style require
const imageblobber = require("image-blobber");

Usage

All image-blobber functions are promisified.

Supported: boolean

An exported boolean that indicates whether the current environment supports the FileReader needed by the getFileBlobs function. If false, getFileBlobs will throw an error.

import {Supported} from "image-blobber";

if (!Supported)
{
    throw new Error("Your browser does not support the FileReader API.");
}

getFileBlobs(input: HtmlInputElement): Promise<File[]>

Gets all file blobs for an HTML5 file input element.

import {getFileBlobs} from "image-blobber";

const input = document.getElementById("my-file-input");

getFileBlobs(input).then((blobs) =>
{
    console.log(blobs.length); // 3
});

getBase64(file: File): Promise<BlobDetails>

Gets the base64 string, filename and dimensions for the given image file. See below for the BlobDetails interface.

import {getBase64} from "image-blobber";

const file: File = ...;

getBase64(file).then((details) =>
{
    console.log(details.filename);   // "my-file.png"
    console.log(details.base64);     // "data:image/png;base64,..."
    console.log(details.dimensions); // { height: 150, width: 75 }
});

scaleBase64(base64: string, options: ScaleOptions): Promise<ScaleResult>

Scales a base64 image string according to the options passed in. See below for the ScaleOptions and ScaleResult interfaces.

import {scaleBase64} from "image-blobber";

const base64: string = ...;

scaleBase64(base64, {height: 400, width: 400, preserveRatio: true}).then((scaledImage) =>
{
    console.log(scaledImage.scaledBase64);     // "data:image/png;base64,..."
    console.log(scaledImage.scaledDimensions); // { height: 150, width: 75 }
});

Tie it all together

import * as Promise from "bluebird";
import * as blobber from "file-blobber";

const input = document.querySelector("input") as HTMLInputElement;

blobber.getFileBlobs(input)
    .then((blobs) =>
    {
        return Promise.all(blobs.map(blob => blobber.getBase64(blob)));
    })
    .then((images) =>
    {
        return Promise.all(images.map(i => blobber.scaleBase64(i.base64, {height: 400, width: 400, preserveRatio: true})));
    })
    .then((scaledImages) =>
    {
        // Do something with the scaled images.
    })

Interfaces

The following interfaces are used or returned at some point by image-blobber. If you're using Typescript, the compiler should automatically pick up these definitions when image-blobber is installed.

Dimensions

| Property | Type | Comments | | -------- | ---- | -------- | | height | number | The image's height. | | width | number | The image's width. |

BlobDetails

| Property | Type | Comments | | -------- | ---- | -------- | | filename | string | The name of the file as it appears on the user's machine. | | base64 | string | A base64 string representing the image. Can be set as an <img /> element's src. | | dimensions | Dimensions | The image's height and width dimensions. |

ScaleResult

| Property | Type | Comments | | -------- | ---- | -------- | | scaledBase64 | string | A base64 string representing the scaled image. Can be set as an <img /> element's src. | | scaledDimensions | Dimensions | The scaled image's new height and width dimensions. |

ScaleOptions

| Property | Type | Comments | | -------- | ---- | -------- | | height | number | The maximum height allowed for a scaled image. Optional, but options must include either a height or width. | | width | number | The maximum width allowed for a scaled image. Optional, but options must include either a height or width. | | preserveRatio | boolean | Whether aspect ratio should be preserved. If true, image will be scaled to an aspect ratio that satisfies both height and width. Default true. |