使用节点缩略图从图像

时间:2018-01-28 21:53:14

标签: javascript node.js azure

我正在尝试使用node-thumbnail从图像生成缩略图,缩略图正在上传到我的容器中的azure存储,但它看起来像原始文件不像缩略图。这是我的代码,首先我上传原始图片,然后阅读它并从中生成缩略图,然后将缩略图上传到容器。我究竟做错了什么?我无法在线找到有关如何执行此操作的资源,请帮忙!

app.post('/upload', function(req, res) {
if (!req.files)
  return res.status(400).send('No files were uploaded.');

// The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
let sampleFile = req.files.sampleFile;
let name = sampleFile.name;
let data = sampleFile.data;
//var options = { contentSettings: { contentType: 'image/jpeg' } }

blobSvc.createBlockBlobFromText('test-container', name, data, function(error, result, response){
    if (error){
        return res.status(500).send(error);
    } else {
        console.log('Uploaded to container');
    }


    var info = blobSvc.getBlobToLocalFile ("test-container", name, name,
        function (error, blockBlob, response) {
            thumb({
                source: name, // could be a filename: dest/path/image.jpg
                destination: './',
                concurrency: 4,
                width: 100
              }, function(files, err){
                if (err) throw err;
                console.log("resized");
                //Delete the downloaded BIG one


                //Upload the thumbnail
                blobSvc.createBlockBlobFromLocalFile("test-container", files[0].dstPath, files[0].dstPath,
                function (error, blockBlob, response) {
                    if (!error) {
                        console.log("thumbnail uploaded: " + name);

                    } else{
                        console.log(error);
                    }

                });
            });
        });

});     

1 个答案:

答案 0 :(得分:1)

这不是Azure存储问题,而是node-thumbnail问题。

如何使用Jimp

var azure = require('azure-storage');
var Jimp = require("jimp");
var path = require('path');

// ...

var info = blobSvc.getBlobToLocalFile("test-container", name, name, function(error, blockBlob, response) {

    if (!error) {

        var dstName = path.parse(name).name + "_thumb" + path.parse(name).ext;

        Jimp.read(name, function(err, image) {

            if (err) throw err;
            image.resize(100, Jimp.AUTO)             // resize
                .quality(60)                         // set JPEG quality
                .write(dstName, function(err, ret) { // save

                    //Upload the thumbnail
                    blobSvc.createBlockBlobFromLocalFile("test-container", dstName, dstName, function(error, blockBlob, response) {
                        if (!error) {
                            console.log("thumbnail uploaded: " + dstName);
                        } else {
                            console.log(error);
                        }

                    });
                });
        });
    }
});