Skip to content

Instantly share code, notes, and snippets.

@chriszs
Created March 31, 2014 20:58
Show Gist options
  • Save chriszs/9902074 to your computer and use it in GitHub Desktop.
Save chriszs/9902074 to your computer and use it in GitHub Desktop.
Proportional image resize script for Photoshop in Adobe's weird proprietary JavaScript extension as ported from PHP (god help us all).
#target photoshop
var destHeight = 800
var destWidth = 800
function resize (sourceDoc,dest_w,dest_h) {
var source_w = sourceDoc.width.as("px")
var source_h = sourceDoc.height.as("px")
if (dest_w == 0 && dest_h != 0) {
dest_w = (dest_h/source_h)*source_w;
}
else if (dest_h == 0 && dest_w != 0) {
dest_h = (dest_w/source_w)*source_h;
}
else if (dest_h == 0 && dest_w == 0) {
return sourceDoc;
}
sourceDoc.resizeImage(UnitValue(dest_w,"px"),UnitValue(dest_h,"px"),72,ResampleMethod.BICUBIC)
return sourceDoc
}
function maxsize (sourceDoc,dest_w,dest_h) {
var source_w = sourceDoc.width.as("px")
var source_h = sourceDoc.height.as("px")
if (source_w <= dest_w) {
dest_w = 0;
}
if (source_h <= dest_h) {
dest_h = 0;
}
if (dest_w != 0 && dest_h != 0) {
if ((dest_h/source_h) < (dest_w/source_w)) {
dest_w = 0;
}
else {
dest_h = 0;
}
}
return resize(sourceDoc,dest_w,dest_h);
}
Array.prototype.inArray = function (value) {
var i;
for (i=0; i < this.length; i++) {
if (this[i] === value) {
return true;
}
}
return false;
};
var sourceFolder = Folder.selectDialog("Choose a folder:")
if (sourceFolder != null) {
var sourceFiles = sourceFolder.getFiles()
var exportWebOptions = ExportOptionsSaveForWeb
exportWebOptions.format = SaveDocumentType.JPEG
exportWebOptions.quality = 85
var destFolder = sourceFolder
destFolder.changePath("./processed/")
destFolder.create()
for (i in sourceFiles) {
var extension = sourceFiles[i].name.split(".").pop().toLowerCase()
if (sourceFiles[i].name != "." && sourceFiles[i].name != ".." && (extension == "tiff" || extension == "tif" || extension == "jpg" || extension == "psd")) {
var sourceDoc = app.open(sourceFiles[i])
var sourceHeight = sourceDoc.height.as("px")
var sourceWidth = sourceDoc.width.as("px")
sourceDoc = maxsize(sourceDoc,destWidth,destHeight)
var destFile = File(destFolder.fullName + "/" + sourceFiles[i].name.split(".")[0] + ".jpg")
sourceDoc.exportDocument(destFile,ExportType.SAVEFORWEB,exportWebOptions)
sourceDoc.close(SaveOptions.DONOTSAVECHANGES)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment