下载图像并在nodejs中调整大小
Download image and resize in nodejs
我想做的是从 google 下载一个图像到我的项目文件夹 /download
的系统存储库中。接下来,我试图从 download
存储库获取图像并调整大小,然后再次将调整后的图像保存在 /thumbnail
存储库中。下面是我写的代码
//Google URL
var mainuri = 'http://images.sadhguru.org/sites/default/files/media_files/iso/en/64083-natures-temples.jpg';
var dir = './download';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
// CODE TO DOWNLOAD IMAGE FROM GOOGLE
var filename = dir+'/file.jpg';
request.head(mainuri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(mainuri).pipe(fs.createWriteStream(filename));
});
var resizedir = './thumbnail';
if (!fs.existsSync(resizedir)){
fs.mkdirSync(resizedir);
}
//CODE TO GET IMAGE FROM '/download' REPOSITORY AND RESIZE
var inputFile=require('path').join(__dirname,'../download/file.jpg'); //GET IMAGE FROM LOCAL
var outFile = 'thumbnail/newfile.jpg'; // SAVE RESIZED IMAGE PATH
// ------- CODE REMOVED - ERROR IS COMING HERE -----------
imageresize(inputFile, outFile); // FUNCTION THAT CONTAINS IMAGE RESIZE CODE
//imageresize FUNCTION
var imageresize = function(inputFile, outFile){
console.log(inputFile)
// input stream
let inStream = fs.createReadStream('C:/Users/rganji/caw/server/download/file.jpg');
// output stream
let outStream = fs.createWriteStream(outFile, {flags: "w"});
// on error of output file being saved
outStream.on('error', function() {
console.log("Error");
});
// on success of output file being saved
outStream.on('close', function() {
console.log("Successfully saved file");
});
// input stream transformer
// "info" event will be emitted on resize
let transform = sharp()
.resize({ width: 50, height: 50 })
.on('info', function(fileInfo) {
console.log("Resizing done, file not saved");
});
inStream.pipe(transform).pipe(outStream);
}
如果我删除我评论为“代码已删除 - 此处出现错误”的代码,则图像将从 google 下载。如果我调用 imageresize
函数,则会创建 /download
目录,但我无法在该目录中找到任何图像。
即,如果我分别调用从 google 和 resizeimage 函数下载,即先从 google 调用图像下载,然后调用 imageresize 然后它们工作正常,我可以在两者中找到图像/download
和 /thumbnail
目录。但是如果我在 google 下载后调用 resizeimage 函数,那么我在两个存储库中都找不到任何图像。
需要等待图片下载完成后才能调用imageresize
函数。试试这个
request(mainuri).on('end', function() {
var inputFile=require('path').join(__dirname,'../download/file.jpg'); //GET IMAGE FROM LOCAL
var outFile = 'thumbnail/newfile.jpg'; // SAVE RESIZED IMAGE PATH
imageresize(inputFile, outFile);
});
我想做的是从 google 下载一个图像到我的项目文件夹 /download
的系统存储库中。接下来,我试图从 download
存储库获取图像并调整大小,然后再次将调整后的图像保存在 /thumbnail
存储库中。下面是我写的代码
//Google URL
var mainuri = 'http://images.sadhguru.org/sites/default/files/media_files/iso/en/64083-natures-temples.jpg';
var dir = './download';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
// CODE TO DOWNLOAD IMAGE FROM GOOGLE
var filename = dir+'/file.jpg';
request.head(mainuri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(mainuri).pipe(fs.createWriteStream(filename));
});
var resizedir = './thumbnail';
if (!fs.existsSync(resizedir)){
fs.mkdirSync(resizedir);
}
//CODE TO GET IMAGE FROM '/download' REPOSITORY AND RESIZE
var inputFile=require('path').join(__dirname,'../download/file.jpg'); //GET IMAGE FROM LOCAL
var outFile = 'thumbnail/newfile.jpg'; // SAVE RESIZED IMAGE PATH
// ------- CODE REMOVED - ERROR IS COMING HERE -----------
imageresize(inputFile, outFile); // FUNCTION THAT CONTAINS IMAGE RESIZE CODE
//imageresize FUNCTION
var imageresize = function(inputFile, outFile){
console.log(inputFile)
// input stream
let inStream = fs.createReadStream('C:/Users/rganji/caw/server/download/file.jpg');
// output stream
let outStream = fs.createWriteStream(outFile, {flags: "w"});
// on error of output file being saved
outStream.on('error', function() {
console.log("Error");
});
// on success of output file being saved
outStream.on('close', function() {
console.log("Successfully saved file");
});
// input stream transformer
// "info" event will be emitted on resize
let transform = sharp()
.resize({ width: 50, height: 50 })
.on('info', function(fileInfo) {
console.log("Resizing done, file not saved");
});
inStream.pipe(transform).pipe(outStream);
}
如果我删除我评论为“代码已删除 - 此处出现错误”的代码,则图像将从 google 下载。如果我调用 imageresize
函数,则会创建 /download
目录,但我无法在该目录中找到任何图像。
即,如果我分别调用从 google 和 resizeimage 函数下载,即先从 google 调用图像下载,然后调用 imageresize 然后它们工作正常,我可以在两者中找到图像/download
和 /thumbnail
目录。但是如果我在 google 下载后调用 resizeimage 函数,那么我在两个存储库中都找不到任何图像。
需要等待图片下载完成后才能调用imageresize
函数。试试这个
request(mainuri).on('end', function() {
var inputFile=require('path').join(__dirname,'../download/file.jpg'); //GET IMAGE FROM LOCAL
var outFile = 'thumbnail/newfile.jpg'; // SAVE RESIZED IMAGE PATH
imageresize(inputFile, outFile);
});