我想保存图像后调整大小,所以使用夏普包,但不要小图像size.Iadded等待夏普(uploadImage.name).resize(600,600);行保存文件夹后,我错过了什么?
const path = require('path');
const fs = require('fs');
const AdminBro = require('admin-bro');
const sharp=require('sharp');
/** @type {AdminBro.After<AdminBro.ActionResponse>} */
const after = async (response, request, context) => {
const { record, uploadImage } = context;
if (record.isValid() && uploadImage) {
// console.log(uploadImage.name);
await sharp(uploadImage.name).resize(600, 600);
const filePath = path.join('uploads', record.id().toString(), uploadImage.name);
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await fs.promises.rename(uploadImage.path, filePath);
await record.update({ imagePath: `/${filePath}` });
}
return response;
};
/** @type {AdminBro.Before} */
const before = async (request, context) => {
if (request.method === 'post') {
const { uploadImage, ...otherParams } = request.payload;
// eslint-disable-next-line no-param-reassign
context.uploadImage = uploadImage;
return {
...request,
payload: otherParams,
};
}
return request;
};
module.exports = { after, before };const filePath = path.join('uploads', record.id().toString(), uploadImage.name);
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
await sharp(uploadImage.path).withMetadata().resize(600,600).toFile(filePath);//here added correct paths but now although it saved image, doesnt reduce imageSize发布于 2021-05-27 20:03:19
您没有将处理后的图像写入文件。
假设这是您的存储路径:
const outputPath = __dirname + `/../storage/myImage.jpeg`;您可以将该路径提供给sharp
await sharp('path/of/originalImage').withMetadata().resize(600, 600).toFile(outputPath);我强烈建议你使用withMetaData(),否则你会丢失你的图片metaData
https://stackoverflow.com/questions/67720474
复制相似问题