javascript - 使用 Node 缩略图从图像生成缩略图

标签 javascript node.js azure

我正在尝试使用 Node 缩略图从图像生成缩略图,该缩略图正在上传到我的 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 缩略图 问题。

使用 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);
                        }

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

关于javascript - 使用 Node 缩略图从图像生成缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491738/

相关文章:

javascript - 点击元素跨容器遍历

javascript - jquery 最小和最大长度验证

node.js - 一个 NodeJS 服务器中有两个 Express "app"?

node.js - 在 sequelizejs 中从搜索中排除用户

c# - ASP.NET Core 404 响应中的 Service Fabric 反向代理集成

javascript - 绘图 : x-axis y axis set min and max values

javascript - 如何为grpc(node.js)编写中间件

python-2.7 - 无法在 Azure LAMP shell 服务器上使用 pip 安装 Python 包

azure - 从二头肌模块检索存储帐户访问 key

javascript - "2018-06-29T08:51:00Z"是什么时间格式?你能告诉我我应该在 moment.js 中使用什么格式的字符串来实现这个吗?