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

microjpeg

v0.3.0

Published

Deliver micro-sized images to clients by decoupling a static JPEG header

Downloads

3

Readme

MicroJPEG

Deliver micro-sized images to clients by decoupling a static JPEG header.

Inspired by Facebook's approach to delivering previews of cover photos, this library creates JPEG images with a static header and footer that can be stored statically in the client. It is designed for use on very small images (e.g. 32x32 pixels) where the header can be several times larger than the image data itself.

The image data can easily be delivered to the client in an API response as base64 encoded ASCII data, reassembled with the header and footer and displayed to the user whilst the full image downloads. This can work especially well with a Gaussian blur filter to mask the low resolution and low JPEG quality.

Installation

This module can be installed with npm:

$ npm install microjpeg

Usage

The module exposes two classes - the Deflator andInflator. These can both be imported from the module or directly from lib/deflator and lib/inflator.

import { Deflator, Inflator } from 'microjpeg';

// or

import Deflator from 'microjpeg/lib/deflator';
import Inflator from 'microjpeg/lib/inflator';

The structure is intended such that the Deflator is run on the server to produce the header, footer and image data, whereas the Inflator is run on the client to reassemble the image.

Deflator

The Deflator class takes a JPEG image and breaks it apart into its header, dynamic data and footer. The header is consistent for a given JPEG quality, the sole argument passed into the constructor. The header will be created assuming image size 0x0 but the true height and width are patched in by the Inflator later.

import { Deflator } from 'microjpeg';

const deflator = new Deflator(50); // 50 represents the JPEG quality from 0-100

const header = deflator.getHeader('base64');
console.log(header);
// static header for 0x0 image as a base64 encoded ASCII string
// /9j/4AAQSkZJRgABAQAAAQABtfY2dri4+Tl5...AAhEDEQA/AA==

const footer = deflator.getFooter('base64');
console.log(footer);
// static footer for image as a base64 encoded ASCII string
// /9k=

const filePath = './path/to/my-small-image.jpg';
const image = deflator.microsizeSync(filePath, 'base64');

console.log(image);
// the dynamic image data as a base64 encoded ASCII string
// rfZjjkV6h5lhfs3tRdBYUWue1FxcofZD6UXD...V7HrWkUQ2fw==

In the example above, the header and footer should be stored in the client to be used when we want to inflate the image later - these will always be identical for the same quality value passed into the Deflator constructor. The dynamic image is what can be returned in the API.

new Deflator(quality)

The constructor expects a single argument which defines the JPEG quality to use on a scale of 0-100. As a guide, a quality of 50 is a good starting point.

import { Deflator } from 'microjpeg';

const deflator = new Deflator(
  50 // JPEG quality from 0-100
);

deflator.getHeader()

deflator.getFooter()

deflator.microsize(file, encoding = null)

deflator.microsizeSync(file, encoding = null)

deflator.deflateFromJpeg(buffer)

deflator.deflate({ width, height, data })

Inflator

Contributing

MicroJPEG is written and maintained by Matt Knight. Contributions in the form of issues, feature requests and pull requests are always appreciated.

License

This library is licensed under the MIT License, and full details can be found in the LICENSE file.