Skip to content

Instantly share code, notes, and snippets.

@rise-worlds
Last active March 23, 2021 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rise-worlds/8b6495f28a45713b66a60f983ee0573d to your computer and use it in GitHub Desktop.
Save rise-worlds/8b6495f28a45713b66a60f983ee0573d to your computer and use it in GitHub Desktop.
ps generate android icon

Install - Save Create Icons.jsx to:

MAC: /Applications/Adobe Photoshop CS5/Presets/Scripts

PC: c:\program files\Adobe Photoshop CS5\Presets\Scripts

Run:

With Photoshop open, select File > Scripts > Create Icons

Adobe Photoshop JavaScript Reference http://www.adobe.com/devnet/photoshop/scripting.html

try {
    // Prompt user to select iTunesArtwork file. Clicking "Cancel" returns null.
    var iTunesArtwork = File.openDialog("Select a sqaure PNG file that is at least 1024x1024.", "*.png", false);

    if (iTunesArtwork !== null) {
        var doc = open(iTunesArtwork, 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 != doc.height) {
            throw "Image is not square";
        }
        else if ((doc.width < 1024) && (doc.height < 1024)) {
            throw "Image is too small! Image must be at least 1024x1024 pixels.";
        }
        else if (doc.width < 1024) {
            throw "Image width is too small! Image width must be at least 1024 pixels.";
        }
        else if (doc.height < 1024) {
            throw "Image height is too small! Image height must be at least 1024 pixels.";
        }

        // Folder selection dialog
        var destFolder = Folder.selectDialog("Choose an output folder");

        if (destFolder == null) {
            // User canceled, just exit
            throw "";
        }

        // Save icons in PNG using Save for Web.
        var sfw = new ExportOptionsSaveForWeb();
        sfw.format = SaveDocumentType.PNG;
        sfw.PNG8 = false; // use PNG-24
        sfw.transparency = true;
        doc.info = null; // delete metadata

        var icons = [
            { "name": "ic_launcher-web", "size": 512 },
            { "name": "ic_launcher-xxxhdpi", "size": 192 },
            { "name": "ic_launcher-xxhdpi", "size": 144 },
            { "name": "ic_launcher-xhdpi", "size": 96 },
            { "name": "ic_launcher-hdpi", "size": 72 },
            { "name": "ic_launcher-mdpi", "size": 48 },
            { "name": "ic_launcher-ldpi", "size": 36 }
        ];

        var icon;
        for (i = 0; i < icons.length; i++) {
            icon = icons[i];
            doc.resizeImage(icon.size, icon.size, // width, height
                null, ResampleMethod.BICUBICSHARPER);

            var destFileName = icon.name + ".png";

            doc.exportDocument(new File(destFolder + "/" + destFileName), ExportType.SAVEFORWEB, sfw);
            doc.activeHistoryState = startState; // undo resize
        }

        alert("Android Icons created!");
    }
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment