![](/img/trans.png)
[英]How to resize image with sharp then upload with multer in nodejs
[英]NodeJS + Multer + PkgCloud: resize image before sending it to cloud
我正在尋找一種在將圖像發送到雲(openstack,s3或其他)之前調整圖像大小(由標准html表單提交上傳)的方法。
我正在使用NodeJS和3個插件:Multer(上傳管理),lwip(圖像調整大小)和PkgCloud(傳輸到雲)。
我可以使用multer處理上載,並且可以使用lwip在服務器中寫入一個新的調整大小的文件。
var fs = require('fs'),
multer = require('multer'),
upload = multer({ dest: 'uploads/medias/' }),
pkgcloud = require('pkgcloud'),
client = pkgcloud.storage.createClient({/* some config */});
// "router" is an instance of a route from Express
router.post('/media/:id/image', upload.single('file'), function (req) {
var lwip = require('lwip'),
type = 'png',
npath = req.file.path + '_150x150';
// the file exists and the log shows information
console.log(req.file);
// open image
lwip.open(req.file.path, type, function (err, image) {
image.batch()
.resize(150, 150)
.writeFile(npath, type, function () {
// remove raw file
fs.unlink(req.file.path, function () {
// how do I send the new resized file to the pkgcloud client?
});
});
});
});
但是我丟失了一些東西,而且找不到將這種新文件發送到雲的方法。 我發現了一個插件,可以管理multer和已配置的pkgcloud實例(multer-storage-pkgcloud)之間的傳輸,但是我不知道在將文件發送到雲之前如何調整文件大小。
有誰知道如何處理嗎?
謝謝
我找到了一種方法,只需從文件創建一個流,然后將其與pkgcloud實例進行管道傳輸:
var fstream = fs.createReadStream(path_to_file);
fstream.pipe(client.upload({
container: 'container-id',
remote: 'remote-name'
});
此代碼中缺少:所有成功和錯誤回調。 但想法就在這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.