How can I stop ImageMagick failing due to SIGKILL? - node.js

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?

Related

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");
});

Pipe image output from Graphics Magic to response without base64 encoding

I have a Node server where, instead of storing cropped images, I want to crop them in response to an AJAX call, and send them to the client that way. I'm storing the information of what to crop and how to crop it in cookies and the body. On the server I crop it, encode it in base64, and send it back the user. Here is what my code looks like
res.set('Content-Type', 'image/jpeg');
gm(request(body.URL))
.crop(req.cookies[name+"Width"],req.cookies[name+"Height"],req.cookies[name+"X"],req.cookies[name+"Y"])
.stream(function streamOut (err, stdout, stderr) {
if (err) return next(err);
stdout.pipe(base64encode()).pipe(res);
stdout.on('error', next);
});
This works, but I don't like it. I was only able to get this to work by encoding it in base64, but on the client side this is seems slow to decode this to an image. I would rather just send an image directly, but I was unable to get this to work. Pipping the image without decoding it resulted in a gibberish response from the server. Is there a better way to do this? Or does the unsaved image have to be encoded in order to send?
I have no idea how you write node.js - it all looks like a bunch of dots and parentheses to me, but using what I know about the GraphicsMagick command line, I tried this and it does what I think you want - which is to write a JPEG encoded result on stdout:
// Send header "Content-type: image/jpeg"...
var gm = require('gm');
var input = 'input.jpg';
gm(input).resize(350).toBuffer('JPG',function (err, buffer) {
if (err) return handle(err);
process.stdout.write(buffer);
})
Update
Have you considered ruling out the AJAX aspects and just using a static src for your image that refers to the node script? As I said, I do not know node and Javascript but if I generate a thumbnail via a PHP script, I would add this into the HTML
<img src="/php/thumb.php"/>
So that just invokes a PHP script to generate an image. If you remove the /php/thumb.php and replace that with however you have named the node script I suggested above, it should tell you whether the problem is the AJAX or the GraphicsMagick aspects...

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 NPM sharp. cannot write .gif to file

I am currently making an image resizer for a website (it resizes an image based on the the width of the container it is in).
I can successfully save and server jpeg's and png's however i cannot seem to save .gif's to a file.
This code works for the jpg's and png's
sharp(imgPath)
.resize(containWidth, Null)
.withoutEnlargement()
.toFile(modImgPath, function(err) {
if(err){console.log(err)}
});
I can get the gif's to display in the browser (With the code below) however i cannot save them to file.
sharp(imgPath)
.resize(modImgPath, null)
.withoutEnlargment()
.toBuffer(function(err, outputBuffer) {
if (err) {console.log(err)}
}
Iv looked into how to save image buffers to file however nothing seems to work.
Currently the gif output seems to be unsupported in sharp.
https://github.com/lovell/sharp/issues/162
https://github.com/lovell/sharp/issues/245
One solution, though I haven't tried to see if it works, is to change the output to another format. This will save the first frame of your gif.

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