0

我想要做的是从谷歌下载一个图像到我的项目文件夹中的系统存储库中/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);
        }   

如果我删除了我评论为 CODE REMOVED - ERROR IS Coming HERE 的代码,则图像将从谷歌下载。如果我调用imageresize函数,则/download目录正在创建,但我在目录中找不到任何图像。

即,如果我分别调用从 google 下载和 resizeimage 函数,即先从 google 调用图像下载,然后再调用 imageresize,那么它们工作正常,即我可以在两个目录/download中找到图像。/thumbnail但是如果我在谷歌下载后调用 resizeimage 函数,那么我在两个存储库中都找不到任何图像。

4

1 回答 1

1

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);
});
于 2019-06-26T04:45:56.653 回答