Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

allow to resize images #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"node": ">=0.8.4"
},
"dependencies": {
"express": "3.x",
"config": "0.4.15",
"request": "2.9.153"
"express": "3.x",
"request": "2.9.153",
"rsz": "^0.2.3"
}
}
6 changes: 5 additions & 1 deletion public/usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
# Return a 1024x600 PNG screenshot of the www.google.com homepage
# 1 second after it's loaded

# Resize the image (thumbnails)
GET /?url=www.google.com&resizeWidth=200&resizeHeight=200
# Return a 200x200 PNG thumbnail of screenshot of the www.google.com homepage

# Use an HTML form to ask for a screenshot
<a href="/form.html">GET /form.html</a>
</pre>
</body>
</body>
34 changes: 27 additions & 7 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var join = require('path').join;
var fs = require('fs');
var path = require('path');
var request = require('request');
var rsz = require("rsz");

module.exports = function(app, useCors) {
var rasterizerService = app.settings.rasterizerService;
Expand All @@ -24,6 +25,13 @@ module.exports = function(app, useCors) {
if (req.param(name, false)) options.headers[name] = req.param(name);
});

// parameters to pass the sendImage-call
options.sendImage = {};
['resizeWidth', 'resizeHeight'].forEach(function(name) {
if (req.param(name, false)) options.sendImage[name] = parseInt(req.param(name));
});


var filename = 'screenshot_' + utils.md5(url + JSON.stringify(options)) + '.png';
options.headers.filename = filename;

Expand Down Expand Up @@ -69,7 +77,7 @@ module.exports = function(app, useCors) {
// synchronous
callRasterizer(rasterizerOptions, function(error) {
if (error) return callback(error);
sendImageInResponse(filePath, res, callback);
sendImageInResponse(filePath, res, callback, rasterizerOptions);
});
}
}
Expand Down Expand Up @@ -101,16 +109,28 @@ module.exports = function(app, useCors) {
}));
}

var sendImageInResponse = function(imagePath, res, callback) {
var sendImageInResponse = function(imagePath, res, callback, options) {
console.log('Sending image in response');
if (useCors) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Expose-Headers", "Content-Type");
}
res.sendfile(imagePath, function(err) {
fileCleanerService.addFile(imagePath);
callback(err);
});
var sendImage = function(){
res.sendfile(imagePath, function(err) {
fileCleanerService.addFile(imagePath);
callback(err);
});
}
var resizeScheduled = options != undefined && options.sendImage.resizeWidth != undefined && options.sendImage.resizeHeight != undefined;
if(resizeScheduled){
rsz(imagePath, options.sendImage.resizeWidth,options.sendImage.resizeHeight, function (err, buf) {
if (err) console.log('Error while resizing the screenshot: %s', err);
fs.writeFileSync(imagePath, buf)
sendImage();
})
} else {
sendImage();
}
}

};
};