NodeJS gm model use to get information of image? - node.js

Any one tell me to how to get image information using NodeJs module 'gm' . Here i used below code for re-size. But if image is smaller than re-size size than does not re-size so, that i want validation for image width and height so that purpose i want to get image information.
gm(main_url_path)
.resize(size, size)
.write(compress_path, function (err){
if(!err){
callback(compress_path);
}else{
console.log("Not Compressed success !!"+err);
callback(null);
}
});

// output all available image properties
gm('/path/to/img.png')
.identify(function (err, data) {
if (!err) console.log(data)
});
Edit
If you want to know basic implementation of NodeJS packages, visit the npm website as it generally provides examples of the essential functions within the package: https://www.npmjs.org/package/gm

Related

How can I stop ImageMagick failing due to SIGKILL?

I'm trying to resize GIF and PNG images using imagemagick.
Sometimes it fails just trying to identify the images:
im.identify(tempFilePath, function(err, features){
if (err) {
console.log(err);
return next();
}
}
So usually the process completes without issue, with both PNG and GIF. But when I upload a specific GIF image (sent to me by user, is a working gif file), I get this error:
{"error":{"timedOut":false,"killed":true,"code":null,"signal":"SIGKILL"}
I can't figure out what this means, seems like it maybe has to do with memory? Is there a way to prevent this from happening?

How to change color image (jpg,jpeg,png) to black and white or grayscale in node js

I used all npm package that support grayscaling but nothing is working properly.
Some of them are working but quality reduced.
there is a good grayscale package named image-grayscale but problem is that when one of the src image (example/image.gpg) file is corrupted then according to promise code stopped to go further.
Below code is the problem.
globby(['./upload/*.*','!./upload/*.ico','!./upload/*.gif', '!./upload/*.txt']).then(function (paths) {
return Promise.all(paths.map(function (e) {
return imageGrayScale(e, {logProgress: 1}) }));
}).then(function (val) {
// if one of the file in directory is corrupted then promise is rejected and my code stooped and i cant do anything further
})
Please tell me how to handle error promise to code go further. or is there any solution like callback.
should i go to any other module or can written own algorithim for grayscale please tell me how to convert color of image to black and white
I have used Jimp before with success so you could look into that.
An example taken from the documentation
Jimp.read("file.png", (err, image) => {
if (err) throw err;
image.greyscale().write("image.png");
});

Composite images in Graphicsmagick

I'm trying to request an image from an API and "paste" it on top of another image. In Photoshop, I would paste the image into a new layer and then merge the layers. I can accomplish this with Graphicsmagick using gm's composite().
gm().command("composite")
.in("path/to/topImg.png")
.in("path/to/bottomImg.png")
.toBuffer('PNG', function(err, buffer) {
if (!err) {return buffer;}
});
However, composite only takes file paths. So let's say I want to get the logo from http://www.google.com. I could save the image, use it in the code above, and then delete it. What I'm looking for is a way to accomplish this without having to save the image to disk first.
You can use URL directly as image path, without downloading and saving it
gm()
.command("composite")
.in("http://someurl...")
.in("http://someurl...")
.toBuffer('PNG', function(err, buffer) {
if (!err) {return buffer;}
});
But GraphicsMagick uses the HTTP support from libxml2, which does not currently support HTTPS. So if you want to download images over HTTPS you will need external program.

node.js File upload configurations

No Matter how much I've searched and changed and toyed over the last 24 hours, I simply can not find the right combination of settings that allows Node.js to upload multiple files.
My setup is quite simple - I have a form interface that posts multipart content (files) to an endpoint.. lets call it: /theendpoint
and this end point is supposed to parse the multiple files. However, during the parsing, there are various events that need to be called once the file is uploaded.
I'm currently using the express.bodyParser({ uploadDir:'/tmp', keepextensions:true, defer:true}); in the app configuration.
Using the following method, I am trying to parse the file, but the problem is
Only 2 files will begin uploading, and will not complete (ie. the progress bar hangs near the end without fully completing).
The other files to be uploaded by the form (item 3+) do not even begin to upload to the server.
It seems to be some sort of asynchronus holdup, however I can't properly interpret the problem. Some of the code used at the upload endpoint are as follows:
// This applies to /theendpoint route. Using Express.
exports.theendpoint = function(req,res){
console.log(req.files);
fs.readfile(uploadPath, function(err,data){
if(err) throw err;
fs.writeFile(newFilePath, data, function(err){
// Series of checks and definitions
// Database connection
// Conditional executions
fs.unlink(req.files.file.path, function(err){
if(err) throw err;
console.log('Deleted');
});
});
});
};
Obviously I've left out some of the code here. If anyone can help - is this structure workable?
You should know that items in the commented section.. ie DB connection etc. are asynchronus tasks.
after
fs.unlink(req.files.file.path, function(err){
if(err) throw err;
console.log('Deleted');
});
add
res.redirect("back");
and it works!

nodejs image manipulation with gm / imagemagick

I'm writing simple app that downloads JPEGs images from Flickr API, and then process them.
All I want to do, is to pick 4 random pixels from each image and save the HEX values.
Is it possible at all? I read a lot of graphicmagick documentation, but can't find a way to do this.
Whats the best way to decode JPEG and get this values? I tried a few plugins but neither can do this by default...
Take care!
https://npmjs.org/package/get-pixels seems nice for that:
var getPixels = require("get-pixels")
getPixels("lena.png", function(err, pixels) {
if(err) {
console.log("Bad image path")
return
}
console.log("got pixels", pixels.shape)
})

Resources