Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable jpeg support in node-canvas on x64 windows with MSVC10 compiler?

I need to render jpeg images on nodejs serverside. I install canvas npm module with following build instructions: https://github.com/Automattic/node-canvas/wiki/Installation---Windows I have C:\libjpeg-turbo\ as it is mentioned in the manual. If I do:

npm install canvas

then this piece of code doesn't launch onload:

 var data = fs.readFileSync("./t.jpg"); var img = new Image();
 img.onload = function () {
     console.log("onload"); 
 }; 
 img.src = data;

(it works with .png data). If i specify --with-jpeg in my command line, then MSVC complains about missing libjpeg.h. Right after this npm deletes file with msvc project, so I am unable to set paths myself (is it possible to switch off this cleanup?)

So how can I build canvas for windows with jpeg support? Found questions but no answers regarding this topic on the net.

like image 555
Stepan Yakovenko Avatar asked Jun 20 '15 11:06

Stepan Yakovenko


1 Answers

Install MSVC10 and 64bit nodejs. When your build fails, use the following distributives, which you'll find on the net:

  • unzip gtk+-bundle_2.22.1-20101229_win64.zip to c:/gtk . Use this version, others failed for me

  • setup libjpeg-turbo-1.4.0-vc64.exe to C:\libjpeg-turbo (32bit libs would fail with no meaningfull message)

  • go to the .\node_modules\canvas\build\ and modify your binding.gyp. Add 'variables':
  {
    'GTK_Root%': 'C:/GTK', # Set the location of GTK all-in-one bundle
    'with_jpeg%': 'true',
    'libjpeg_root%':'C:/libjpeg-turbo',
    'with_gif%': 'false',
    'with_pango%': 'false',
    'with_freetype%': 'false'
  }

and also

['with_jpeg=="true"', {
          'defines': [
            'HAVE_JPEG'
          ],
          'conditions': [
            ['OS=="win"', {
              'libraries': [
                '-l<(libjpeg_root)/lib/jpeg-static.lib','-l<(libjpeg_root)/lib/jpeg.lib'
              ],
            'include_dirs': [
        '<(libjpeg_root)/include'
      ]
            }, {
              'libraries': [
                '-ljpeg'
              ]
            }]
          ]
        }] 

then exec following commands in the folder, where .gyp file is located:

node-gyp configure

node-gyp build

IMHO libraries for such modules should be included as a part of npm distributions, its strange to expect WIN32/C++ expertise from nodejs programmer...

like image 195
Stepan Yakovenko Avatar answered Oct 21 '22 15:10

Stepan Yakovenko