node.jsbase64gm

Nodejs: Decoding base64Image Data and process it with gm "size error spawn E2BIG" error


I have made the following string where I try to decode a base64 encoded image and process it via gm using buffer:

const gm=require('gm');
const URLSafeBase64 = require('urlsafe-base64');
const iconv = require('iconv-lite');
const fs=require('fs');

//Content too bit to put it inside the script
var base64Data="iVBORw0KGgoAAAANSUhEUg....AAA3NCSVQICAjb4U/gAASuQmCC"

base64Data = base64Data.replace(/^data:\w*\/\w*;base64,/gm,'')
base64data = new Buffer(base64Data,'base64')

gm(base64Data).setFormat('png').size(function(err,value){
    if(err){
     return console.log("size error",err.message);
   }

   if(500 < value.width){
     let ratio= width/value.width;
     let newHeight=value.height*ratio;
     this.resize(width,newHeight)
        /**
         * @var {Buffer} value
         */
        .toBuffer(function(err,value){
          if(err){
            return console.log("Error: ",err)
          }
          fs.writeSync('./image.png',value.toString('binary'));
        });
   } else {
    fs.writeSync('./image.png',buffer.toString('binary'));
   }
});

But When I try to tun it I get the following error:

size error spawn E2BIG

Do you have any idea why does it happen?


Solution

  • You're not passing the correct argument to gm(): instead of the buffer, called base64data, you're passing the string, called base64Data.

    This will make gm think you're passing a filename, which ends up being passed as an argument to the GraphicsMagick executable, and because it's large you get an E2BIG ("Error: too big").