Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active August 29, 2015 14:25
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 PierBover/2ce931fd092c5cba99dc to your computer and use it in GitHub Desktop.
Save PierBover/2ce931fd092c5cba99dc to your computer and use it in GitHub Desktop.
Conditional resize image script for Photoshop. Creates thumbnail and large version and saves in JPEG.
var image = app.activeDocument;
var imageRatio = image.width / image.height;
// if your path has spaces you have to escape them, or use a path with no spaces
var folderThumbs = "/path/to/folder/thumbs";
var folderLarge = "/path/to/folder/large";
var thumbArea = {
width: 700,
height: 500
};
thumbArea.ratio = thumbArea.width / thumbArea.height;
var largeArea = {
width: 2000,
height: 1500
};
largeArea.ratio = largeArea.width / largeArea.height;
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.quality = 70;
saveOptions.format = SaveDocumentType.JPEG;
saveOptions.optimized = true;
// THUMBS
var scaleThumb;
if(imageRatio >= thumbArea.ratio){
scaleThumb = thumbArea.width / image.width.value;
} else {
scaleThumb = thumbArea.height / image.height.value;
}
// do not upscale
if (scaleThumb < 1) {
image.resizeImage(UnitValue(image.width * scaleThumb,"px"),UnitValue(image.height * scaleThumb,"px"),null,ResampleMethod.AUTOMATIC);
}
image.exportDocument(File(folderThumbs + "/" + image.name),ExportType.SAVEFORWEB,saveOptions);
// we go back to initial state
image.activeHistoryState = image.historyStates[0];
// LARGE
var scaleLarge;
if(imageRatio >= largeArea.ratio){
scaleLarge = largeArea.width / image.width.value;
} else {
scaleLarge = largeArea.height / image.height.value;
}
// do not upscale
if (scaleLarge < 1) {
image.resizeImage(UnitValue(image.width * scaleLarge,"px"),UnitValue(image.height * scaleLarge,"px"),null,ResampleMethod.AUTOMATIC);
}
image.exportDocument(File(folderLarge + "/" + image.name),ExportType.SAVEFORWEB,saveOptions);
image.close(SaveOptions.DONOTSAVECHANGES);
@PierBover
Copy link
Author

Check this GIST only for conditional resize
https://gist.github.com/PierBover/2748189c25dee4dfa195

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment