1

I am using gm for node.js on linux server with express.js . It works fine with images less than 2MB filesize. But the moment it exceeds that the code stops working. Here's the code

gm('public/'+article.page.image)
    .resize(width,height)
    .quality(80)
    .write('public/article_image/'+ folder + '/' + "something.jpg", function(err){
        if (err){
            console.log('error occurred')
            console.log(err)
        }
        else{
            console.log('resized image 16')
            somefunction(16, true);
        }
   })

The error I am facing goes

Error: Command failed:
at ChildProcess.onExit (/root/patarboi/node_modules/gm/lib/command.js:301:17) 
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:920:16)
at Socket.<anonymous> (internal/child_process.js:351:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:497:12) code: null, signal: 'SIGSEGV'

How to solve this?

2 Answers2

1

The problem was causing because of segmentation error of imagemagick. I was using an old version of imagemagick. Instead of installing like this.

sudo apt-get install ImageMagick

find the latest version of imagemagick from here. Then install it like below. At present the latest version is ImageMagick-7.0.7-22.tar.gz. At first build the dependency. Then get it from the url.

$sudo apt-get install build-essential checkinstall && apt-get build-dep imagemagick -y   
$sudo wget http://www.imagemagick.org/download/ImageMagick-7.0.7-22.tar.gz

Now unzip

$sudo tar xzvf 7.0.7-22.tar.gz

Move to the directory

cd ImageMagick-7.0.7-22

Check the configuration ./configure Now build the code

make clean
make
checkinstall
ldconfig /usr/local/lib

To check if the installation is done

convert -version
0

We should use pipe to handle large file to avoid reading full file into memory.

let writeStream = fs.createWriteStream('public/article_image/' + folder + '/' + "something.jpg");
writeStream.on('finish', () => {
    console.log('resized image 16')
    somefunction(16, true);
});

writeStream.on('error', (errr) => {
    console.log('error occurred')
    console.log(err)
});

gm('public/' + article.page.image)
    .resize(width, height)
    .quality(80)
    .stream()        
    .on('error', (err) => {
        console.log('read stream err', er)
    })
    .pipe(writeStream);
Cr.
  • 886
  • 7
  • 12
  • Well, seems like the images are being written without any data if the size is greater than 2MB. But not throwing any error. Any word on that? – Hassan Nomani Feb 01 '18 at 11:51
  • It's work for me, and I edit the code to monitor `readStream` error event, have any error now? And if nothing throwing, use linux command `ps -ef | grep gm` to check what command the `gm` library execute, and then execute it by command to see the error. – Cr. Feb 02 '18 at 03:42
  • Not actually. It was happening due to the imagemagick segmentation error. – Hassan Nomani Feb 08 '18 at 05:57
  • In order to solve this problem using the latest version helped. – Hassan Nomani Feb 08 '18 at 05:58