sharp by wasm
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import sharp from 'sharp';
|
||||
import { optimizeImage } from 'wasm-image-optimization';
|
||||
import * as dbCompanySetting from '../../db/companysetting.js';
|
||||
import { crawlAndProcessImage } from '../../services/imageCrop.js';
|
||||
|
||||
@@ -60,22 +60,29 @@ async function generateWebpThumbnail(img, { signal, preferSource = false } = {})
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
const thumb = await sharp(buffer)
|
||||
.rotate()
|
||||
.resize(200, 200, {
|
||||
fit: 'inside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.webp({ quality: 80 })
|
||||
.toBuffer({ resolveWithObject: true });
|
||||
const meta = await optimizeImage({ image: buffer, format: 'none' });
|
||||
const srcW = meta.originalWidth || 0;
|
||||
const srcH = meta.originalHeight || 0;
|
||||
const needsResize = srcW > 200 || srcH > 200;
|
||||
const scale = needsResize ? Math.min(200 / srcW, 200 / srcH) : 1;
|
||||
|
||||
const dataUrl = `data:image/webp;base64,${thumb.data.toString('base64')}`;
|
||||
const thumb = await optimizeImage({
|
||||
image: buffer,
|
||||
width: needsResize ? Math.max(1, Math.round(srcW * scale)) : undefined,
|
||||
height: needsResize ? Math.max(1, Math.round(srcH * scale)) : undefined,
|
||||
fit: 'fill',
|
||||
format: 'webp',
|
||||
quality: 80,
|
||||
});
|
||||
|
||||
const thumbBuffer = Buffer.from(thumb.data);
|
||||
const dataUrl = `data:image/webp;base64,${thumbBuffer.toString('base64')}`;
|
||||
|
||||
return {
|
||||
dataUrl,
|
||||
width: thumb.info.width,
|
||||
height: thumb.info.height,
|
||||
size: thumb.info.size,
|
||||
width: thumb.width,
|
||||
height: thumb.height,
|
||||
size: thumbBuffer.length,
|
||||
};
|
||||
} catch (err) {
|
||||
lastError = err;
|
||||
|
||||
Reference in New Issue
Block a user