I have a process for getting images in node.js using jsdom or Canvas. During the boot process, I want to extract samples using Vibrant.js in the backend. None of my codes below work.
Using jsdom
const Vibrant = require('node-vibrant'); const request = require('request'); var jsdom = require("jsdom").jsdom; var window = jsdom().defaultView; var document = jsdom('<html><body></body></html>', { features: { FetchExternalResources : ['img'] } }); var imgDom = document.createElement("img"); imgDom.onload = function() { console.log('onload triggered'); // var imgData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC'; // var imgData = imgDom.replace(/^data:image\/gif;base64,/, "") // var binaryData = new Buffer(imageData, 'base64').toString('binary'); request.get(imgDom.src, function(err, res, body) { console.log(body.length); // Spit out a bunch of base64 code let v = new Vibrant(new Buffer(body, 'binary').toString('base64')); // Error image.load not found // let v = new Vibrant(new Buffer(body, 'binary').toString('base64'), {ImageClass: window.Image}); // Error: Path must be a string without null bytes // let v = new Vibrant(new Buffer(body, 'base64').toString('binary')); // v.getPalette().then((palette) => console.log(palette)); var swatches = v.swatches(); console.log(JSON.stringify(swatches)); for (var swatch in swatches) if (swatches.hasOwnProperty(swatch) && swatches[swatch]) console.log(swatch, swatches[swatch].getHex()) }); } imgDom.src = 'https://unsplash.it/200'; // imgDom.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAIAAAA7ljmRAAAAGElEQVQIW2P4DwcMDAxAfBvMAhEQMYgcACEHG8ELxtbPAAAAAElFTkSuQmCC';
Using Canvas
const Vibrant = require('node-vibrant'); const request = require('request'); const Canvas = require('canvas-prebuilt'); const Image = Canvas.Image; var imgDom = new Image; imgDom.onload = function() { console.log('onload triggered'); let v = new Vibrant(imgDom, {ImageClass: Canvas.Image}); v.getPalette().then((palette) => console.log(palette)); }; request.get('https://unsplash.it/200', function(err, res, body) { console.log(body.length); imgDom.src = new Buffer(body);
I could use the URL in Vibrant.js, but another http GET using Bluebird, and I need to avoid reloading the network.
I think Vibrant.js is not easy to understand Image object in node.js.
What is the workaround here so that I can pass the correct Image class to Vibrant.js?
source share