Skip to content

Instantly share code, notes, and snippets.

@r17x
Last active March 24, 2019 16:42
Show Gist options
  • Select an option

  • Save r17x/73ed2c8a07429e4c50cf8b467563e52d to your computer and use it in GitHub Desktop.

Select an option

Save r17x/73ed2c8a07429e4c50cf8b467563e52d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/**
* @description Image Generator for Splash Screen Mobile Development.
* @author ri7nz <[email protected]>
*
* @todo - [ ] handle sharp if not available in environment.
* @todo - [ ] add image dimensions list for iOS (.ipa) splash image.
* @todo - [ ] handle image type
* @todo - [ ] Send Your Request Feature
* @todo - [ ] handle for potrait or landscape type
**/
const sharp = require('sharp')
const ArgumentParser = require('argparse').ArgumentParser
const parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description:
'Generator Image For Splash Screen Mobile Development Examples:' +
' BuildAssetsMobile.js --source a.png --platform android|ios' +
' --target $HOME',
})
parser.addArgument(['-s', '--source'], {
action: 'store',
type: 'string',
help: '-s path/to/img.png',
required: true,
})
parser.addArgument(['-t', '--target'], {
action: 'store',
type: 'string',
help: '-t path/to/target/generate/img.png',
defaultValue: './',
required: true,
})
parser.addArgument(['-p', '--platform'], {
action: 'store',
type: 'string',
help: '-p android|ios',
required: true,
})
const args = parser.parseArgs()
const android = {
ldpi: { x: 200, y: 320 },
mdpi: { x: 320, y: 480 },
hdpi: { x: 480, y: 800 },
xhdpi: { x: 720, y: 1280 },
xxhdpi: { x: 960, y: 1600 },
xxxhdpi: { x: 1280, y: 1920 },
}
if (args.source) {
let target = args.target.endsWith('/') ? args.target : args.target.concat('/')
if (args.platform === 'android') {
try {
console.log(`Generate Image For Platform ${args.platform}`)
console.log(`Image List:`)
Object.keys(android).forEach(img => {
const original = sharp(args.source)
const toFile = `${target}${img}.png`
original
.resize(android[img].x, android[img].y)
.toFile(toFile)
.then(res => console.log(`- ${toFile}`))
})
} catch (e) {
//handle error
console.log('err', e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment