如何在 Node js 中调整图像大小 [英] How to resize image in Node js
本文介绍了如何在 Node js 中调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在将图像上传到 s3(亚马逊)之前,我需要调整它们的大小.我试过这个功能,但它不起作用.
I need to resize my images before I upload them to s3 (amazon). I tried this function but it's not working.
这里是上传图片的函数.
Here is the function that uploads the image.
我的文件名是:beach_life-normal.jpg
我试过这个新代码,但还是不行!!!
这是我的代码:
var AWS = require('aws-sdk'),
fs = require('fs');
var express = require("express");
var app = express();
var im = require('imagemagick');
// For dev purposes only
AWS.config.update({ accessKeyId: '', secretAccessKey: '' });
// Read in the file, convert it to base64, store to S3
var fileStream = fs.createReadStream('beach_life-normal.jpg');
fileStream.on('error', function (err) {
if (err) { throw err; }
});
fileStream.on('open', function () {
var s3 = new AWS.S3();
im.resize({
srcPath: 'beach_life-normal.jpg',
dstPath: 'beach_life-normal-small.jpg',
width: 256
});
s3.putObject({
Bucket: 'adinoauploadefile',
Key: 'beach_life-normal.jpg',
Body: fileStream
}, function (err) {
if (err) { throw err; }
});
});
推荐答案
使用此代码.
im.convert(['kittens.jpg', '-resize', '25x120', 'kittens-small.jpg'],
function(err, stdout){
if (err) throw err;
console.log('stdout:', stdout);
});
代替 kittens.jpg ,你可以给出图像的路径,或者在你的情况下你可以使用 fileStream 并且 kittens-small.jpg 将是调整大小的图像名称的名称.
In place of kittens.jpg ,you can give path of image or in your case you can use fileStream and kittens-small.jpg will be name of resized image name.
这篇关于如何在 Node js 中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文