Skip to content

Instantly share code, notes, and snippets.

@colin-johnston
Forked from dudleystorey/batch-resize.jsx
Last active August 29, 2015 14:19
Show Gist options
  • Save colin-johnston/43c3f1946971d672f0cc to your computer and use it in GitHub Desktop.
Save colin-johnston/43c3f1946971d672f0cc to your computer and use it in GitHub Desktop.
// explanatory article: https://demosthenes.info/blog/993/1x-2x-3x-more-Batch-Processing-Retina-Images-with-PhotoShop-and-JavaScript
var inputFolder = Folder.selectDialog("Select a folder to process"),
outputFolder = Folder.selectDialog("Select a folder for the output files"),
imageSizes = [
["1200px", "", "_large"],
["800", "", "_medium"],
["400", "", "_small"]
],
numImageSizes = imageSizes.length;
if (inputFolder != null && outputFolder != null) {
var fileList = inputFolder.getFiles(/\.(jpg|jpeg|png|gif)$/i);
for(var i=0; i<fileList.length; i++) {
var doc = app.open( fileList[i] );
for (var j = 0; j < imageSizes.length; j++) {
var currentImageSize = imageSizes[j],
currentImageWidth = currentImageSize[0],
currentImageHeight = currentImageSize[1],
currentImageVersion = currentImageSize[2],
fullname = doc.name,
filename = fullname.substr(0, fullname.lastIndexOf('.')) || fullname,
extension = fullname.split('.').pop(),
exportOptionsSaveForWeb = new ExportOptionsSaveForWeb();
doc.resizeImage(currentImageWidth, currentImageHeight);
exportOptionsSaveForWeb.includeProfile = true;
exportOptionsSaveForWeb.optimized = true;
if (extension == "jpg" || extension == "jpeg") {
exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;
exportOptionsSaveForWeb.includeProfile = true;
exportOptionsSaveForWeb.quality = 40;
}
if (extension == "png") {
exportOptionsSaveForWeb.format = SaveDocumentType.PNG;
}
if (extension == "gif") {
exportOptionsSaveForWeb.format = SaveDocumentType.GIF;
}
var documentPath = decodeURI(outputFolder) + "/" + filename + "-" + currentImageVersion + "." + extension,
file = new File(documentPath);
doc.exportDocument (file, ExportType.SAVEFORWEB, exportOptionsSaveForWeb);
}
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment