21 lines
609 B
JavaScript
21 lines
609 B
JavaScript
import sharp from "sharp";
|
|
/**
|
|
* Convert to WebP
|
|
* with resize, remove metadata, resolve orientation, stop animation
|
|
*/ export async function convertToWebp(path, width, height, quality = 85) {
|
|
return convertSharpToWebp(await sharp(path), width, height, quality);
|
|
}
|
|
export async function convertSharpToWebp(sharp1, width, height, quality = 85) {
|
|
const data = await sharp1.resize(width, height, {
|
|
fit: "inside",
|
|
withoutEnlargement: true
|
|
}).rotate().webp({
|
|
quality
|
|
}).toBuffer();
|
|
return {
|
|
data,
|
|
ext: "webp",
|
|
type: "image/webp"
|
|
};
|
|
}
|