Skip to content

Instantly share code, notes, and snippets.

@egv
Last active August 29, 2015 14:07
Show Gist options
  • Save egv/0bb3986fdc195c563359 to your computer and use it in GitHub Desktop.
Save egv/0bb3986fdc195c563359 to your computer and use it in GitHub Desktop.
js script to make iOS app icons
#!/usr/bin/env node
//
// Obviously, you should have node and npm installed.
//
// also do not forget to
// npm install q
// npm install imagemagick
//
// before using this script
//
var fs = require('fs')
, Q = require('q')
, im = require('imagemagick');
// check command-line params, we should have at least one which is source file name
// idioms allowed for usage
var allowedIdioms = ['iphone', 'ipad', 'car'];
var requiredSizesAndScales = {'iphone': [{'size': 29, 'scale': 1}
, {'size': 29, 'scale': 2}
, {'size': 29, 'scale': 3}
, {'size': 40, 'scale': 2}
, {'size': 40, 'scale': 3}
, {'size': 57, 'scale': 1}
, {'size': 57, 'scale': 2}
, {'size': 60, 'scale': 2}
, {'size': 60, 'scale': 3}]
, 'ipad': [ {'size': 29, 'scale': 1}
, {'size': 29, 'scale': 2}
, {'size': 40, 'scale': 1}
, {'size': 40, 'scale': 2}
, {'size': 50, 'scale': 1}
, {'size': 50, 'scale': 2}
, {'size': 72, 'scale': 1}
, {'size': 72, 'scale': 2}
, {'size': 76, 'scale': 1}
, {'size': 76, 'scale': 2}]
, 'car': [ {'size': 120, 'scale': 1}]};
function printUsageAndExit() {
console.log("Usage: " + process.argv[1] + " <source image> [idiom1] [idiom2] [idiom3]");
console.log('Valid idiom values are "iphone", "ipad" and "car". If you specify no idioms all 3 will be used.');
process.exit(1);
}
function flatten() {
var merged = [];
Array.prototype.forEach.call(arguments, function (arg) {merged = merged.concat.apply(merged, arg)});
return merged;
}
function resizePromise(elt2) {
return Q.promise(function(resolve, reject) {
var res = {'idiom': elt2['idiom']
, 'size': elt2['size'] + 'x' + elt2['size']
, 'scale': elt2['scale'] + 'x'
, 'filename': 'Icon' + elt2['size'] + (elt2['scale'] === 1 ? '' : '@'+ elt2['scale'] + 'x') + '.png'};
var sizeArg = elt2['size']*elt2['scale'] + 'x' + elt2['size']*elt2['scale'];
if (processed.indexOf(res['filename']) == -1) {
im.convert([sourceImageName, '-resize', sizeArg, __dirname + '/' + res['filename']],
function (err, stdout) {
if (err) {
console.log('error processing ' + JSON.stringify(elt2));
console.log(stdout);
reject(stdout);
} else {
resolve(res);
}
});
} else {
resolve(res);
}
});
}
if (process.argv.length < 3 || process.argv.length > 6) {
console.log("Usage: " + process.argv[1] + " <source image> [idiom1] [idiom2] [idiom3]");
console.log('Valid idiom values are "iphone", "ipad" and "car". If you specify no idioms all 3 will be used.');
process.exit(1);
}
var sourceImageName = process.argv[2];
var idioms = allowedIdioms;
if (process.argv.length > 3) {
idioms = [];
for (var i=3; i < process.argv.length; i++) {
var idiom = process.argv[i].toLowerCase();
if (idioms.indexOf(idiom) == -1 && allowedIdioms.indexOf(idiom) != -1) {
idioms.push(idiom);
}
}
}
if (idioms.length == 0) {
printUsageAndExit();
}
console.log('will use following idioms: ' + idioms);
var result = {'images': []
, 'info': {'version': 1, 'author': 'make_icons.js script'}};
var processed = [];
Q.all(flatten(idioms.map(function(elt){
return requiredSizesAndScales[elt]
.map(function(elt1){
var res = elt1;
res['idiom'] = elt;
return res;})
.map(resizePromise)})))
.then(function(e) {
return Q.promise(function (resolve, reject) {
result['images'] = e;
fs.writeFile('Contents.json', JSON.stringify(result, undefined, 2), function (err) {
if (err) {
reject('error writing contents');
} else {
resolve();
}
});
});
})
.done(function(e) {
console.log("we're good!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment