Skip to content

Instantly share code, notes, and snippets.

@jonasrafael
Forked from dawsontoth/generateSplash.js
Created December 2, 2020 22:55
Show Gist options
  • Save jonasrafael/cb66f38b4455eb864d62842d5f67aaa1 to your computer and use it in GitHub Desktop.
Save jonasrafael/cb66f38b4455eb864d62842d5f67aaa1 to your computer and use it in GitHub Desktop.
Start with a 1000x1000 PSD with an icon in the middle. In Photoshop select File > Scripts > Browse... generateSplash.js. Saves out to ../Resources/iphone and android.
/*
Define our various sizes.
*/
var AndroidSizes = {
'android/images/res-ldpi/splash.9.png': 240,
'android/images/res-mdpi/splash.9.png': 360,
'android/images/res-hdpi/splash.9.png': 480,
'android/images/res-xhdpi/splash.9.png': 720,
'android/images/res-xxhdpi/splash.9.png': 960,
'android/images/res-xxxhdpi/splash.9.png': 1440
},
iOSSizes = {
'iphone/Default@2x.png': { width: 640, height: 960 },
'iphone/Default-568h@2x.png': { width: 640, height: 1136 },
'iphone/Default-667h@2x.png': { width: 750, height: 1334 },
'iphone/Default-Landscape-736h@3x.png': { width: 2208, height: 1242 },
'iphone/Default-Portrait-736h@3x.png': { width: 1242, height: 2208 },
'iphone/Default-Landscape.png': { width: 1024, height: 768 },
'iphone/Default-Portrait.png': { width: 768, height: 1024 },
'iphone/Default-Landscape@2x.png': { width: 2048, height: 1536 },
'iphone/Default-Portrait@2x.png': { width: 1536, height: 2048 }
};
/*
Utility functions.
*/
var originalPath = app.activeDocument.path;
var black = new SolidColor();
black.rgb.red = 0;
black.rgb.green = 0;
black.rgb.blue = 0;
function save(doc, name) {
var options = new ExportOptionsSaveForWeb();
options.quality = 70;
options.format = SaveDocumentType.PNG;
options.PNG8 = false; // PNG-24
options.optimized = true;
doc.exportDocument(File(originalPath + '/../Resources/' + name), ExportType.SAVEFORWEB, options);
return options;
}
function dot(doc, x, y) {
doc.selection.select([
[x, y], [x + 1, y], [x + 1, y + 1], [x, y + 1]
]);
doc.selection.fill(black);
}
/*
Let's get to it!
*/
(function() {
var doc = app.activeDocument.duplicate(),
originalWidth = doc.width,
originalHeight = doc.height;
/*
Generate iOS splash screens.
*/
for (var iOSName in iOSSizes) {
var width = iOSSizes[iOSName].width,
height = iOSSizes[iOSName].height,
least = Math.min(width, height);
// Scale.
doc.resizeImage(UnitValue(least, 'px'), UnitValue(least, 'px'));
// Canvas to the proper size.
doc.resizeCanvas(UnitValue(width, 'px'), UnitValue(height, 'px'), AnchorPosition.MIDDLECENTER);
// Save the image.
save(doc, iOSName);
// Canvas back to our original aspect ratio.
doc.resizeCanvas(UnitValue(least, 'px'), UnitValue(least, 'px'), AnchorPosition.MIDDLECENTER);
// Reverse back to the original.
doc.resizeImage(originalWidth, originalHeight);
}
/*
Generate Android .9 patch splash screens.
*/
for (var androidName in AndroidSizes) {
var dim = AndroidSizes[androidName],
unitValue = UnitValue(dim, 'px');
// Crop the edges.
doc.crop([0, 0, originalWidth, originalHeight]);
// Resize and add a 1px gutter along the whole image.
doc.resizeImage(unitValue, unitValue);
doc.resizeCanvas(UnitValue(dim + 2, 'px'), UnitValue(dim + 2, 'px'), AnchorPosition.MIDDLECENTER);
// Draw dots.
var newLayer = doc.artLayers.add();
newLayer.name = '.9';
dot(doc, 2, 0);
dot(doc, 0, 2);
dot(doc, dim - 2, 0);
dot(doc, 0, dim - 2);
save(doc, androidName);
// Undo our changes.
doc.resizeCanvas(unitValue, unitValue, AnchorPosition.MIDDLECENTER);
doc.crop([0, 0, unitValue, unitValue]);
doc.resizeImage(originalWidth, originalHeight);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment