【问题标题】:How do I scale multiple images in javascript?如何在javascript中缩放多个图像?
【发布时间】: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


    【解决方案1】:

    将大部分代码转换为返回最终承诺的函数并使用Parse.Promise.when() 等待一组承诺完成,这里有一些帮助您入门:

    var imagePromises = [];
    
    var garment = request.object;
    
    // you said "image" is always populated, so always add it
    imagePromises.push(createImagePromise(garment, "image", garment.get("image").url()));
    
    // now conditionally add the other promises, using a loop to further reduce repeated code
    for (var i = 2; i < 7; i++) {
        var imageColumn = "image" + i;
        if (garment.get(imageColumn) && garment.get(imageColumn).url()) {
            imagePromises.push(createImagePromise(garment, imageColumn, garment.get(imageColumn).url()));
        }
    }
    
    // now we have all the promises, wait for them all to finish before we're done
    Parse.Promise.when(imagePromises).then(function () {
        response.success();
    }, function (error) {
        response.error(error);
    });
    

    唯一的最后一部分是创建createImagePromise() 函数。

    function createImagePromise(garment, imageColumn, url) {
        // we want to return the promise
        return Parse.Cloud.httpRequest({
            url: url
        }).then(function (response) {
            // ... etc ...
        }).then(function (cropped) {
            // Attach the image file to the original object.
            garment.set(imageColumn, cropped);
        });
    }
    

    注意:

    limit 允许运行多长时间,beforeSave 在终止之前只有 3 秒可以运行,这可能不足以处理 6 张图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多