Is's my code that works on localhost, but not on EC2 (I used Elastic BeanStalk). I get error: Stream yields empty buffer on toBuffer function. Can somebody explain how to fix it? I saw this post, but I didn't understand how to add JPEG support in EC2.
var _45px = { width: 45, dstnKey: fileName, destinationPath: "thumbnails" };
function convert(response, next) {
console.time("convertImage");
console.log("Reponse content type : " + response.ContentType);
console.log("Conversion");
GM(response.Body, fullName).antialias(true).density(300).setFormat('jpeg').toBuffer(
function(err, buffer) {
if (err) {
next(err);
} else {
console.timeEnd("convertImage");
next(null, buffer);
}
});
}
I faced a similar problem with PNG format. The problem arises because EC2 instances does not have support for PNG and JPEG, so we need to install the required dependencies accordingly, else the imagemagick (or graphicmagick) does not return any stream value as output.
For adding PNG support I have tried uninstalling graphicsmagick module and then installed the dependencies and reinstalled graphicsmagick:
apt-get install libpng-dev,
apt-get install graphicsmagick
(In your case this would be imagemagick)
This solved the problem. I believe for jpeg support the library would be:
libjpeg-dev.
These are for EC2 ubuntu instances
Related
I am trying to run the basic hello world example from the documentation from here (it's the first example on the page):
https://github.com/agracio/edge-js
I have a typescript file that I am running (see the code below). I am on node version 9.9.0 on a Windows 10 64 bit. I have made only the following installations:
npm install edge
npm install edge.js
npm install #types/node --save-dev
I have made the installations to the same directory as the typescript file.
I am able to run ts-node app.ts in the command line and have it console.log("hi") successfully in that directory.
However, when I change my code to the example below, it is giving me the error throw new Error('If .NET source is provided as JavaScript function, function body must be a /* ... */ comment.');
I had originally tried doing this using edge.js but I kept getting the error that I needed to precompile. For the life of me I couldn't get it to find my python executable when executing build.bat release 10.5.3. (Despite including an environment variable PYTHON with a value of c:\Python\Python37\python.exe) I wanted to try using edge-js because it was already precompiled. I downgraded node to 9.9 (I uninstalled node 10.15.3 and then installed the 9.9.0 msi from the website) because I thought edge-js only supported version 9. Well here I am trying to run edge-js with version 9 and I am still getting an error, although it is a different error.
Here is the code I am trying to run:
var edge = require('edge-js');
var helloWorld = edge.func(function () {/*
async (input) => {
return ".NET Welcomes " + input.ToString();
}
*/});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
});
I can't believe this worked. It was a syntax error. A problem with the amount of whitespace between the comment and the end of the function. Here is the correct syntax:
var edge = require('edge-js');
var helloWorld = edge.func(function () {
/*async (input) => {
return ".NET Welcomes " + input.ToString();
}*/
});
helloWorld('JavaScript', function (error, result) {
if (error) throw error;
console.log(result);
});
I am trying to run an imageMagick command on AWS Lambda and using the gm module. I keep getting an error no decode delegate for this image format `' # error/constitute.c/ReadImage/544. I believe this error indicates that my syntax isn't correct for the command. I've tried many ways. I can run this command on the command line on my Linux system fine.
Here's the command. (adapted from here)
convert test.jpg -crop 120x120+300+300 -colorspace gray -format "%[fx:100*mean]%%" info:
Here's my function.
gm(imgobj,'test.jpg').command('convert')
.in('-crop', '120x120+300+300','-colorspace','gray','-format','%[fx:100*mean]%%')
.out('info:')
.stream(function (err, stdout, stderr) {
});
gm nodejs module. is here.
SOLVED!
gm(imgobj,'test.jpg').command('convert')
.in('-crop', '120x120+300+300')
.in('-colorspace', 'gray')
.toBuffer(function(err, buffer) {
if(err) throw err;
gm(buffer, 'test.jpg').identify({bufferStream: true, format:'%[fx:100*mean]'},function(err, data) {
if(err) throw err;
console.log('identify',data);
});
});
The documentations mentions this "GOTCHA":
When working with input streams and any 'identify'operation (size,
format, etc), you must pass {bufferStream: true} if you also need to
convert (write() or stream()) the image afterwards (NOTE: this buffers
the readStream in memory!).
The documentation says to use: gm().identify(format, callback) and this seems to work for me without setting bufferStream: true. I suppose that is correct since I do not "need to stream the image afterwards." However, for general knowledge, I looked at the source and figured out how to pass both params {bufferStream: true, format:'%[fx:100*mean]'} format being the escape argument.
I have a very strange error. I'm using MAC OS X 10.9.5.
When i use some functions from GM (npm install gm) like resize or something else i got this error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
I have found many threads about it and according to them i have installed graphicksmagic and imagemagick with brew to system.
brew install imagemagick
brew install graphicsmagick
It was working for a while but for no reason it starts to showing the above error again.
I checked that i have installed imagemagick and graphicksmagick in system and its working from terminal.
I checked if i have it in $PATH and its there.
If i run this in nodejs it shows correct version of imagemagick in console, so i assume path is ok in nodejs.
var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) {
if(error)
sys.puts(error);
if(stderr)
sys.puts(stderr);
sys.puts(stdout);
}
exec("identify --version", puts);
This is code which is crashing:
gm(request(url), thumbName)
.resize('300', '300', '^')
.gravity('Center')
.crop('300', '300')
.toBuffer('JPG',function (err, buffer) {
var data = {
Bucket: bucket,
Key: thumbName,
Body: buffer,
ACL:'public-read',
ContentType: 'image/jpeg'
};
s3.putObject(data, function(err, res) {
....
});
Everything is working when i deploy it on heroku
IF someone will have this issue i solved it.
I add variable PATH to enviroment variables in node with this value
bin:node_modules/.bin:/usr/local/bin:/usr/bin:/bin
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
i'm trying to create a thumbnail of an jpg that resides on the server. i tried using node-gd and/or node-imagemagick but neither could access the file:
var gd = require('node-gd');
gd.openJpeg("./test.jpeg", function (img, path) {
if (img) {
console.log("file opened ... " + img);
}
else {
console.log("failed to open file ...");
}
});
logs: failed to open file ...
imagemagick:
var im = require('imagemagick');
im.identify('./test.jpeg', function (err, features) {
if (err) throw err;
console.log(features);
});
logs: Error: Command failed: execvp(): No such file or directory
but the test.jpeg file is definitely there.
var fs = require('fs');
fs.open(filePath, 'r', function (err, fd) {
console.log("open file ... " + err + " " + fd);
});
works fine!? no error is logged.
i tried chmod 0777 on the jpeg. nothing.
From what I understand of the documentation of the imagemagick module for node is, that the module provides access to the comandline binaries of imagemagick. Do you have imagemagick (the commandline binaries) installed? Are they in the PATH of you shell?
You are looking for a binary named "identify". You can show the path to it by running "which identify". It should give you a full path - if the prompt just returns, you don't have it installed or it's not in your path.
If you are on win32 the which command won't help, you have to check for a binary called identify.exe.
(never worked with gd - so I am unsure there)
here is the imagemagick example with your code - please note, the path to identify may be different in your environment:
snowflake:Desktop rhaen$ node check_im.js
{ format: 'JPEG', width: 320, height: 250, depth: 8 }
snowflake:Desktop rhaen$ which identify
/usr/local/bin/identify
So - the node module and your code works for me.