Skip to content

Instantly share code, notes, and snippets.

@birksy89
Last active January 11, 2018 09:33
Show Gist options
  • Save birksy89/fba401e0a789ea2579e52ea41a85c90a to your computer and use it in GitHub Desktop.
Save birksy89/fba401e0a789ea2579e52ea41a85c90a to your computer and use it in GitHub Desktop.
Conditional Resizer, With Subfolder, and Folder Name Checker, and Quality Prompt etc
var imageFolder = Folder.selectDialog("Select a folder to process");
var fWidth = prompt("Width", "1000", "Width2");
var fHeight = prompt("Height", "1000", "Height2");
var fQuality = prompt("Quality 1-100", "70", "Quality2");
if (imageFolder != null) processFolder(imageFolder);
function processFolder(folder) {
//alert('Ready!');
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof File && file.name.match(/\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png|tga)$/i)) {
//Do Stuff
open(file)
// get a reference to the current (active) document and store it in a variable named "doc"
doc = app.activeDocument;
//Check the document isn't already smaller than the required new size
if (doc.height > fHeight || doc.width > fWidth) {
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
}
}
//Regardless of the sizing, apply filter and or save.
// call autoContrast and applySharpen on the active layer.
// if we just opened a gif, jpeg, or png, there's only one layer, so it must be the active one
//doc.activeLayer.autoContrast();
//doc.activeLayer.applySharpen();
//Make Black and White
//doc.activeLayer.desaturate();
//var newName = 'web-' + doc.name + '.jpg'; - This used to append the .jpg - It would do it to all files inc existing jpgs making them .jpg.jpg
var newName = 'Web-' + doc.name; // Appends Web
// var newName = doc.name; //Nothing Changes
//Add files to Sub folder?
var folder1 = Folder(doc.path + "/Resized");
//Check if it exist, if not create it.
if (!folder1.exists) folder1.create();
//Normal Save and close
//doc.save()
//doc.close(SaveOptions.DONOTSAVECHANGES);
//Export Style Save
//Web export options
var options = new ExportOptionsSaveForWeb();
options.quality = fQuality;
options.format = SaveDocumentType.JPEG;
options.optimized = true;
//Remove whatever filename extension it used to have (png, gif, etc)
newName = newName.substr(0, newName.lastIndexOf('.')) || newName;
//doc.exportDocument(File(doc.path + "/"+newName), ExportType.SAVEFORWEB, options); - Into Same Folder
doc.exportDocument(File(doc.path + "/Resized/" + newName + ".jpg"), ExportType.SAVEFORWEB, options); //Into Sub Folder created above
doc.close(SaveOptions.DONOTSAVECHANGES);
}
else {
//Check to see if the next "file" is actually a folder...
if (file instanceof Folder) {
//If it is, check it's not already one of our "resized" folders
var folderName = file.toString();
var isResizeFolder = new RegExp ("Resized$").test(folderName);
if (!isResizeFolder) {
processFolder(file);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment