You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is not really an issue with Parse-Server, rather a cry for help with using Imagemagick instead of Parse-Image in my cloud code functions. Can anyone point me in the right direction to achieve the same as expressed in my old cloud code:
Parse.Cloud.beforeSave("TheClass", function(request, response) {
var theObject= request.object;
Parse.Cloud.httpRequest({
url: theObject.get("img").url()
}).then(function(response) {
var image = new Image();
return image.setData(response.buffer);
}).then(function(image) {
var currentWidth = image.width();
var currentHeight = image.height();
var ratio = currentHeight / currentWidth;
if (currentWidth > 640) {
return image.scale({
width: 640,
height: 640 * ratio
});
} else {
return image;
}
}).then(function(image) {
return image.setFormat("JPEG");
}).then(function(image) {
return image.data();
}).then(function(buffer) {
var base64 = buffer.toString("base64");
var cropped = new Parse.File("thumb.jpeg", { base64: base64 });
return cropped.save();
}).then(function(cropped) {
theObject.set("thumb", cropped);
}).then(function(result) {
response.success();
}, function(error) {
response.error(error);
});
});
I tried console.logging the url to my uploaded ParseFile, console.log(theObject.get("img").url());. The url was http, not https. Then i ran this command in the heroku bash identify -verbose <URL-TO-FILE>, and there the identify seemed to work fine at least.
identify -verbose http://<app>/parse/files/sv78uRNzD4RPUKW9
Image: 5ef1d74591c83c189609957ee1f18860_portal.PNG
Base filename: 5ef1d74591c83c189609957ee1f18860_portal.PNG
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 2438x1400+0+0
Resolution: 75.59x75.59
...
...
and so on
I used jimp to replace parse-image when I migrated our app. It is written entirely in JavaScript for Node so no external dependencies needed (no special install on heroku or windows). It probably has worse performance than imagemagick, but I have yet to run any benchmarks. We are scaling a lot of images in our app however and so far it looks promising.
@simonbengtsson That looks lightweight and good. Are you using jimp to e.g. save thumbnails via cloud code? I am having trouble getting that work. With the code provided below, everything seems OK, and the thumb is saved on the "Message" object, but viewing the thumb in the browser with the file URL is not working.
var Jimp = require("jimp");
Parse.Cloud.beforeSave("Message", function(request, response) {
var obj = request.object;
if (!obj .get("img")) {
response.success();
return;
}
Jimp.read(obj.get("img").url())
.then(function(image) {
var currentWidth = image.bitmap.width;
var currentHeight = image.bitmap.height;
var ratio = currentHeight / currentWidth;
if (currentWidth > 640) {
return image.resize(640, 640 * ratio)
} else {
return image;
}
})
.then(function(image) {
console.log("### 1");
var base64 = image.hash();
var cropped = new Parse.File("thumb.jpeg", { base64: base64 });
return cropped.save();
})
.then(function(cropped) {
console.log("### 2");
obj.set("thumb", cropped);
})
.then(function(result) {
console.log("### 3");
response.success();
}, function(error) {
console.log("### 4 ERROR: " + error);
response.error(error);
})
.catch(function (err) {
console.log("### 5 ERROR: " + err);
});
});
Yes we are generating thumbnails in cloud code using jimp. Your issue is probably that the string from image.hash() cannot be used as data for parse files. Try something like this instead:
@flovilmart I tried your parse-image module on Heroku for a simple thumbnail generation. I did not need to change a single line of code, only added "parse-image": "~0.1.1" in my package.json dependencies, and it works fine so far.
This all seems to be working great for me using jimp until the part where I try to set it to the object in the beforeSave hook. I see the images showing up in S3 and it comes back correctly. I have other attributes that are getting set correctly as well. It is just the 'pictureThumbnail' that isn't. For some reason it just seems as if it's not updating the request.object. @rickkrauland Was there something you had to do different then what is documented in @nitrag 's example?
Activity
oyvindvol commentedon Feb 25, 2016
After adding parse-image to my package.json the module is found.
But now I get this error:
flovilmart commentedon Feb 25, 2016
That seems to be related to imagemagick, http://stackoverflow.com/questions/30309633/after-replacing-rmagick-with-minimagick-gets-identify-im6-no-decode-delegate
http://stackoverflow.com/questions/24685803/rmagick-image-type-detection-failing-on-heroku
oyvindvol commentedon Feb 25, 2016
Ok, thanks a lot so far @flovilmart!
I tried console.logging the url to my uploaded ParseFile,
console.log(theObject.get("img").url());. The url was http, not https. Then i ran this command in the heroku bashidentify -verbose <URL-TO-FILE>, and there the identify seemed to work fine at least.flovilmart commentedon Feb 25, 2016
Is your response.buffer set?
oyvindvol commentedon Feb 25, 2016
Yes
flovilmart commentedon Feb 25, 2016
Uhm... that's very very odd indeed. Did you try on you local machine, see if it's heroku related or something else?
flovilmart commentedon Feb 25, 2016
You should probably open an issue on parse-image instead of there. Closing for now.
oyvindvol commentedon Feb 25, 2016
Well, initially this issue was about using the recommended Imagemagick in parse server cloud code. I am still interested in examples on that though..
flovilmart commentedon Feb 25, 2016
OK, can you post an issue on parse-image I'll re-open in that case.
flovilmart commentedon Feb 25, 2016
http://stackoverflow.com/questions/30309633/after-replacing-rmagick-with-minimagick-gets-identify-im6-no-decode-delegate
simonbengtsson commentedon Feb 25, 2016
I used jimp to replace
parse-imagewhen I migrated our app. It is written entirely in JavaScript for Node so no external dependencies needed (no special install on heroku or windows). It probably has worse performance than imagemagick, but I have yet to run any benchmarks. We are scaling a lot of images in our app however and so far it looks promising.oyvindvol commentedon Feb 26, 2016
@simonbengtsson That looks lightweight and good. Are you using jimp to e.g. save thumbnails via cloud code? I am having trouble getting that work. With the code provided below, everything seems OK, and the thumb is saved on the "Message" object, but viewing the thumb in the browser with the file URL is not working.
simonbengtsson commentedon Feb 26, 2016
Yes we are generating thumbnails in cloud code using jimp. Your issue is probably that the string from
image.hash()cannot be used as data for parse files. Try something like this instead:oyvindvol commentedon Feb 29, 2016
@simonbengtsson Thanks! :) That worked..
nitrag commentedon Mar 3, 2016
Since it took me several hours to get this correct I'm posting here so the next guy has an easier time:
6thfdwp commentedon May 14, 2016
Thanks for sharing this pure js image module, here is the benchmarks I found for jimp in comparison with other libraries.
lovell/sharp#275 (comment)
It seems enough for common image processing cases.
majidhassan commentedon May 29, 2016
@flovilmart I tried your parse-image module on Heroku for a simple thumbnail generation. I did not need to change a single line of code, only added "parse-image": "~0.1.1" in my package.json dependencies, and it works fine so far.
Great job!
rickkrauland commentedon Aug 7, 2016
@nitrag Thanks for your post. Fwiw, I had to add a buffer.toString conversion before I could get everything to work.
parkej60 commentedon Sep 23, 2016
This all seems to be working great for me using jimp until the part where I try to set it to the object in the beforeSave hook. I see the images showing up in S3 and it comes back correctly. I have other attributes that are getting set correctly as well. It is just the 'pictureThumbnail' that isn't. For some reason it just seems as if it's not updating the request.object. @rickkrauland Was there something you had to do different then what is documented in @nitrag 's example?
parkej60 commentedon Sep 23, 2016
So... turns out that response.success doesn't want a string sent along with it. I removed "thumbnail saved" from response.success() and it worked.