Skip to content

Commit

Permalink
call phantomjs from python for each resolution to make it work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yaph committed Sep 12, 2012
1 parent c88ec1b commit 7eb0536
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 52 deletions.
28 changes: 28 additions & 0 deletions .gitignore
@@ -1 +1,29 @@
*.png

*.py[co]

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox

#Translations
*.mo

#Mr Developer
.mr.developer.cfg
7 changes: 7 additions & 0 deletions LICENSE
@@ -0,0 +1,7 @@
Copyright (C) 2012 by Ramiro Gómez (http://www.ramiro.org/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions README.md
@@ -0,0 +1 @@
# webshots - Take Web page screenshots at common browser resolutions
25 changes: 25 additions & 0 deletions webshots
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Create Web page with common browser resolutions for given URL and save them as
# PNG files in current working directory.
#
# Requires PhantomJS: http://phantomjs.org/
# Written by Ramiro Gómez http://ramiro.org/
# MIT licensed: http://rg.mit-license.org/

import argparse, os
from subprocess import call

# common screen resolutions from gs.statcounter.com
resolutions = [
[1440, 900], [1366, 768], [1280, 1024], [1280, 800], [1024, 768], # web
[800, 600], [480, 800], [360, 640], [320, 480], [240, 320] # mobile
]

parser = argparse.ArgumentParser(description='webshots - create Web page screenshots')
parser.add_argument('url')
url = parser.parse_args().url

for r in resolutions:
call(['phantomjs', os.path.join(os.path.dirname(__file__), 'webshots.js'), url, str(r[0]), str(r[1])])
63 changes: 11 additions & 52 deletions webshots.js
@@ -1,78 +1,37 @@
/*
A script using PhantomJS http://phantomjs.org to create Web page screenshots
with common browser resolutions and save them as PNG files.
Known issues:
- Lower timeouts may lead to errors which can cause the script to not terminate.
- undefined:0 TypeError: 'undefined' is not an object
A script using PhantomJS http://phantomjs.org to create Web page screenshot at
the given browser resolution and save it as a PNG file.
Written by Ramiro Gómez http://ramiro.org/
MIT licensed : http://rg.mit-license.org/
MIT licensed: http://rg.mit-license.org/
*/

var fs = require('fs')
var cwd =fs.workingDirectory
var files = []

var page = require('webpage').create()
var re_proto = /https?:\/\//
var re_conv = /[^\w\.-]/

// common screen resolutions from gs.statcounter.com
var resolutions = [
[1440, 900],
[1366, 768],
[1280, 1024],
[1280, 800],
[1024, 768],
[800, 600],
[480, 800],
[360, 640],
[320, 480],
[240, 320]
]

var url2filename = function(url, w, h) {
return url.replace(re_proto, '').replace(re_conv, '-') + '.' + w + 'x' + h + '.png'
}

var webshot = function(url, w, h) {
var page = require('webpage').create()
page.viewportSize = { width: w, height: h }
page.open(url, function(status) {
if (status !== 'success') {
console.log('Unable to load url: ' + url)
} else {
window.setTimeout(function() {
fname = url2filename(url, w, h)
files.push(fname)
page.render(fname)
page.release()
}, 1000);
page.clipRect = { top: 0, left: 0, width: w, height: h }
page.render(url2filename(url, w, h))
phantom.exit()
}, 200)
}
})
}

if (phantom.args.length < 1 || phantom.args.length > 2) {
console.log('Usage: webshots.js URL')
if (3 !== phantom.args.length) {
console.log('Usage: phantomjs webshots.js http://example.com 1024 768')
phantom.exit()
} else {
var url = phantom.args[0]
for (i in resolutions) {
webshot(url, resolutions[i][0], resolutions[i][1])
}
webshot(phantom.args[0], phantom.args[1], phantom.args[2])
}

// exit callback hell when finished
window.setInterval(function() {
var created = 0;
if (files.length === resolutions.length)
var files_in_dir = fs.list(cwd)
for (i in files) {
if (-1 !== files_in_dir.indexOf(files[i])) {
created++
}
}
if (created === resolutions.length) {
phantom.exit()
}
}, 1000)

0 comments on commit 7eb0536

Please sign in to comment.