Last active
March 28, 2017 22:09
-
-
Save sugendran/50aa23a0eab27ae16602fcbab09ab834 to your computer and use it in GitHub Desktop.
For NativeScript - Process all images from a source folder and creates suitably sized images in the app's target folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// based around the setup in https://www.npmjs.com/package/nativescript-media-generator | |
// all %s come from it | |
// npm install imagemagick async | |
// update the source and target paths | |
// then run node mkgraphics.js | |
// It should put correctly sized images in the right folders | |
var im = require('imagemagick'); | |
var fs = require("fs"); | |
var path = require("path"); | |
var async = require("async"); | |
var sourcePath = path.resolve("./tipple-graphics/graphics"); | |
var targetPath = path.resolve("./Tipple/app/App_Resources"); | |
var queue = []; | |
function isImageFile(src) { | |
var ext = path.extname(src).toLowerCase(); | |
return ext === ".jpg" || ext === ".jpeg" || ext === ".png"; | |
} | |
function resize(src, percent, target, callback) { | |
var dir = path.dirname(target); | |
if (!fs.existsSync(dir)) { | |
fs.mkdirSync(dir); | |
} | |
console.log(src + " --> " + target); | |
im.convert([src, '-resize', percent, target], callback); | |
} | |
function processFile(src, callback) { | |
var ext = path.extname(src); | |
var diff = path.relative(sourcePath, src); | |
var target = path.join(targetPath, diff); | |
async.series([ | |
resize.bind(null, src, "24.5%", path.join(targetPath, 'iOS', diff)), | |
resize.bind(null, src, "49%", path.join(targetPath, 'iOS', diff).replace(ext, '@2x' + ext)), | |
resize.bind(null, src, "73.6%%", path.join(targetPath, 'iOS', diff).replace(ext, '@3x' + ext)), | |
resize.bind(null, src, "75%", path.join(targetPath, 'Android/drawable-hdpi', diff)), | |
resize.bind(null, src, "50%", path.join(targetPath, 'Android/drawable-mdpi', diff)), | |
resize.bind(null, src, "100%", path.join(targetPath, 'Android/drawable-xhdpi', diff)), | |
resize.bind(null, src, "70%", path.join(targetPath, 'Android/drawable-xxhdpi', diff)), | |
resize.bind(null, src, "93.4%", path.join(targetPath, 'Android/drawable-xxxhdpi', diff)) | |
], callback); | |
} | |
function processPath(src) { | |
var files = fs.readdirSync(src); | |
for (var i = 0; i < files.length; i++) { | |
var file = files[i]; | |
var fullpath = path.join(src, file); | |
var stats = fs.statSync(fullpath); | |
if (stats.isDirectory()) { | |
processPath(fullpath); | |
} else if (isImageFile(fullpath)) { | |
queue.push(processFile.bind(null, fullpath)); | |
} | |
} | |
} | |
processPath(sourcePath); | |
async.series(queue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment