---
type: snippet
tags: ['node', 'sharp', 'image']
status: release
ctime: 2022-04-09
mtime: 2024-03-22
---

```js
import sharp from 'sharp'
import fg from 'fast-glob'

const entries = await fg('./**/*.png')

for (const entry of entries) {
  const trimmedBuffer = await sharp(entry).trim().toBuffer()
  const trimmedImage = sharp(trimmedBuffer)
  const trimmedMetadata = await trimmedImage.metadata()

  trimmedImage
    .resize({
      width: Math.round(trimmedMetadata.width / 2),
      height: Math.round(trimmedMetadata.height / 2),
    })
    .png()
    .toFile('output.png')
}
```

offset(top, left)값, 이미지 사이즈 설정으로 crop 구현이 가능하다

```js
sharp('img.png')
  .extract({
    left: 50,
    top: 50, 
    width: 200, 
    height: 400
  })
```

---

- [resize](https://sharp.pixelplumbing.com/api-resize#resize)
- [extract](https://sharp.pixelplumbing.com/api-resize#extract)