Skip to content

Instantly share code, notes, and snippets.

@oxyflour
Last active October 14, 2016 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oxyflour/1fd4923852a9e5ee66fe to your computer and use it in GitHub Desktop.
Save oxyflour/1fd4923852a9e5ee66fe to your computer and use it in GitHub Desktop.
create image thumbnails with phantom.js
// 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