Javascript 解析调整图像大小未正确保存

Javascript 解析调整图像大小未正确保存,javascript,parse-platform,parse-cloud-code,Javascript,Parse Platform,Parse Cloud Code,我试图在解析云代码中按照以下步骤调整图像的大小 基本思想如下:当用户调用afterSave时,检查小配置文件pic是否为null,标准配置文件pic是否为null。如果为true,则从Parse获取标准配置文件pic,读入缓冲区,创建文件,保存文件,然后将文件添加到用户并保存。不幸的是,文件似乎没有正确保存 以下是cloud afterSave函数: Parse.Cloud.afterSave(Parse.User, function(request) { ...

我试图在解析云代码中按照以下步骤调整图像的大小

基本思想如下:当用户调用afterSave时,检查小配置文件pic是否为null,标准配置文件pic是否为null。如果为true,则从Parse获取标准配置文件pic,读入缓冲区,创建文件,保存文件,然后将文件添加到用户并保存。不幸的是,文件似乎没有正确保存

以下是cloud afterSave函数:

    Parse.Cloud.afterSave(Parse.User, function(request) {

    ...

        Parse.Cloud.httpRequest({
        url: request.object.get("profilePicture").url(),
        success: function(response) {
            // The file contents are in response.buffer.

            var image = new Image();
            console.log("Buffer: " + response.buffer );
            console.log("Length " + response.buffer.length);
            image.setData(response.buffer);
            var imgData = image.data();


            // Save the image into a new file.
            var base64 = imgData.toString("base64");
            var scaled = new Parse.File("thumbnail.png", { base64: base64 });
            scaled.save().then(function() {
                request.object.set("profilePictureSmall", scaled);
                request.object.save();
            }, function(error) {
                console.log( "The file either could not be read, or could not be saved to Parse.");
            });
        }
    });
    ...
});
用户对象似乎保存得很好,但保存的图像文件是一个损坏的图像

奇怪的是
console.log(“Length”+response.buffer.Length)将正确的大小输出到控制台

console.log(“Buffer:+response.Buffer”)给出输出:
�巴布亚新几内亚


知道这里发生了什么吗?

问题是
setData()
是一个异步调用,您需要等待它完成后再执行下一位操作

下面是一个片段:

Parse.Cloud.httpRequest({
    url: request.object.get("profilePicture").url()
}).then(function(response) {
    var image = new Image();
    return image.setData(response.buffer);
}).then(function(image) {
    // make it fit in 100x100
    var width = 100, height = 100;
    if (image.width() > image.height()) {
        // work out scaled height
        height = image.height() * (100/image.width());
    } else {
        // work out scaled width
        width = image.width() * (100/image.height());
    }
    console.log("..scale to " + width + "x" + height);
    return image.scale({
        width: width,
        height: height
    });
}).then(function(scaledImage) {
    // get the image data in a Buffer
    return scaledImage.data();
}).then(function(buffer) {
    // save the image to a new file
    console.log("..saving file");
    var base64 = buffer.toString("base64");
    var name = "Thumbnail.png";
    var thumbnail = new Parse.File(name, { base64: base64 });
    return thumbnail.save();
}).then(function(thumbnail) {
    // attach the image file to the original object
    console.log("..attaching file");
    request.object.set("profilePictureSmall", thumbnail);
    return request.object.save();
}) /* further chaining here for after-save */;

基本上,您必须将您的承诺链接在一起才能完成上一步。

问题在于
setData()
是一个异步调用,您需要等待它完成后再执行下一步

下面是一个片段:

Parse.Cloud.httpRequest({
    url: request.object.get("profilePicture").url()
}).then(function(response) {
    var image = new Image();
    return image.setData(response.buffer);
}).then(function(image) {
    // make it fit in 100x100
    var width = 100, height = 100;
    if (image.width() > image.height()) {
        // work out scaled height
        height = image.height() * (100/image.width());
    } else {
        // work out scaled width
        width = image.width() * (100/image.height());
    }
    console.log("..scale to " + width + "x" + height);
    return image.scale({
        width: width,
        height: height
    });
}).then(function(scaledImage) {
    // get the image data in a Buffer
    return scaledImage.data();
}).then(function(buffer) {
    // save the image to a new file
    console.log("..saving file");
    var base64 = buffer.toString("base64");
    var name = "Thumbnail.png";
    var thumbnail = new Parse.File(name, { base64: base64 });
    return thumbnail.save();
}).then(function(thumbnail) {
    // attach the image file to the original object
    console.log("..attaching file");
    request.object.set("profilePictureSmall", thumbnail);
    return request.object.save();
}) /* further chaining here for after-save */;

基本上,您必须将您的承诺连成一个整体,才能完成上一步。

谢谢。工作完美!但不要忘记var Image=require(“解析图像”);谢谢工作完美!但不要忘记var Image=require(“解析图像”);