Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progressive quality digression to meet max_size requirements. #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 33 additions & 9 deletions lib/web2splash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var path = require('path');
var async = require('./async');
var fs = require('fs');

/*
* render(input, output, [callback)]
Expand Down Expand Up @@ -77,14 +78,34 @@ exports._renderImages = function(input, output, images, callback) {
document.body.style.zoom = pixelRatio;
}, pixelRatio);

/* wait a bit then render the image */
setTimeout(function () {
var filepath = path.join(output, image.name);
page.render(filepath, function() {
exports.onRenderImage(image);
next();
});
}, 200);
/* Attempt first render at 100% quality */
(function attemptRender(quality){
/* wait a bit then render the image */
setTimeout(function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be function() {

var filepath = path.join(output, image.name);
page.render(filepath,{quality:quality},function(e) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add spaces after each options:

page.render(filepath, { quality: quality }, function(e) {

// Check file size and append to image hash.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this file uses the /* blah blah */ style of comments.

fs.stat(filepath,function(err,stat){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be: fs.stat(filepath, function(e, stat) {

image.size = stat.size;
image.quality = quality;
// If max_size option is set, check it and try again at a lower quality.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment style

if (exports.max_size && exports.max_size < stat.size) {
if (quality > exports.min_quality)
attemptRender(quality-exports.quality_increment);
else {
image.warn = 'Could not render image under the max_size setting ('+exports.max_size+') without going under the min_quality setting ('+exports.min_quality+').';
exports.onRenderImage(image);
next();
}
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Else on the next line.

exports.onRenderImage(image);
next();
}
});
});
}, exports.render_timeout);
})(100);

});
});
},
Expand Down Expand Up @@ -121,7 +142,10 @@ function evaluate(page, func) {
return page.evaluate(fn);
}


exports.max_size;
exports.min_quality = 50;
exports.quality_increment = 5;
exports.render_timeout = 200;
/*
* The splash screens to render as images.
*/
Expand Down