From 3f27ae249a92a88935aa7037e272b609b656727d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Thu, 28 May 2015 11:09:45 +0200 Subject: [PATCH 1/2] Add a white background to screenshots --- lib/screenshotHandler.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js index 5b82cd3..1b81f9e 100644 --- a/lib/screenshotHandler.js +++ b/lib/screenshotHandler.js @@ -48,6 +48,8 @@ var screenshotHandler = function() { }) + .then(this.addWhiteBackground) + .then(this.toBuffer); }; @@ -92,6 +94,39 @@ var screenshotHandler = function() { return deferred.promise; }; + // If the page doesn't set a background color, the default PhantomJS one is transparent. + // When transforming PNG to JPG, transparent pixels become black. + // This is why we need to had a transparent background. + this.addWhiteBackground = function(image) { + var deferred = Q.defer(); + + // Create a canvas with the same dimensions as your image: + lwip.create(image.width(), image.height(), 'white', function(err, canvas){ + if (err) { + debug('Could not create a white canvas'); + debug(err); + + deferred.reject(err); + } else { + // Paste original image on top of the canvas + canvas.paste(0, 0, image, function(err, image){ + if (err) { + debug('Could not paste image on the white canvas'); + debug(err); + + deferred.reject(err); + } else { + // Now image has a white background... + debug('White background correctly added'); + deferred.resolve(image); + } + }); + } + }); + + return deferred.promise; + }; + this.toBuffer = function(image) { var deferred = Q.defer(); From b00eba77f94b6d480af97c18bf9f752733c6a53c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Thu, 28 May 2015 11:12:02 +0200 Subject: [PATCH 2/2] Fix typo in comment --- lib/screenshotHandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js index 1b81f9e..b803ed9 100644 --- a/lib/screenshotHandler.js +++ b/lib/screenshotHandler.js @@ -94,9 +94,9 @@ var screenshotHandler = function() { return deferred.promise; }; - // If the page doesn't set a background color, the default PhantomJS one is transparent. + // If the page doesn't set a bg color, the default PhantomJS one is transparent // When transforming PNG to JPG, transparent pixels become black. - // This is why we need to had a transparent background. + // This is why we need to add a transparent background. this.addWhiteBackground = function(image) { var deferred = Q.defer();