media-proxy/built/image-processor.js
2023-03-17 13:44:13 +00:00

28 lines
695 B
JavaScript

import sharp from 'sharp';
export const webpDefault = {
quality: 77,
alphaQuality: 95,
lossless: false,
nearLossless: false,
smartSubsample: true,
mixed: true,
effort: 2,
};
export function convertToWebpStream(path, width, height, options = webpDefault) {
return convertSharpToWebpStream(sharp(path), width, height, options);
}
export function convertSharpToWebpStream(sharp, width, height, options = webpDefault) {
const data = sharp
.resize(width, height, {
fit: 'inside',
withoutEnlargement: true,
})
.rotate()
.webp(options);
return {
data,
ext: 'webp',
type: 'image/webp',
};
}