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

img-flo-node-revision

v0.0.4

Published

Modify and optimize images at lightspeed

Downloads

2

Readme

Node bindings for Imageflow

Macos Linux Windows

Quickly scale or modify images and optimize them for the web.

If the AGPLv3 does not work for you, you can get a commercial license on a sliding scale. If you have more than 1 server doing image processing your savings should cover the cost.

API docs are here.

Installation

npm install @imazen/imageflow

Usage

const {
    MozJPEG,
    Steps,
    FromURL,
    FromFile,
    FromStream,
    FromBuffer,
} = require('@imazen/imageflow')
const fs = require('fs')

let step = new Steps(new FromURL('https://jpeg.org/images/jpeg2000-home.jpg'))
    .constrainWithin(500, 500)
    .branch((step) =>
        step
            .constrainWithin(400, 400)
            .branch((step) =>
                step
                    .constrainWithin(200, 200)
                    .rotate90()
                    .colorFilterGrayscaleFlat()
                    .encode(new FromFile('./branch_2.jpg'), new MozJPEG(80))
            )
            .copyRectangle(
                (canvas) =>
                    canvas.decode(
                        new FromStream(fs.createReadStream('./test.jpg'))
                    ),
                { x: 0, y: 0, w: 100, h: 100 },
                10,
                10
            )
            .encode(new FromFile('./branch.jpg'), new MozJPEG(80))
    )
    .constrainWithin(100, 100)
    .rotate180()
step.encode(new FromBuffer(null, 'key'), new MozJPEG(80))
    .execute()
    .then(console.log)
    .catch(console.log)

Examples

  1. Reading a file from disk. FromFile provide an easy method for reading and writing images to disk.
const { MozJPEG, Steps, FromFile } = require('@imazen/imageflow')

const output = await new Step(new FromFile('path/to/file'))
    .rotate180()
    .encode(new FromFile('./path/to/output/file'))
    .execute()
  1. Reading from a stream. FromStream can read and write to a stream.
const { MozJPEG, Steps, FromStream } = require('@imazen/imageflow')

const output = await new Step(new FromStream(req))
    .constrainWithin(400, 400)
    .encode(new FromStream(res))
    .execute()
res.end()
  1. Reading from a url. FromURL can make a GET request to download and POST request to upload the image.
const { MozJPEG, Steps, FromURL } = require('@imazen/imageflow')

const output = await new Step(new FromURL('url to image'))
    .colorFilterGrayscaleFlat()
    .encode(new FromURL('url to image upload'))
    .execute()
  1. Providing buffer. FromBuffer can read and provide the output buffer. To read the output a key should be provided, which used later to access buffer in the output.
const { MozJPEG, Steps, FromBuffer } = require('@imazen/imageflow')

const output = await new Step(new FromBuffer(getSomeBuffer()))
    .colorFilterGrayscaleFlat()
    .encode(new FromBuffer(null, 'key'))
    .execute()
console.log(output.key)
  1. Performing Batch operations. branch, decode, and encode are used together to perform batch operation. This example shows how to create varying size images from a single image.
const { MozJPEG, Steps, FromStream, FromFile } = require('@imazen/imageflow')

const test = new Steps(new FromStream(req))
    .constrainWithin(800, 800)
    .branch((step) => step.encode(new FromFile('large.jpg'), new MozJPEG()))
    .branch((step) =>
        step
            .constrainWithin(400, 400)
            .branch((step) =>
                step
                    .constrainWithin(200, 200)
                    .branch((step) =>
                        step
                            .constrainWithin(100, 100)
                            .encode(
                                new FromFile('tiny.jpg'),
                                new MozJPEG()
                            )
                    )
                    .encode(new FromFile('small.jpg'), new MozJPEG())
            )
            .encode(new FromFile('medium.jpg'), new MozJPEG())
    )
    .execute()
  1. Example with complex graph of operations
const {
    MozJPEG,
    Steps,
    FromURL,
    FromFile,
    FromStream,
    FromBuffer,
} = require('@imazen/imageflow')
const fs = require('fs')

let step = new Steps(new FromURL('https://jpeg.org/images/jpeg2000-home.jpg'))
    .constraintWithin(500, 500)
    .branch((step) =>
        step
            .constraintWithin(400, 400)
            .branch((step) =>
                step
                    .constraintWithin(200, 200)
                    .rotate90()
                    .colorFilterGrayscaleFlat()
                    .encode(new FromFile('./branch_2.jpg'), new MozJPEG(80))
            )
            .copyRectangle(
                (canvas) =>
                    canvas.decode(
                        new FromStream(fs.createReadStream('./test.jpg'))
                    ),
                { x: 0, y: 0, w: 100, h: 100 },
                10,
                10
            )
            .encode(new FromFile('./branch.jpg'), new MozJPEG(80))
    )
    .constraintWithin(100, 100)
    .rotate180()
step.encode(new FromBuffer(null, 'key'), new MozJPEG(80))
    .execute()
    .then(console.log)
    .catch(console.log)
  1. Using query style commands
await new Steps().executeCommand(
        'width=100&height=100&mode=max',
        new FromBuffer(str),
        new FromBuffer(null, 'key')
    )

Local Setup

git clone https://github.com/imazen/imageflow-node
cd imageflow-node
yarn
yarn test