Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
How to resize an image proportionally using a blur effect, instead of letterboxing, on Node.js?

I have a photo which is a rectangle and I want to resize it down to a square while preserving the aspect ratio. Libraries like Sharp allow you to do that by applying letterboxing to the resulting image.

await sharp(photoBuffer).resize({ width: 200, height: 200, fit: 'contain' })

This is the result:

with letterboxing

Instead of applying letterboxing I'd like the remaining empty space to be filled with a 2nd blurred version of the image, placed behind the resized one, like so:

example

Is there a Node.js library that does that out of the box or some custom way of achieving this ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Turns out this is how you do it using Sharp:

import Sharp from 'sharp'

export default function({ pathToInputFile, pathToOutputFile, size, blur }) {
  let sharpOriginal = Sharp(pathToInputFile)
  
  return new Promise((resolve) => {
    sharpOriginal
      .resize({ width: size })
      .toBuffer()
      .then((resizedBuffer) => {
        sharpOriginal
          .resize(size, size, { // the result will be a square
            fit: 'cover'
          })
          .blur(blur) // 6 seems to work well
          .composite([{
            input: resizedBuffer,
            gravity: 'center'
          }])
          .toFile(pathToOutputFile)
          .then((info) => {
            console.log(info)
            resolve(true)
          })
          .catch((err) => {
            console.error(err)
            resolve(false)
          })
      })
      .catch((err) => {
        console.error(err)
        resolve(false)
      })
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!