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

@astropub/codecs

v0.4.4

Published

Codecs for NodeJS

Downloads

5,581

Readme

Codecs

Codecs lets you use read, write, edit, and analyze images.

npm install @astropub/codecs

Usage

import * as fs from 'node:fs/promises'
import * as codecs from '@astropub/codecs'

// load the JPG image
const image = await codecs.load(
	await fs.readFile('./kitten.jpg')
)

image.type   // string representing the image type ('image/jpeg')
image.data   // Uint8Array representing the image data
image.width  // number representing the image width
image.height // number representing the image height
image.ext    // string representing the image extension ('jpg')

const decoded = await image.decode()

decoded.data   // Uint8ClampedArray representing the decoded image data
decoded.width  // number representing the decoded image width
decoded.height // number representing the decoded image height

// encode the image as Avif & WebP, at 320, 640, & 960
for (const size of [ 320, 640, 960 ]) {
	const resized = await decoded.resize({ width: size })

	for (const type of [ 'image/avif', 'image/webp' ]) {
		const encoded = await resized.encode(type, { quality: 80 })

		encoded.type   // string representing the encoded image type ('image/webp')
		encoded.data   // Uint8Array representing the encoded image data
		encoded.width  // number representing the encoded image width (320 | 640 | 960)
		encoded.height // number representing the encoded image height
		encoded.ext    // string representing the encoded image extension ('webp')

		await fs.writeFile(`./kitten-${size}.${encoded.ext}`, encoded.data)
	}
}

API

load

The load function returns a loaded image. It accepts a string path, file URL, Buffer, Response, or TypedArray.

const image = await codecs.load('./kitten.jpg')

image.type   // string representing the image type ('image/jpeg')
image.data   // Uint8Array representing the image data
image.width  // number representing the image width
image.height // number representing the image height
image.ext    // string representing the image extension ('jpg')

decode

The decode function returns a decoded image. It accepts a Buffer or TypedArray.

const buffer = await fs.readFile('./kitten.jpg')

const decoded = await codecs.decode(buffer)

decoded.data   // Uint8ClampedArray representing the decoded image data
decoded.width  // number representing the decoded image width
decoded.height // number representing the decoded image height

Individual decoders are available for avif, jpg, jxl, png, webp, and wp2.

import * as codecs from '@astropub/codecs'

codecs.avif.decode(await fs.readFile('./kitten.avif'))
codecs.jpg.decode(await fs.readFile('./kitten.jpg'))
codecs.jxl.decode(await fs.readFile('./kitten.jxl'))
codecs.png.decode(await fs.readFile('./kitten.png'))
codecs.webp.decode(await fs.readFile('./kitten.webp'))
codecs.wp2.decode(await fs.readFile('./kitten.wp2'))

encode

The encode function returns an encoded image. It accepts a decoded image.

const encodedImage = await codecs.encode(decoded, 'image/webp', { quality: 80 })

encoded.type   // string representing the encoded image type ('image/webp')
encoded.data   // Uint8Array representing the encoded image data
encoded.width  // number representing the encoded image width (320 | 640 | 960)
encoded.height // number representing the encoded image height
encoded.ext    // string representing the encoded image extension ('webp')

await fs.writeFile('./kitten.webp', encodedImage)

Individual encoders are available for avif, jpg, jxl, png, webp, and wp2.

import * as codecs from '@astropub/codecs'

codecs.avif.encode(decoded)
codecs.jpg.encode(decoded)
codecs.jxl.encode(decoded)
codecs.png.encode(decoded)
codecs.webp.encode(decoded)
codecs.wp2.encode(decoded)

resize

The resize function returns a resized image. It accepts a decoded image.

const resized = await codecs.resize(decoded, { width: 320 })

resized.data   // Uint8ClampedArray representing the resized image data
resized.width  // number representing the resized image width
resized.height // number representing the resized image height

If not specified, the resized height will be determined from the width using the formula width / naturalWidth * naturalHeight.

blur

The blur function returns a blurred image. It accepts a decoded image.

const blurred = await codecs.blur(decoded, { radius: 30 })

blurhash

The blurhash function returns a blurhashed image, using the Wolt BlurHash algorithm. It accepts a decoded image.

const blurhashed = await decoded.blurhash({ width: 32 })

If not specified, the height will be determined from the image width using the formula width / naturalWidth * naturalHeight.

type

The type function returns the content type for an image buffer. It accepts a Buffer or TypedArray.

// 'image/jpeg'
const type = await codecs.type(buffer)

ext

The ext function returns the file extension for an image buffer. It accepts a Buffer or TypedArray.

// 'jpg'
const ext = await codecs.ext(buffer)

DecodedImage

The DecodedImage class represents raw, decoded image data.

const decoded = new DecodedImage(
	data   // Uint8ClampedArray
	width  // number
	height // number
)

DecodedImage#encode

The encode function of DecodedImage returns a promised encoded image from the current decoded image.

const encoded = await decoded.encoded('image/webp') // EncodedImage<'image/web', Uint8Array>

DecodedImage#blur

The blur function of DecodedImage returns a promised blurred image from the current decoded image.

const blurred = await decoded.blur({ radius: 30 }) // DecodedImage

DecodedImage#blurhash

The blurhash function of DecodedImage returns a promised blurhashed image from the current decoded image.

const blurhash = await decoded.blurhash({ radius: 30 }) // DecodedImage

DecodedImage#resize

The resize function of DecodedImage returns a promised resized image from the current decoded image.

const resized = await decoded.resize({ width: 320 }) // DecodedImage

DecodedImage#color

The color property of DecodedImage returns the dominant color in the decoded image.

decoded.color // [ 57, 52, 43 ]

EncodedImage

The EncodedImage class represents analyzed, encoded image data.

const encoded = new EncodedImage(
	type   // string ('image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2')
	data   // Uint8Array
	width  // number
	height // number
)

EncodedImage#decode

The decode function of EncodedImage returns a promised decoded image from the current encoded image.

const decoded = await encoded.decoded()

Types

ImageType

The ImageType type represents known image content types.

import type { ImageType } from '@astropub/codecs'

// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType

ImageType

The ImageType type represents known image content types.

import type { ImageType } from '@astropub/codecs'

// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType

License

Codecs is generally a remix of Squoosh!.

Code original to this project is licensed under the CC0-1.0 License.

Code from Squoosh! is licensed under the Apache-2.0 License, copyright Google Inc.

Code from Avif Encoder is licensed under the BSD License, copyright Joe Drago.

Code from MozJPEG is licensed under the Modified (3-clause) BSD License, copyright Viktor Szathmáry.

Code from JXL is licensed under the Apache-2.0 License, copyright Google Inc.

Code from OxiPNG is licensed under the MIT License, copyright Joshua Holmer.

Code from WebP is licensed under the Modified (3-clause) BSD License, copyright Google Inc.

Code from WebP2 is licensed under the Apache-2.0 License, copyright Google Inc.

Code from blurhash is licensed under the MIT License, copyright Olli Mahlamäki.