Last active
October 14, 2016 10:37
-
-
Save oxyflour/1fd4923852a9e5ee66fe to your computer and use it in GitHub Desktop.
create image thumbnails with phantom.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run `phantomjs index.js [directory] [imagename]` in the shell and file will be out.png | |
var WIDTH = 200, | |
MARGIN = 5, | |
COLS = 3 | |
var fs = require('fs'), | |
sys = require('system'), | |
dir = sys.args[1] || '.' | |
var imgs = fs.list(dir).filter(function (file) { | |
var ext = file.toLowerCase().split('.').pop() | |
return ext === 'bmp' || ext === 'jpg' || ext === 'png' | |
}).map(function (file) { | |
return '<img src="' + dir + '/' + file + '" style="float:left;width:' + WIDTH + 'px;margin:' + MARGIN + 'px" />' | |
}) | |
fs.write('out.html', '<html><body style="padding:0;margin:0">' + imgs.join('') + '</body></html>', 'w') | |
var page = require('webpage').create() | |
page.viewportSize = { width: (WIDTH + MARGIN * 2) * COLS, height: 800 } | |
page.open('out.html', function() { | |
page.render(sys.args[2] || 'out.png') | |
phantom.exit() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment