Giter VIP home page Giter VIP logo

html-image-generator's Introduction

HTML Image Generator

The HTML Image Generator allows you to generate multiple images, with a common layout, using an HTML file as the template.

Potential uses include:

  • Generating social media preview images for a series of blog posts.
  • Creating images with a common background and varying text.
  • Anything where you want to have multiple similar images.

Mail Merge for Images

Using the image generator is as easy as creating address labels.

These images:

green example red example blue example yellow example

are all generated from the same basic HTML markup:

<body>
	<div class="container">
		<img src="{{image}}">
		<p class="name">{{name}}</p>
	</div>
</body>

And it's as simple as:

  1. Create a template for your images using HTML.
  2. Prepare the data source
  3. Run the generator.

Quick Start

A sample template is included in template/sample.htm and sample data files in the data directory.

  1. Install NodeJS
  2. Download the application
  3. Run npm install from the application's root directory.
  4. From the application's root directory, run
node index.js

Installation

Prerequisite The HTML Image Generator requires that you install NodeJS

  1. Download the application
  2. In the application's root directory, run
npm install	

Create a Template

The HTML image generator transforms HTML files into an images. The HTML file is created to look like the final version of the image, and then the portions that will vary between versions of the image are replaced with placeholder values.

<body>
	<div class="container">
		<img src="image/frog.png">
		<p class="name">Green</p>
	</div>
</body>

(see layout.htm for the full file)

External resources (images, stylesheets, etc.) may be loaded from either subdirectory paths relative to the HTML file, or from any accessible web site.

TIP: Place a <div> element (e.g. the "container" div in this exmple) around the entire markup and style it with a fixed height and width equal to the desired image size; next, add a solid, 1 pixel, visible border. This will allow you to preview the image and compare how it aligns to the planned edges. Once the layout is complete, change the border to match the background.

Once the layout has been created, locate the elements which will vary between images, and replace them with placeholder values surrounded by double curly-braces ({{ and }}). In this example, the text image/frog.png and Green will be replaced by other images and color names, leading to this template file:

<body>
	<div class="container">
		<img src="{{image}}">
		<p class="name">{{name}}</p>
	</div>
</body>

The layout.htm file also contains some CSS for the container div:

.container {
	height: 200px;
	width: 600px; 
	border: #0f0 10px solid;
}

The border color changes too, so the #0f0 color code is also replaced with a placeholder, becoming:

.container {
	width: 580px;
	height: 180px;
	border: {{colorCode}} 10px solid;
}

TIP: Technically, the template file is a Handlebars template, and may be as complex or simple as desired.

Configure the template

In the config sub-directory, create a new file named local.json. This is where you override the settings to control the image file size and the template name. (Don't edit the defaults file!)

The local.json should look something like this:

{
    "templates": [
        {
            "template":"template/sample.htm",
            "width": 600,
            "height": 200
        }
    ]
}

Change the width and height values to the desired image size and change the template value to your actual template.

(NOTE: although templates is an array, support for multiple templates is planned for a future version. For now, only the first set of values is used.)

Create the data source

In the data subdiretory, create a separate .json file for each image you would like to create. Each file must contain the name and replacement values for each of the placholders in the template. The formal specification for JSON files is online, but a quick summary is that the file is a collection of name/value pairs. Each name appears in quotation marks (That's "double quotes" NOT the so-called 'single quotes'), is followed by a colon, and then the value. String values must also appear in quotation marks, numbers may appear without. Each name/value pair is separated from the next by a comma, and the entire thing is surrounded by curly braces ({ and }).

Continuing with the above example, red.json would be

{
    "name": "Red",
    "image": "image/cherries.png",
    "colorCode": "#770000"
}

green.json would be

{
    "name": "Green",
    "image": "image/frog.png",
    "colorCode": "#007700"
}

and so on.

The generated files will use the same name as the JSON file (e.g. red.json will contain values for creating red.png, yellow.json will create yellow.png and so on).

Running the Image Generator

Congratulations if you've made it this far! The steps you should have completed by now are:

  1. Install NodeJS
  2. Download the application
  3. Run npm install from the application's root directory.
  4. Create a template
  5. Configure the template
  6. Create the data source

To generate the images, from the application's root directory, run the command

node index.js

(There are no command-line parameters.)

If all goes well, the generated images should appear in the output directory.

html-image-generator's People

Contributors

thatblairguy avatar

Watchers

James Cloos avatar  avatar

Forkers

praveentelu

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.