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

rightimage

v3.0.0

Published

Stream images with dynamic re-orientation

Downloads

9,907

Readme

rightimage

NPM version Build Status Coverage Status

This module is a small library for streaming dynamic images. Its key feature is to automatically detect and correct oritentation.

Use

The library exposes a function that can be passed image processing options and will return a stream. We carefully arrange for error propogation and teardown of resources to ensure operation in servers is safe.

const fs = require("fs");

const rightImage = require("rightimage");

rightImage.createRightImagePipeline(
  {
    contentType: "image/jpeg",
    imageOptions: {
      setFormat: "png",
      resize: "100,100"
    },
    inputStream: fs.createReadStream("./testdata/test.jpg")
  },
  (err, pipelineResult) => {
    if (err) {
      // call error handling code
      return callback(err);
    }

    const { outputContentType, outputStream } = pipelineResult;

    const outputFile = "./testdata/output/test_small.png";
    const outputFileStream = fs.createWriteStream(outputFile);
    outputFileStream.on("close", () => {
      // call some callback to signify success
      callback(null, `wrote an ${outputContentType} to path ${outputFile}`);
    });

    outputStream.pipe(outputFileStream);
  }
);
'wrote an image/png to path ./testdata/output/test_small.png'

The example above would take the test JPEG file in the project repository and convert it to a 100x100 PNG write the output "wrote image/png". Since the source JPEG has an orientation, it will be oriented correctly without any additional steps required.

Implementation

The primary trick is to read the first 128K bytes of the image on-the-fly and parse the EXIF data for the image oritentation. We use any present orientation data to calculate the correction required and trigger rotation via image processing libraries. The image data is never buffered.

Production safety

This module is intended to be used in production situations for the dynamic conversion of untrusted image data; it is imperative that the library is safe. A great deal of emphasis has been placed on error codepath hardening and the validation of any operations that will be performed.

Every requested format conversion and transformation operation is checked against a set of whitelisted operations and the module will not proceed if these checks fail. This module will always prefer a safer feature subset.

Image processing

Internally two modules are used to do the core image manipulation work.

impro

This awesome library wraps multiple image libraries - those configured by rightimage are sharp and Gifsicle (for the correct conversion of all GIFs including those with animated frames).

We bypass the outer layer and instead use the lower-level "operations API" where we construct an array of operations and pass that directly into the core fo the library. Based on input options and input content-type, will construct a streaming pipeline that will perform the conversion.

jpegtran

In the case of JPEGs that require nothing more than an orientation change we switch over to the jpegtran library to ensure we make a best effort to best preserve the image quality.