No file produced when resizing image with imagemagick on node - node.js

So my issue is basically what the title says. When I run resize on a set of images i get no error but I also get no images in the final directory. I made the permissions on the directory 777 just in case that was the issue. I can use imagemagick to read the metadata from the images but it won't resize them. Can you find a simple, or huge, mistake I've made?
var im = require('imagemagick');
im.resize({
srcPath: srcPath,
destPath: destPath,
width: 256,
height: 256
}, function(err, stdout, stderr) {
if (err) {
console.log(err);
} else {
console.log("Image Resized");
}
});
All I ever get out is "Image Resized" out on the console.
Thanks in advance for any help and sorry if this is vague or I missed something.

I feel like an idiot. The problem is the parameter is 'dstPath' not 'destPath'

Related

How to setting dpi for nodejs html-pdf

I'm using html-pdf to generate dynamic pdf file with html.
But pdf file is ugly, I think the reason is dpi not good.
I tried setting:
var options = {
dpi: 96,
};
pdf.create(htmlTemplate, options).toStream(function(err, stream){
if (err) {
return res.end(err.stack)
}
res.setHeader('Content-type', 'application/pdf');
stream.pipe(res);
});
But no lucky. It still not work. I changed the value of dpi many times but pdf file looks the same.
Anyone can help me? Thank you!

Meteor.JS CollectionFS Video to Image Thumbnails (Graphics Magick)

I am working on one Meteor App where I am using CollectionFS to upload Files.
I am able to upload and generate thumbnails for Images.
But my Issue is : How should I create thumbnails for Videos?
I can see that it is possible via command line: https://superuser.com/questions/599348/can-imagemagick-make-thumbnails-from-video
But how can I apply this to my Meteor code.
Here is what I am doing:
VideoFileCollection = new FS.Collection("VideoFileCollection", {
stores: [
new FS.Store.FileSystem("videos", {path: "/uploads/videos"}),
new FS.Store.FileSystem("videosthumbs", {path: "/uploads/videosthumbs",
beforeWrite: function(fileObj) {
// We return an object, which will change the
// filename extension and type for this store only.
return {
extension: 'png',
type: 'image/png'
};
},
transformWrite: function(fileObj, readStream, writeStream) {
gm(readStream, fileObj.name()).stream('PNG').pipe(writeStream);
}
})
]
});
What is happening here that video is getting Uploaded to "videos" folder and one PNG is created under "videosthumbs" with 0 Bytes and thumbnail is not getting generated.
I have also read at : https://github.com/aheckmann/gm#custom-arguments
that we can use : gm().command() - Custom command such as identify or convert
Can Anybody advise me on what can be done to handle this situation?
Thanks and Regards
Checked the link that you have added and here is a rough solution that might help you
ffmpeg -ss 600 -i input.mp4 -vframes 1 -s 420x270 -filter:v 'yadif' output.png
Here is a function that i have made.
var im = require('imagemagick');
var args = [
"ffmpeg", "-ss", "600", "-i", "input.mp4", "-vframes", " 1", "-s", "420x270", "-filter:v", "'yadif'", "output.png"
];
// Function to convert and
im.convert(args, function(err)
if (err) throw err;
});

Write text on existing PNG with Node.js

I'm trying to create a simple dynamic-badge (png) to embed in static pages to let know the status of my application.
I'd like so to use an existing PNG image and write on it some text with Node.js.
I've found lot of libraries but all of them use Imagemagick or Cairo as native dependencies, I'd like to avoid to install anything else on the server.
I've then found lwip, but I can't really understand how to write text on an image with it. How can I do?
You could use Jimp. It has a print method:
var Jimp = require("jimp");
var fileName = 'test.png';
var imageCaption = 'Image caption';
var loadedImage;
Jimp.read(fileName)
.then(function (image) {
loadedImage = image;
return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
})
.then(function (font) {
loadedImage.print(font, 10, 10, imageCaption)
.write(fileName);
})
.catch(function (err) {
console.error(err);
});
If you want to use a ttf file you can use gm
This will also align the text automatically so you don't have to keep track of your letter sizing / location.
const gm = require('gm').subClass({imageMagick: true});
gm('path/to/image.png')
.fill("#FFFFFF")
.font("./font.ttf", 20)
.drawText(15, 10, "your text", 'southeast') //can be any location
.write("./output.png", function (err) {
if (!err) console.log('done');
});

How to convert image type to 'jpg' using imagemagick in node.js

I am using imagemagick in my node.js app. I have resized the image as follows:
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
//some code
}
});
I want my code to convert the incoming images to jpg. How can I achieve this?
For this solution you need use easyimage package . This package run under and need imagemagick (very important).
$npm install easyimage --save // saved in your package.json
Including in your code:
var easyimg = require('easyimage');
First you need get the path of original image:
tmp_path = 'path/to/image.svg';
Now you change the extension of the "tmp_path":
tmp_extless = tmp_path.replace('.svg','.png'); //be sure of include "."
So, tmp_path is the original path, and tmp_extless is the path of our future image.
Now, the magic of easyimage:
easyimg.convert({src: tmp_path, dst: tmp_extless, quality: 80},
function(err,stdout){
if(stdout){ console.log('stdout', stdout);
//here you can run a script to delete tmp_path
}
}
);
Now, your new image is in the path: tmp_extless.
With this method you don't need writeFile or readFile.
You need to writeFileSync and pipe your data in through the anonymous function.
im.resize({
format: 'jpg',
srcPath: imagePath,
dstPath: thumbPath,
width: 220,
}, function(err, stdout, stderr){
fs.writeFileSync('[filename].jpg', stdout,'binary'); //write the file here
}
});
I believe you also need to call the convert method.
im.convert(['originalfilename.originalextension', 'jpg:-'],

Resizing images using imagemagick in node.js

In my application i tried to resize the image using imagemagick but i got following error error while resizing: imagesexecvp(): No such file or directory.this is my code
im.resize({
srcPath: '/tmp/Images/' + req.files.image.name,
dstPath: 'resized_'+req.files.image.name ,
width:42,
height:42
}, function(err, stdout, stderr){
if (err) {
console.log('error while resizing images' + stderr);
};
});
The imagemagick module uses the convert and identify commands and the error you describe could happen because the commands can't be found. Make sure the commands are in a folder referenced by the path environment variable or, alternatively you can reference the commands in you node application:
var im = require('imagemagick');
im.identify.path = '/opt/ImageMagick/bin/identify'
im.convert.path = '/opt/ImageMagick/bin/convert';
It looks like you are trying to resize the file without changing the destination. Try this:
im.resize({
srcPath: process.cwd() + '/tmp/Images/' + req.files.image.name,
dstPath: process.cwd() + '/tmp/Images/resized_'+req.files.image.name ,
width:42,
height:42
}, function(err, stdout, stderr){
if (err) {
console.log('error while resizing images' + stderr);
};
console.log( process.cwd() + '/tmp/Images/' + req.files.image.name + 'has been resized and saved as ' + process.cwd() + '/tmp/Images/resized_'+req.files.image.name)
});
You might also check your permissions (ls -l) in /tmp/Images/ to make sure they are set properly
You need to install image magic command line tools. Try brew install imagemagick
If you are on ubuntu, install package like this:
apt-get install imagemagick
After that, try again :D

Resources