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

@dolos/jpegoptim

v0.5.0

Published

Optimize JPEGs in memory

Downloads

11

Readme

node-jpegoptim

Optimize JPEGs in memory

What

Losslessly transform jpegs to save some bytes. This is primarily done by telling libjpeg to optimize huffman codes, and by throwing away some garabge segments that another software might have inserted.

Also allows to strip metadata (EXIF, IPTC, XMP) segments and/or ICC profile segments (tho your colors will look bigly wrong).

Requirements

  • Node, obviously, a recent version (at the time of writing, 10 and 11 tested).
  • Linux, macOS, potentially other *nix (untested)
  • x86_64, potentially others (untested)
  • libjpeg (or libjpeg-turbo or libmozjpeg, whatever pkg-config finds as libjpeg)
  • Optionally libexif to enable stripThumbnail.

E.g. to build it on macOS with mozjpeg and libexif from brew, do:

export PKG_CONFIG_PATH=$(brew --prefix mozjpeg)/lib/pkgconfig:$(brew --prefix libexif)/lib/pkgconfig
yarn add @dolos/jpegoptim

How

The API is really simple: there is only one function (default export of the module) and one custom error type.

jpegoptim(buf, [options])

  • @param {Buffer} buf Buffer containing the JPEG to optimize
  • @param {Object} [options] Some options for you.
  • @param {Buffer|Number} [options.out] Either the output buffer to use. The result is the used slice. Or a number limitting the buffer size. If omitted, a new Buffer without upper bound for the size is returned. Please remember that this function might add JFIF headers in otherwise already optimized files, which can lead to slight growing in size.
  • @param {Boolean} [options.strip] Strip all meta data.
  • @param {Boolean} [options.stripICC] Strip all ICC profile data.
  • @param {Boolean} [options.stripThumbnail] Strip any EXIF thumbnail present in the image metadata. Requires that this module was compiled against libexif.
  • @returns {Promise<Buffer>} The optimized jpeg.
  • @throws TypeError
  • @throws RangeError
  • @throws OptimizeError

Moreover, there is a feature to dump the raw dct stream of an image. This allows to e.g. compare image data quickly without the need for full decoding, i.e. two images, e.g. one original and one losslessly optimized should still yield the same DCT stream.

jpegoptim.dumpdct(buf, func)

  • @param {Buffer} buf Buffer containing the JPEG to dump
  • @param {Function} func callback function receiving the raw dct buffers. The callback function will be called multiple times, usually with a line of blocks. Please note that the buffer func will receive will be reused and thus change between calls. As such, you must copy the buffer if you want to keep the contents. Callback invocations happen only before dumpdct returns. The function is synchronous.
  • @throws TypeError
  • @throws RangeError
  • @throws OptimizeError

Additionally jpegoptim has the following properties

  • @property {OptimizeError} OptimizeError Reference to OptimizeError

  • @property {Object} versions Library version of libjpeg etc

  • @property {Boolean} supportsThumbnailStripping Does this build support it?

See sample.js for a small program demonstrating the use.

Design

  • Uses whatever your system libjpeg is (or what pkg-config said it was).
  • To provide EXIF thumbnail stripping functionality, uses whatever libexif is on your system (if any). If there is none, that feature will not be available.
  • Offload to the node worker pool.
  • Avoid buffer memory copies. Operate directly on the input buffer. And either create an (external) output buffer, or operate directly on the user supplied output buffer.

Todo

(as in: probably won't do)

  • Support Windows
  • Support systems where libjpeg cannot be pkg-config'ed
  • Support node versions I am not using.