javascript - GraphicsMagick processes resulting in empty file -
i'm doing this
gm(jpgname).setformat('jpg') .resize(160,158) .compress('jpeg') .write(fs.createwritestream(jpgname),function(err){ if(err){ console.log(err,jpgname); res.send(400); }else{ console.log('file formated jpg' + jpgname);
and get
{ [error: command failed: gm convert: empty input file (/home/ubuntu/node/uploads/icon.jpg). ] code: 1, signal: null }
if stop code right before first line written here, , @ file, has file length. if let process happen (error out) @ file, has size of 0 bytes.
i tried this:
fs.stat(jpgname, function (err, stats) { console.log('file size:',stats.size); gm(jpgname).setformat('jpg') .resize(160,158) .compress('jpeg') .write(fs.createwritestream(jpgname),function(err){ if(err){ console.log('console.log error writing jpg file',err,jpgname); fs.unlink(jpgname); callback(400); return; }else{//...
and i'm getting file size back, know it's not empty before start process.
edit: start process on form.on('end')
sure entire upload process done.
the entire function:
function formatprofileimage(req,form,file,callback){ console.log('formatting profile image'); var ext = file.name.split('.')[1].tolowercase(); var name = encodeuricomponent(file.name.split('.')[0].tolowercase()); var smallname = form.uploaddir+"/"+"small_"+name+".jpg"; var jpgname = form.uploaddir + "/" + name+'.jpg'; console.log('extension:',ext); console.log('name:',name); console.log('smallname:',smallname); console.log('jpgname:',jpgname); if(!(ext == "png" || ext == "jpeg"|| ext == "jpg"|| ext == "gif")){ fs.unlink(file.path); console.log("extension rejected"); callback(415); return; } console.log('renaming file ' + file.path + ' ' + jpgname); fs.rename(file.path,jpgname,function(err){ if(err){ console.log('error renaming file',err); callback(400); return; } console.log('file renamed'); fs.stat(jpgname, function (err, stats) { console.log('file size:',stats.size); if(!(typeof req.query.tenant === 'undefined')){ //rename jpg gm(jpgname).setformat('jpg') .resize(160,158) .compress('jpeg') .write(fs.createwritestream(jpgname),function(err){ if(err){ console.log('console.log error writing jpg file',err,jpgname); fs.unlink(jpgname); callback(400); return; }else{ console.log('file formated jpg' + jpgname); //make "thumbnail" gm(jpgname).setformat('jpg') .resize(50) .write(fs.createwritestream(smallname),function(err){ if(err){ fs.unlink(smallname); console.log('error writing thumbnail',err,smallname); callback(400); return; }else{ console.log('thumbnail created' + smallname); //upload uploads3(jpgname,req.query.tenant,function(back){ if(back.success){ console.log('success ' +back.url); callback(back.url); fs.unlink(jpgname); }else{ console.log('error uploading' + jpgname, back); callback(400); fs.unlink(jpgname); return; } }); uploads3(smallname,req.query.tenant,function(back){ if(back.success){ console.log('success ' +back.url); fs.unlink(smallname); }else{ console.log('error uploading ' + smallname, back); callback(400); fs.unlink(smallname); return; } }); } }); } }); } }); }); }
.write()
accept string path. need replace stream string (location, resulting image must me saved).
gm support 3 ways of saving files.
1) save file. simple, use .write()
, send parameter string, file should saved. example:
gm('image.png') .resize() .write('resized.png', function (error) { if (error) { console.error(error); } });
2) use streams, example:
gm('image.png') .resize() .stream(function (error, stdout, stderr) { var writestream = fs.createwritestream('resized.jpg'); stdout.pipe(writestream); });
3) , last 1 - buffers:
gm('image.png') .resize() .tobuffer(function (error, buffer) { if (error) { console.error(error); } });
Comments
Post a Comment