gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:37:22 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com