Code Monkey home page Code Monkey logo

imagely's Introduction

Imagely

Imagely renders any browserable content as an image, including:

  • Local HTML or SVG files
  • Remote URLs
  • D3 visualizations
  • jQuery/Angular/React/Ember-powered UIs

If it can be viewed in a WebKit browser, Imagely can render it.

Installation

npm install imagely

To enable command-line usage, install with --link:

npm install imagely --link

Usage

Node

var imagely = require('imagely');
imagely(source, destination, [options], [callback]);

Parameters:

  • source (String) URL or local filepath to render. Required.
  • destination (String) Filepath of the image file to generate. Supported file types: PNG, GIF, JPEG, PDF. Required.
  • options (Object) See options below.
  • options.width (Number) Viewport pixel width.
  • options.height (Number) Viewport pixel height.
  • options.scale (Number) Zoom level at which to render, necessary for generating HiDPI/Retina images (e.g. scale = 2). Defaults to 1.
  • options.bg (String) Background color. Defaults to transparent.
  • options.json (String) Filepath of a JSON file to preload. File contents will be accessible via window.data and available for any scripts in the source file to use.
  • options.batch (Flag) If you want to loop over options.data to create multiple images. This assumes that all created images are identical except for the window.data you inject on the page.
  • callback (Function) Function that will be called after the image has been successfully generated.

Command line

imagely source destination [--width=<number>] [--height=<number>] [--scale=<number>] [--bg=<color>] [--json=<path>] [--log]

Parameters:

  • source (String) URL or local filepath to render. Required.
  • destination (String) Filepath of the image file to generate. Supported file types: PNG, GIF, JPEG, PDF. Required.
  • -w, --width (Number) Viewport pixel width.
  • -h, --height (Number) Viewport pixel height.
  • -s, --scale (Number) Zoom level at which to render, necessary for generating HiDPI/Retina images (e.g. scale = 2). Defaults to 1.
  • -b, --bg (String) Background color. Defaults to transparent.
  • -d, --json (String) Filepath of a JSON file to preload. File contents will be accessible via window.data and available for any scripts in the source file to use.
  • -l, --log (Flag) If specified, the dimensions of the final image will be logged to the console as width height.
  • --batch (Flag) If specified, imagely will loop through the --json file as an array. Each iteration will be injected into the page as window.data.

Examples

As shown below, Imagely can be used interchangeably within Node or the command line. See the examples directory for the full example files.

D3 chart

// Node
var imagely = require('imagely');
imagely(
	'examples/d3/chart.html',
	'examples/d3/chart.gif',
	{
		json: 'examples/d3/data.json',
		bg: 'white'
	}
);
# Command line
imagely examples/d3/chart.html examples/d3/chart.gif --json=examples/d3/data.json --bg=white

Batched D3 chart

// Node
var imagely = require('imagely');
imagely(
	'examples/d3-batch/chart.html',
	'examples/d3-batch/chart.gif', 
	{
		json: 'examples/d3-batch/data.json',
		bg: 'purple',
		batch: true
	}
);
# Command line
imagely examples/d3-batch/chart.html examples/d3-batch/chart.gif --json=examples/d3-batch/data.json --bg=purple --batch

Remote URL

// Node
var imagely = require('imagely');
imagely(
	'http://google.com',
	'examples/google.jpg',
	{ width: 800, height: 600 }
);
# Command line
imagely http://google.com examples/google.jpg -w 800 -h 600

Retina SVG

// Node
var imagely = require('imagely');
imagely(
	'http://ariya.github.io/svg/tiger.svg',
	'examples/tiger.png',
	{ scale: 2 },
	function(dimensions) {
		console.log(dimensions.width, dimensions.height);
	}
);
# Command line
imagely http://ariya.github.io/svg/tiger.svg examples/tiger.png --scale=2 --log
# Logs: 1004 1051

How it works

Imagely is a light wrapper around PhantomJS. Local file rendering isn't well supported by PhantomJS, so in those cases Imagely inlines the content of all referenced scripts and stylesheets, even if they're remote URLs.

imagely's People

Contributors

choshun avatar mpetrovich avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.