Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Last active August 24, 2016 18:16
Show Gist options
  • Save sudodoki/401f4327a8d305ef37c6aa1288e5b7df to your computer and use it in GitHub Desktop.
Save sudodoki/401f4327a8d305ef37c6aa1288e5b7df to your computer and use it in GitHub Desktop.
Placeholdit executable util for creating placeholder images in console

Placeholdit, command line version

Note:

Could replace all the 'Insert {{title}}.jpg here' with short and simple 'X × Y', by replacing 3 lines of ctx.fillText with

ctx.fillText(width + '×' + height, width / 2, height / 2 - fontSize * 0.1);

Installation

Clone this gist.

ln -s %pathToClonedGist%/placeholdit/placeholdit /usr/bin/placeholdit

Usage

placeholdit Something 1000 800 | cat > something.jpg
{
"name": "placeholdit",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "sudodoki <smd.deluzion@gmail.com> (sudodoki.name)",
"license": "MIT",
"dependencies": {
"canvas": "^1.4.0"
}
}
#!/usr/bin/env node
var title = process.argv[2];
var width = process.argv[3] && parseInt(process.argv[3], 10);
var height = process.argv[4] && parseInt(process.argv[4], 10);
var Canvas = require('canvas'),
defaultConfig = {
maxWidth : 8000,
maxHeight : 8000,
backgroundStyle : '#CCC',
textStyle : '#FFF',
fontFamily : 'Impact',
fontSizeParam : 5
};
function generateImage(width = 8000, height = 6000) {
var config = {};
for(k in defaultConfig) {
config[k] = defaultConfig[k];
}
canvas = new Canvas(width, height);
ctx = canvas.getContext('2d');
fontSize = Math.round(Math.min(width/title.length * 1.25,height / 3));
ctx.save();
ctx.fillStyle = config.backgroundStyle;
ctx.fillRect(0, 0, width, height);
ctx.restore();
ctx.save();
ctx.font = fontSize + 'px ' + config.fontFamily;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillStyle = config.textStyle;
ctx.fillText('Insert', width / 2, height / 3 - fontSize * 0.8);
ctx.fillText(`${title}.jpg`, width / 2, height / 3 * 2 - fontSize * 0.8);
ctx.fillText('here', width / 2, height - fontSize * 0.8);
ctx.restore();
return canvas.toBuffer();
}
const image = generateImage(800, 600);
process.stdout.write(image);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment