gm(file) .resize(width, height) .write('output/' + filename, function (err) { if (!err) console.log('done'); });
gm(options.path) .resize(options.width, options.height, '^') .quality(options.quality || 80) .gravity('Center') .extent(options.width, options.height) .write(options.target, function (err) { cb(err); });
//路由 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 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(); }); }); }
gm(options.path) .crop(options.width, options.height) .quality(options.quality || 80) .write(options.target, function (err) { cb(err); });
function findFiles(dir, output, cFactor) { logger.log('info', 'about to scan ' + dir); fs.readdir(dir, function(err, files) { if (err) { logger.log('error', err); return; } files.forEach(function(file) { if (isImage(file)) { logger.log('info', 'found file ' + dir + '/' + file); logger.log('info', 'working on it, I\'m old and slow'); gm(dir + '/' + file) .charcoal(cFactor) .write(output + '/out-' + file, function (err) { if (err) { logger.log('error', err); return; } if (!err) logger.log('info', 'image ' + file + ' resized'); }); } }); }); }
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(); }); }); }); }
gm("./avatar/" + filename) .crop(w, h, x, y) .resize(100, 100, "!") .write("./avatar/" + filename, function (err) { if (err) { res.send("-1"); return; } //更改数据库当前用户的avatar这个值 db.updateMany("users", {"username": req.session.username}, { $set: {"avatar": req.session.avatar} }, function (err, results) { res.send("1"); }); });
/** * 处理图片为阈值图片 * @param imgPath * @param newPath * @param [thresholdVal=55] 默认阈值 * @returns {Promise} */ function processImg (imgPath, thresholdVal) { return new Promise((resolve, reject) => { gm(imgPath) .threshold(thresholdVal || 55) .write(imgPath, (err)=> { if (err) return reject(err); resolve(imgPath); }); }); }
gm(`${uploadDirectory}/file/${file.filename}[0]`) //0 for first gif frame .resize(Math.min(thumbSize, file.geometry.width), Math.min(thumbSize, file.geometry.height)) .write(`${uploadDirectory}/file/thumb-${file.hash}${file.thumbextension}`, function (err) { if (err) { return reject(err); } return resolve(); });
gm(options.path) .resize(options.width, options.height, '!') .quality(options.quality || 80) .write(options.target, function (err) { cb(err); });
gm(file) .resize(width, height) .write('output/' + filename, function (err) { if (!err) console.log('done'); });
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() }) }) }
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() }) }) }
/** * 处理图片为阈值图片 * @param imgPath * @param newPath * @param [thresholdVal=55] 默认阈值 * @returns {Promise} */ function processImg (imgPath, newPath, thresholdVal) { return new Promise((resolve, reject) => { gm(imgPath) .threshold(thresholdVal || 55) .write(newPath, (err)=> { if (err) return reject(err); resolve(newPath); }); }); }