node-gd does not work.

153 views
Skip to first unread message

Ziobudda

unread,
Feb 13, 2014, 3:59:58 AM2/13/14
to nod...@googlegroups.com
Hi all, I’m creating a little node.js app (for test) that get image from a FORM, save it in a directory and create a thumbnail version. 
Form and upload works with no problem, but I can not create the thumbnail. 
This is my code: 

app.post('/upload', function (req, res) {
    var tempPath = req.files.file.path,
        name = '',
        targetPath = '';
    
    fileExist = true; 
    fileNumber = 0;
    
    var fileType = path.extname(req.files.file.name);
    var fileName = path.basename(req.files.file.name,fileType);

    while (fileExist) {
  
      fileNumber_str = fileNumber.toString(); 
  
      var current = fileName + "_" +fileNumber_str + fileType;
      console.log("Controllo per "+current);
  
      if (fs.existsSync(__dirname + "/images/orig/" + current)) {
          console.log("--> Esiste");
          fileNumber++;
      } else {
          var newPath = __dirname + "/images/orig/" + current;
          console.log("nuovo nome : "+newPath);
          fs.rename(tempPath, newPath, function(err) {
                if (err) throw err;
                //Ora creo il thumb
                var newPathT = __dirname + "/images/thumb/" + current;
                gd.openPng([newPath],function(png,path){
                     if(png) {
                        var w = Math.floor(png.width/2), h = Math.floor(png.height/2);
                        var target_png = gd.createTrueColor(w, h);
                  
                        png.copyResampled(target_png,0,0,0,0,w,h,png.width,png.height);
                        target_png.savePng(newPathT, 1, gd.noop);
                     }
                     else {
                       console.log("no png");
                       console.log(png);
                       console.log(path); 
                     }
                });
                console.log("Upload completed!");
            });

  
          break;
      }
    }
    
    res.redirect("/");
res.end();    
});


The function gd.open(…) set always “png” variables to “undefined”. I don’t understand where is my error. 

Thanks in advance and sorry for my bad english.

M.

-- 
Michel 'ZioBudda' Morelli                       mic...@ziobuddalabs.it
Sviluppo applicazioni CMS DRUPAL e web dinamiche (LAMP+Ajax)
Telefono: 0200619074
Mobile: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         Skype: zio_budda
http://www.ziobuddalabs.it                     Twitter: ziobudda

Luca Morettoni

unread,
Feb 13, 2014, 4:07:42 AM2/13/14
to nod...@googlegroups.com
On 13 February 2014 09:59, Ziobudda <ziob...@gmail.com> wrote:
> The function gd.open(...) set always "png" variables to "undefined". I don't
> understand where is my error.
> Thanks in advance and sorry for my bad english.

hello Zio! I don't know the gd lib, but usually a callback function
uses the fingerprint "callback(err, data)", so the first param usually
need to be undef or null if the call is correct...
Can you drive me to the documentation of the "gd" lib?

--
Luca Morettoni <luca(AT)morettoni.net> | http://www.morettoni.net
http://it.linkedin.com/in/morettoni/ | http://twitter.com/morettoni
Google+ profile: https://www.google.com/+LucaMorettoni
Member of GDG Perugia: http://perugia.gtugs.org

Ziobudda

unread,
Feb 13, 2014, 4:10:06 AM2/13/14
to Luca Morettoni, nod...@googlegroups.com


Can you drive me to the documentation of the "gd" lib? 

Hi Luca, I have get node-gd from github:

https://github.com/taggon/node-gd


M.

-- 
Michel 'ZioBudda' Morelli                       ziob...@gmail.com

Alex Kocharin

unread,
Feb 13, 2014, 4:12:46 AM2/13/14
to nod...@googlegroups.com

First argument of openPng is an error. If you don't have an error, it will be undefined.

PS: I think "node-gd" library is a great example of why npm should have namespaces :P

Luca Morettoni

unread,
Feb 13, 2014, 4:14:25 AM2/13/14
to nod...@googlegroups.com
On 13 February 2014 10:12, Alex Kocharin <al...@equenext.com> wrote:
> First argument of openPng is an error. If you don't have an error, it will
> be undefined.

https://github.com/taggon/node-gd#using-gd

I think the Michel code is ok... later I'll try to run the example locally...

Alex Kocharin

unread,
Feb 13, 2014, 6:11:20 AM2/13/14
to nod...@googlegroups.com

What node-gd are we talking about?

taggon/node-gd? mikesmullin/node-gd? andris9/node-gd? hanssonlarsson/node-gd?

Okay, I see it's the first one. I just mistook it for https://npmjs.org/package/node-gd .

npm registry needs namespaces :P

Mr G

unread,
Aug 27, 2014, 8:38:47 PM8/27/14
to nod...@googlegroups.com, ziob...@gmail.com
Hi,

Did you get anywhere with this?  I have a similar problem.

png is set to null when trying to open the image file,

    var projectRootServer="../../../../../server/";
   
var source=projectRootServer+"forrest.png";
   
var target=projectRootServer+'privateimages/target_image.png';

   
var gd = Meteor.npmRequire('node-gd');
   
var path = Meteor.npmRequire('path');
   
var fs = Meteor.npmRequire('fs');

if (fs.exists(target)) fs.unlink(target);

gd
.openPng(source,
function(png, fs) {
   console
.log(png)
   
if(png) {
     
// png is null
   
}
}
);



Driving me crazy and now time for bed!

G :)
Reply all
Reply to author
Forward
0 new messages