Last active
January 4, 2025 07:32
-
-
Save pixelbacon/7754cba64125b0240504 to your computer and use it in GitHub Desktop.
Export folder of AI, PNG, or PSD files to PNG w/ transparency
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
// Source: https://gist.github.com/pixelbacon/7754cba64125b0240504 | |
// Inspired by: | |
// https://github.com/jonahvsweb/Photoshop-Automated-Resize-to-Web.jsx | |
// http://blog.wp.weightpoint.se/2012/02/02/batch-export-photoshop-psd-files-to-png-the-right-way/ | |
// To use open Script via Photoshop OR File > Scripts > Browse | |
#target "photoshop" | |
if (BridgeTalk.appName == "photoshop") | |
{ | |
app.bringToFront; | |
var inputFolder = Folder.selectDialog("Select the folder that contains the files for export:"); | |
var outputFolder = Folder.selectDialog("Select the folder to output to:"); | |
if (inputFolder != null) { | |
var fileList = inputFolder.getFiles (/\.(psd|png|ai)$/i); | |
var maxPixels = prompt ("Maximum height & width for exported files:", "2000"); | |
var desiredWidth = prompt ("Desired WIDTH for exported files:", maxPixels); | |
var desiredHeight = prompt ("Desired HEIGHT for exported files:", maxPixels); | |
for (var i = 0; i < fileList.length; i++) { | |
if (fileList [i] instanceof File) { | |
var document = open (fileList [i]); | |
var documentName = fileList [i].name.slice (0, -4); | |
while (app.documents.length) { | |
var newFile = new File (decodeURI (outputFolder) + "/" + documentName + ".png"); | |
var max = maxPixels + " pixels"; | |
// By flattening we lose transparency | |
// document.flatten (); | |
if (document.resolution > 72 || (document.width > max || document.height > max)) { | |
if (document.width > document.height) { | |
var newWidth = new UnitValue (desiredWidth + " pixels"); | |
var newHeight = new UnitValue (desiredHeight + " pixels"); | |
} else { | |
var newWidth = new UnitValue (desiredHeight + " pixels"); | |
var newHeight = new UnitValue (desiredWidth + " pixels"); | |
} | |
document.resizeImage (newWidth, newHeight, 72); | |
} | |
exportOptions = new ExportOptionsSaveForWeb (); | |
exportOptions.format = SaveDocumentType.PNG; | |
exportOptions.transparency = true; | |
exportOptions.PNG8 = false; | |
// exportOptions.format = SaveDocumentType.JPEG; | |
// exportOptions.quality = 80; | |
document.exportDocument (newFile, ExportType.SAVEFORWEB, exportOptions); | |
document.close (SaveOptions.DONOTSAVECHANGES); | |
} | |
} | |
if (i == fileList.length - 1) { | |
alert ("All the files have been exported."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I tried to use this script to resize a bunch of png's, but when I ran it it didn't seem to keep the aspect ratio (meaning images were coming out squashed).
I had a look and modified your script as follows:
if (document.resolution > 72 || (document.width > max || document.height > max)) {
var newWidth = new UnitValue (desiredWidth + " pixels");
var newHeight = new UnitValue ((document.height*desiredWidth/document.width)+ " pixels");
document.resizeImage (newWidth, newHeight, 72);
}
Essentially this will set the image width to be whatever you choose as your "desired width" and then resizes the height proportionally to the chosen width.
This could be used in a similar fashion to fix heights, too.
:-)