Skip to content

Instantly share code, notes, and snippets.

@UmberFunk
Last active April 9, 2016 00:16
Show Gist options
  • Select an option

  • Save UmberFunk/4bea6eee6f569967d81181d054dfb71c to your computer and use it in GitHub Desktop.

Select an option

Save UmberFunk/4bea6eee6f569967d81181d054dfb71c to your computer and use it in GitHub Desktop.
Photoshop Script to create iOS and Android screenshots (assets) of all required resolutions from multiple PNGs dropped into one folder. NOTE: Input PNGs must be 2732x2048px (Landscape) or 2048x2732px (Portrait).
// Photoshop Script to create iOS and Android screenshots (assets) of all required resolutions
// from multiple PNGs dropped into one folder.
//
// WARNING!!! In the rare case that there are name collisions, this script will
// overwrite (delete perminently) files in the same folder in which the selected
// PNG file is located. Therefore, to be safe, before running the
// script, it's best to make sure the selected PNG file is the only
// file in its containing folder.
//
// Copyright (c) 2016 Irakli Geleishvili. http://bonsters.co
// Based on script created by Matt Di Pasquale https://gist.github.com/mattdipasquale/711203
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Prerequisite:
// * First, create 2732 x 2048 or 2048 x 2732 px PNGs and put them into one folder.
// This is highest resolution required for markets. It will be cropped and
// downsampled for other required resolutions.
//
// * Install - Place "Create Screenshots.jsx" here:
// Win: C:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit CS5\SDK
// Mac:
// * Restart Photoshop
//
// Update:
// * Just modify & save, no need to resart Photoshop once it's installed.
//
// Run:
// * With Photoshop open, select File > Scripts > Create Screenshots
// * Select input folder where you have placed souce PNGs.
// * Select the destination folder.
// * Folders will be created for each type (iOS 4-inch, Android Phone and etc.) and
// the resampled PNGs will be placed each in corresponding folder.
//
// Adobe Photoshop JavaScript Reference
// http://www.adobe.com/devnet/photoshop/scripting.html
// Turn debugger on. 0 is off.
//$.level = 1;
// Is your image in landscape mode or in portrait?
var landscapeMode = false;
var inputFolder = Folder.selectDialog("Select a folder to process. PNGs must be exactly 2732x2048 or 2048x2732 px");
if (inputFolder != null) var destFolder = Folder.selectDialog("Choose an output folder. Subfolders will be created automatically.");
if (inputFolder != null && destFolder != null)
{
var fileList = inputFolder.getFiles("*.png");
//$.writeln(fileList.length);
for (var f = 0; f < fileList.length; f++) {
try
{
var PNG = fileList[f];
var doc = open(PNG, OpenDocumentType.PNG);
if (doc == null)
{
throw "Something is wrong with the file. Make sure it's a valid PNG file.";
}
var startState = doc.activeHistoryState; // save for undo
var initialPrefs = app.preferences.rulerUnits; // will restore at end
app.preferences.rulerUnits = Units.PIXELS; // use pixels
if ((doc.width == 2732 && doc.height == 2048) || (doc.width == 2048 && doc.height == 2732))
{
// Image size is good
}
else
{
throw "Image size is NOT 2732x2048 or 2048x2732 px.";
}
// Save screenshots in PNG using Save for Web.
var sfw = new ExportOptionsSaveForWeb();
sfw.format = SaveDocumentType.PNG;
sfw.PNG8 = false; // use PNG-24
sfw.transparency = false;
doc.info = null; // delete metadata
var screenShots = [
{"type": "Android 7", "size1":1920,"size2":1080},
{"type": "Android 10", "size1":2560,"size2":1600},
{"type": "Android Phone", "size1":1920,"size2":1080},
{"type": "iOS 3.5", "size1":960,"size2":640},
{"type": "iOS 4", "size1":1136,"size2":640},
{"type": "iOS 4.7", "size1":1334,"size2":750},
{"type": "iOS 5.5", "size1":2208,"size2":1242},
{"type": "iOS iPad", "size1":2048,"size2":1536},
{"type": "iOS iPad Pro", "size1":2732,"size2":2048}
];
var sshot;
for (i = 0; i < screenShots.length; i++)
{
sshot = screenShots[i];
var ratio;
if (landscapeMode)
{
sshot.width = sshot.size1;
sshot.height = sshot.size2;
ratio = sshot.height / 2048;
}
else
{
sshot.width = sshot.size2;
sshot.height = sshot.size1;
ratio = sshot.height / 2732;
}
var W = 2 * Math.round((doc.width * ratio) / 2);
var H = 2 * Math.round((doc.height * ratio) / 2);
// Change resolution
doc.resizeImage(W, H, // width, height
null, ResampleMethod.BICUBICSHARPER);
// Crop image to fit requirements
// NOTE: All images, portrait or landscape are cropped from sides. Not from top and bottom.
var T = 0;
var L = (doc.width - sshot.width) / 2;
var B = sshot.height;
var R = (doc.width - sshot.width) / 2 + sshot.width;
var bounds = new Array(L, T, R, B);
doc.crop(bounds);
// Create folder for each type
var folder = new Folder(destFolder + "/" + sshot.type);
if ( !folder.exists ) {
folder.create()
}
var destFileName = doc.name;
doc.exportDocument(new File(destFolder + "/" + sshot.type + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
doc.activeHistoryState = startState; // undo resize
}
}
catch (exception)
{
// Show degbug message and then quit
if ((exception != null) && (exception != ""))
alert(exception);
}
finally
{
if (doc != null) doc.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = initialPrefs; // restore prefs
}
}
alert("Screenshots created!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment