繁体   English   中英

使用node-thumbnail从图像生成缩略图

[英]Using node-thumbnail to generate thumbnail from image

我正在尝试使用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);
                    }

                });
            });
        });

});     

这实际上不是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);
                        }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM