bluebird.promisify(function(input, size, cb) { gm(input).resize(size).toBuffer(function(err, buffer) { if (err) cb(err); else cb(null, buffer); }); })
gm(file) .resize(width, height) .write('output/' + filename, function (err) { if (!err) console.log('done'); });
AV.Cloud.define('imageMagicResize', async request => { return new Promise( (resolve, reject) => { imageMagick('public/leanstorage.png').resize(91, 77).toBuffer('png', (err, buffer) => { if (err) { reject(err) } else { resolve({ imageUrl: 'data:image/png;base64,' + buffer.toString('base64') }) } }) }) })
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 //返回文件名 })
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() }) }) }
/* Generate thumbnail */ router.get('/generatethumb', function(req, res) { exec("gm -help", function (err) { if (err) { res.status(500).send({ error: 'GraphicsMagick is not installed' }) } else { res.setHeader("content-type", "image/png"); gm(path.join(serverRoot, req.query.f)) .resize(req.query.width || '200', req.query.height || '200', '^') .gravity('Center') .crop(req.query.width || '200', req.query.height || '200') .stream('png') .pipe(res); } }); });
this.tempFile().then(tempPath => { const backgroundLayer = gm(this.getBackgroundImagePath()) .resize(this.captchaWidth, this.captchaHeight, '!') if (options.recognitionDifficulty === 'hard') { _.times(parseInt(this.captchaHeight / 6, 10), () => { const x0 = _.random(parseInt(this.captchaWidth / 1.5, 10)) const y0 = _.random(parseInt(this.captchaHeight / 1.5, 10)) backgroundLayer.drawLine( x0, y0, _.random(x0 + 1, this.captchaWidth), _.random(y0 + 1, this.captchaHeight) ).fill(this.randColor()) }) } return backgroundLayer.writeAsync(tempPath) .return(tempPath) })
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"); }); });
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'); });
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(); }); }); }