【发布时间】:2014-08-03 20:46:08
【问题描述】:
我有这种方法可以在将图像保存到 parse.com 数据库之前抓取图像并将其缩小。
看看代码:
var Image = require("parse-image"); // module
Parse.Cloud.beforeSave("Garments", function(request, response) {
Parse.Cloud.httpRequest({
url: request.object.get("image").url()
}).then(function(response) {
var image = new Image();
return image.setData(response.buffer);
}).then(function(image) {
// Resize the image.
return image.scale({
width: 300,
height: 450
});
}).then(function(image) {
// Make sure it's a JPEG to save disk space and bandwidth.
return image.setFormat("JPEG");
}).then(function(image) {
// Get the image data in a Buffer.
return image.data();
}).then(function(buffer) {
// Save the image into a new file.
var base64 = buffer.toString("base64");
var cropped = new Parse.File("image.jpg", { base64: base64 });
return cropped.save();
}).then(function(cropped) {
// Attach the image file to the original object.
request.object.set("image", cropped);
}).then(function(result) {
response.success();
}, function(error) {
response.error(error);
});
});
问题:
是否可以以上 5 张图片进行操作?
我共有 6 个图像列。
图像,图像2,图像3,图像4,图像5,图像6
如果没有填充“图像”列,则永远不会存在行。其他图像是可选的。因此,在缩放时,我需要继续缩放“图像”,如果 image2、image3、image4、image5 和 image6 不存在,请不要抛出任何错误。如果它们确实存在,那么也将它们缩小。
我坐在这里挠头,试图想出一种有效的方法来编写代码。如果 javascript 专家能提出一些建议,我将不胜感激。
我觉得我再重复几次这段代码根本没有效率。
感谢您的宝贵时间
【问题讨论】:
标签: javascript json xmlhttprequest parse-platform getjson