From 5b8ee712c4e572d894c4122f579ce599fcd5a189 Mon Sep 17 00:00:00 2001 From: Artem Sapegin Date: Wed, 12 Dec 2012 17:59:18 +0400 Subject: [PATCH] Bash: rasterize function. --- docs/Bash.md | 4 ++++ includes/bash_functions.bash | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/docs/Bash.md b/docs/Bash.md index a6ecea2..7ccef7f 100644 --- a/docs/Bash.md +++ b/docs/Bash.md @@ -104,6 +104,10 @@ Upload current directory to special directory on my hosting. Backup remote MySQL database to `~/Backups/hostname/dbname_YYYY-MM-DD.sql.gz`. +### rasterize + +Save page screenshot to file. + ## NPM diff --git a/includes/bash_functions.bash b/includes/bash_functions.bash index 745d95f..ec73f4d 100644 --- a/includes/bash_functions.bash +++ b/includes/bash_functions.bash @@ -224,3 +224,37 @@ mysql-dump() { echo "Done: $local_filepath" fi } + +# Save page screenshot to file +# USAGE: rasterize +# Based on https://github.com/oxyc/dotfiles/blob/master/.bash/commands +function rasterize() { + local url="$1" + local filename="$2" + if [[ $url == "" ]] || [[ $filename == "" ]]; then + echo "Usage: rasterize " + else + header "Rasterizing $url..." + + [[ $url != http* ]] && url="http://$url" + [[ $filename != *png ]] && filename="$filename.png" + phantomjs <(echo " + var page = new WebPage(); + page.viewportSize = { width: 1280, height_: 1024 }; + page.open('$url', function (status) { + if (status !== 'success') { + console.log('Unable to load the address.'); + phantom.exit(); + } + else { + window.setTimeout(function() { + page.render('$filename'); + phantom.exit(); + }, 1000); + } + }); + ") + + echo "Screenshot saved to: $filename" + fi +} \ No newline at end of file