Arrow keys: ← previous · → next 7 of 37
@nodedk/image
0.2.0 stable server

image#

Re-exports Sharp directly. All options and defaults are Sharp's own.

Install#

npm i @nodedk/image

Usage#

var image = require('@nodedk/image')

The following examples run inside an async function.

Read metadata#

var metadata = await image('photo.jpg').metadata()
console.log(metadata.width, metadata.height, metadata.format)

A Buffer can be passed instead of a file path.

Resize and convert to WebP#

await image('photo.jpg')
  .resize({ width: 1200, withoutEnlargement: true })
  .webp({ quality: 85 })
  .toFile('photo.webp')

Convert to a buffer#

var output = await image(buffer)
  .webp({ quality: 85 })
  .toBuffer()

Resize to half the width#

var photo = image(buffer)
var { width } = await photo.metadata()

await photo
  .resize({ width: Math.max(1, Math.round(width / 2)) })
  .webp({ quality: 85 })
  .toFile('photo.webp')

Use streams#

var fs = require('node:fs')
var { pipeline } = require('node:stream/promises')

await pipeline(
  fs.createReadStream('photo.jpg'),
  image().webp({ quality: 85 }),
  fs.createWriteStream('photo.webp')
)

For advanced usage, see the Sharp repository and API documentation.

Created by Vidar Eldøy

Tools for business systems.