jwagner / jwagner/smartcrop-sharp
Support current sharp version
- Dominant language
- JavaScript
- Stars
- 119
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
The code perfectly fits the latest sharp version, however package.json still points to a quite old release.
I created this modernized version, might be helpful for anyone:
```js
// Forked from https://github.com/jwagner/smartcrop-sharp/blob/f7d9207ce40e4ba269c6b7a3a4a4a4454a92279d/index.js
// due dependency resolve conflict
import smartcrop from 'smartcrop';
import sharp from 'sharp';
const iop = {
async open(src) {
const image = src instanceof sharp ? src : sharp(src);
const metadata = await image.metadata();
return {
width: metadata.width,
height: metadata.height,
_sharp: image,
};
},
resample(image, width, height) {
// This does not clone the image, better performance but fragile
// (depends on the assumtion that resample+getData is only called once per img)
return new Promise(resolve => {
resolve({
width: Math.trunc(width),
height: Math.trunc(height),
_sharp: image._sharp,
});
});
},
async getData(image) {
const data = await image._sharp
.resize(image.width, image.height, {kernel: sharp.kernel.cubic})
.ensureAlpha(1)
.toColourspace('srgb')
.raw()
.toBuffer();
return new smartcrop.ImgData(image.width, image.height, data);
},
};
export const crop = function (img, options, callback) {
options ||= {};
options.imageOperations = iop;
return smartcrop.crop(img, options, callback);
};
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.