2017-08-22 4 views
-1

Supposons que j'ai une tableComment écrire Parse code Cloud pour faire Thumbnails

---------Image------------- 

imageFile (File) | thumbnail (File) | Post (a pointer to Post) 

Toute idée comment écrire un code de nuage pour prendre une version plus petite de cette image dans une autre colonne?

Par exemple, si un utilisateur a chargé un (2000x3500 px), parse enregistrer dans la colonne imageFile et enregistrer sa vignette dans l'autre colonne

grâce

+0

Peut-être https://github.com/lovell/sharp ou une bibliothèque nodejs –

+0

bonne bibliothèque, mais comment l'utiliser avec parse code nuage? – Hatim

+0

Probablement vous devrez apprendre nodejs, il suffit d'utiliser npm installer la bibliothèque et l'utiliser dans votre code de clode avec require (bibliothèque) –

Répondre

1

est ici la solution actuelle, je suis en utilisant (script original a été publié par Parse.com il y a quelque temps et supprimé après le transfert de propriété à la communauté):

exemple d'utilisation dans main.js:

var resizeImageKey = require('cloud/resize-image-key.js'), 
    THUMBNAIL_SIZE = 100, 
    Image = require("parse-image"); 


Parse.Cloud.afterSave("TableName", function(request) { 

    return resizeImageKey({ 
       object: request.object, // ParseObject in which the thumbnail will be saved 
       url: objectImage.url(), // Full size image URL 
       toKey: "thumbnail", // thumbnail's attribute key 
       width: THUMBNAIL_SIZE, // width 
       crop: false // resize or crop ? 
      }); 
}); 

Redimensionner image-key.js

var Image = require("parse-image"); 

/* 
    Original: https://github.com/ParsePlatform/Anyimg/blob/master/parse/cloud/resize-image-key.js 
    Resizes an image from one Parse Object key containing 
    a Parse File to a file object at a new key with a target width. 
    If the image is smaller than the target width, then it is simply 
    copied unaltered. 

    object: Parse Object 
    url: URL of the Parse File 
    toKey: Key to contain the target Parse File 
    width: Target width 
    crop: Center crop the square 
*/ 
module.exports = function(options) { 
    var format, originalHeight, originalWidth, newHeight, newWidth; 

    // First get the image data 
    return Parse.Cloud.httpRequest({ 
     //url: options.object.get(options.fromKey).url() 
     url: options.url 
    }).then(function(response) { 
     var image = new Image(); 
     return image.setData(response.buffer); 
    }).then(function(image) { 
     // set some metadata that will be on the object 
     format = image.format(); 
     originalHeight = image.height(); 
     originalWidth = image.width(); 

     if (image.width() <= options.width) { 
      // No need to resize 
      return new Parse.Promise.as(image); 
     } else { 
      var newWidth = options.width; 
      var newHeight = options.width * image.height()/image.width(); 

      // If we're cropping to a square, then we need to adjust height and 
      // width so that the greater length of the two fits the square 
      if (options.crop && (newWidth > newHeight)) { 
       var newHeight = options.width; 
       var newWidth = newHeight * image.width()/image.height(); 
      } 

      // resize down to normal width size 
      return image.scale({ 
       width: newWidth, 
       height: newHeight 
      }); 
     } 
    }).then(function(image) { 
     if (options.crop) { 
      var left = 0; 
      var top = 0; 

      // Center crop 
      if (image.width() > image.height()) { 
       var left = (image.width() - image.height())/2; 
      } else { 
       var top = (image.height() - image.width())/2; 
      } 

      return image.crop({ 
       left: left, 
       top: top, 
       width: options.width, 
       height: options.width 
      }); 
     } else { 
      return Parse.Promise.as(image); 
     } 
    }).then(function(image) { 
     newHeight = image.height(); 
     newWidth = image.width(); 
     // 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 scaled = new Parse.File("thumbnail_" + options.object.get("nombre") + "." + format, { 
     var scaled = new Parse.File("thumbnail." + format, { 
      base64: base64 
     }); 
     return scaled.save(); 
    }).then(function(image) { 
     // Set metadata on the image object 
     options.object.set(options.toKey, image); 
     //return options.object.save(); 
     options.object.set("thumbnail_width", newWidth); 
     options.object.set("thumbnail_height", newHeight); 
    }); 
}; 
+0

Je ne sais pas comment vous remercier .... merci beaucoup – Hatim

+0

Vous êtes les bienvenus mais comme J'ai dit que ce n'était pas mon code :) (Je ne sais pas pourquoi Parse a supprimé ce repo spécifique) – nathan