async function compressPng(tempFilePath: string, optimizedFilePath: string) { return new Promise((resolve, reject) => { gm(tempFilePath) .strip() .resize(config.IMAGE_COMPRESSED_WIDTH, config.IMAGE_COMPRESSED_HEIGHT, '>') .quality(5) .interlace('Plane') .colorspace('sRGB') .write(optimizedFilePath, (err): void => { if (err) reject(err); else resolve() }) }) }
//路由 router.post('/uploadImg', upload.single('img'), async(ctx, next) => { // 图片压缩 let filename = ctx.req.file.filename; gm(process.cwd() + "/public/test/" + filename) .resize(1000, 1000, '>') //设置压缩后的w/h .setFormat('JPEG') .quality(70) //设置压缩质量: 0-100 .strip() .autoOrient() .write(process.cwd() + "/public/test/" + filename, function(err) { console.log("压缩失败", err) }) ctx.body = '/test/' + filename //返回文件名 })
function compressPng(tempFilePath, optimizedFilePath) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { gm(tempFilePath) .strip() .resize(config.IMAGE_COMPRESSED_WIDTH, config.IMAGE_COMPRESSED_HEIGHT, '>') .quality(5) .interlace('Plane') .colorspace('sRGB') .write(optimizedFilePath, (err) => { if (err) reject(err); else resolve(); }); }); }); }
function gmCreator(asset, bucket, resize_options) { // function to create a GM process return new Promise((resolve, reject) => { try { const func = gm(s3.getObject({ Bucket: bucket, Key: asset }).createReadStream()); func.options({ timeout: resize_options.timeout || DEFAULT_TIME_OUT }); if (resize_options.quality) func.quality(resize_options.quality); if (resize_options.resize && resize_options.crop) func.resize(resize_options.resize.width, resize_options.resize.height, '^'); else if (resize_options.resize) func.resize(resize_options.resize.width, resize_options.resize.height); if (resize_options.filter) func.filter(resize_options.filter); if (resize_options.strip) func.strip(); if (resize_options.gravity) func.gravity(resize_options.gravity); if (resize_options.crop) func.crop(resize_options.crop.width, resize_options.crop.height, 0, 0); if (resize_options.max) func.resize(resize_options.max); if (resize_options.compress) func.compress(resize_options.compress); if (resize_options.blur) { func.blur(resize_options.blur[0], resize_options.blur[1]); } return resolve(func); } catch (err) { return reject({ statusCode: 500, body: `{"message":"${err.message}"}` }); } }); }
.quality(70) //设置压缩质量: 0-100 .strip() .autoOrient()
/** * For processing the image * * @param {Object} options * @param {String} options.srcFile * @param {Number} options.width * @param {Number} options.height * @param {String} options.destFile */ const ProcessImage = options => { return new Promise((resolve, reject) => { let { srcFile, width, height, destFile } = options gm(srcFile) .resize(width, height) .gravity('center') .quality(100) .write(destFile, err => err ? reject(err) : resolve('Processed!!') ) }) }
function compressJpeg(tempFilePath: string, optimizedFilePath: string) { return new Promise((resolve, reject) => { gm(tempFilePath) .strip() .resize(config.IMAGE_COMPRESSED_WIDTH, config.IMAGE_COMPRESSED_HEIGHT, '>') .quality(70) .interlace('Plane') .colorspace('sRGB') .write(optimizedFilePath, (err): void => { if (err) reject(err); else resolve() }) }) }
function compressJpeg(tempFilePath, optimizedFilePath) { return new Promise((resolve, reject) => { gm(tempFilePath) .strip() .resize(config.IMAGE_COMPRESSED_WIDTH, config.IMAGE_COMPRESSED_HEIGHT, '>') .quality(70) .interlace('Plane') .colorspace('sRGB') .write(optimizedFilePath, (err) => { if (err) reject(err); else resolve(); }); }); }