u
This commit is contained in:
@@ -110,40 +110,36 @@ export async function handlePimage(req, res) {
|
|||||||
return res.status(304).end();
|
return res.status(304).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = image.buffer;
|
const imageSharp = sharp(image.buffer);
|
||||||
let contentType = 'image/jpeg';
|
const meta = await imageSharp.metadata();
|
||||||
|
|
||||||
try {
|
let pipeline = imageSharp
|
||||||
const imageSharp = sharp(buffer);
|
.resize(targetW, targetH, {
|
||||||
const meta = await imageSharp.metadata();
|
|
||||||
|
|
||||||
let pipeline = imageSharp.resize(targetW, targetH, {
|
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
withoutEnlargement: true,
|
withoutEnlargement: true,
|
||||||
});
|
})
|
||||||
|
.flatten({ background: '#ffffff' });
|
||||||
|
|
||||||
const accept = String(req.headers.accept || '');
|
const accept = String(req.headers.accept || '');
|
||||||
const formatParam = typeof req.query.format === 'string' ? req.query.format.toLowerCase() : '';
|
const formatParam = typeof req.query.format === 'string' ? req.query.format.toLowerCase() : '';
|
||||||
|
|
||||||
if (formatParam === 'avif' || (!formatParam && accept.includes('image/avif'))) {
|
let contentType = 'image/jpeg';
|
||||||
pipeline = pipeline.avif({ effort: 2 });
|
if (formatParam === 'avif' || (!formatParam && accept.includes('image/avif'))) {
|
||||||
contentType = 'image/avif';
|
pipeline = pipeline.avif({ effort: 2 });
|
||||||
} else if (formatParam === 'webp' || (!formatParam && accept.includes('image/webp'))) {
|
contentType = 'image/avif';
|
||||||
pipeline = pipeline.webp({ quality: 85 });
|
} else if (formatParam === 'webp' || (!formatParam && accept.includes('image/webp'))) {
|
||||||
contentType = 'image/webp';
|
pipeline = pipeline.webp({ quality: 85 });
|
||||||
} else if (formatParam === 'png' || (!formatParam && meta.hasAlpha)) {
|
contentType = 'image/webp';
|
||||||
pipeline = pipeline.png();
|
} else if (formatParam === 'png' || (!formatParam && meta.hasAlpha)) {
|
||||||
contentType = 'image/png';
|
pipeline = pipeline.png();
|
||||||
} else {
|
contentType = 'image/png';
|
||||||
pipeline = pipeline.jpeg({ quality: 85 });
|
} else {
|
||||||
contentType = 'image/jpeg';
|
pipeline = pipeline.jpeg({ quality: 85 });
|
||||||
}
|
contentType = 'image/jpeg';
|
||||||
|
|
||||||
buffer = await pipeline.toBuffer();
|
|
||||||
} catch (resizeErr) {
|
|
||||||
console.warn(`[pimage] resize/convert fallback for ${rawPath}:`, resizeErr.message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const buffer = await pipeline.toBuffer();
|
||||||
|
|
||||||
res.setHeader('Content-Type', contentType);
|
res.setHeader('Content-Type', contentType);
|
||||||
res.setHeader('Content-Length', String(buffer.length));
|
res.setHeader('Content-Length', String(buffer.length));
|
||||||
res.setHeader('Cache-Control', 'public, max-age=86400');
|
res.setHeader('Cache-Control', 'public, max-age=86400');
|
||||||
|
|||||||
Reference in New Issue
Block a user