JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1208
  • Score
    100M100P100Q118217F
  • License (CC0-1.0 AND Apache-2.0 AND BSD-3-Clause)

Codecs for NodeJS

Package Exports

  • @astropub/codecs

Readme

Codecs

Codecs lets you use read, write, and edit different image formats.

npm install @astropub/codecs

Usage

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

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

image.data // Uint8ClampedArray of the decoded JPG
image.width // Width of the decoded JPG
image.height // Height of the decoded JPG

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

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

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

API

decode

Decodes the given image file to a raw image format.

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

// { data: Uint8ClampedArray, width: number, height: number }
const imageData = await codecs.jpg.decode(imageFile)

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

encode

Encodes the given image data to an encoded image format.

// Uint8Array
const encodeU8A = await codecs.webp.encode(imageData)

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

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

resize

Resizes the given image data and returns new image data.

// { data: Uint8ClampedArray, width: number, height: number }
const sizedData = await codecs.resize(imageData, { width: 320 })

If not specified, the height will be determined from the image width using the formula Math.round(width / imageData.width * imageData.height).

blur

Blurs an image and returns new image data.

// { data: Uint8ClampedArray, width: number, height: number }
const blurImageData = await codecs.blur(imageData, { radius: 30 })

blurhash

Encodes or decodes image data using the Wolt BlurHash algorithm.

// { data: string, width: number, height: number }
const blurhash = await codecs.blurhash.encode(imageData)

// { data: Uint8ClampedArray, width: number, height: number }
const blurhashImageData = await codecs.blurhash.decode(blurhash, { width: 32 })

If not specified, the height will be determined from the image width using the formula Math.round(width / imageData.width * imageData.height).

getType

Returns the content type of an image file.

// "image/jpeg"
const imageType = await codecs.getType(imageFile)

getExtension

Returns the extension of an image file.

// "jpg"
const imageExtension = await codecs.getExtension(imageFile)

License

Codecs is 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 (BSD), Copyright Joe Drago.

Code from MozJPEG is licensed under the Modified (3-clause) BSD License (BSD), 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 (MIT), Copyright Joshua Holmer.

Code from WebP is licensed under the Modified (3-clause) BSD License (BSD), 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.