Skip to content

Instantly share code, notes, and snippets.

@webarthur
Created July 10, 2023 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webarthur/02cca620a2a3f1d2e206966c1e9275f8 to your computer and use it in GitHub Desktop.
Save webarthur/02cca620a2a3f1d2e206966c1e9275f8 to your computer and use it in GitHub Desktop.
Sharp: Resize if image is bigger than a specific size (use withoutEnlargement prop)
const sharp = require('sharp')
const maxWidth = 1200
const maxHeight = 2000
await sharp('uploads/image.png')
.resize(maxWidth, maxHeight * 1.5, {
fit: 'inside',
withoutEnlargement: true, // this
})
.webp({ effort: 6 })
.toFile('image.webp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment