-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptimizeMyImages_ServerEditon.jsx
executable file
·104 lines (86 loc) · 2.52 KB
/
optimizeMyImages_ServerEditon.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Resizer script
*
* Copyright (c) Mohammad Jangda ([email protected])
*
*/
#target photoshop
const MAX_WIDTH = 800;
const MAX_HEIGHT = 600;
var FILE_TYPES = [".jpg",".jpeg",".png",".tiff",".tif",".gif"];
openFilePath = prompt("Please enter the location folder to pull images from:", "");
saveFilePath = prompt("Please enter the location folder to save images to:", "");
openFolder = new Folder(openFilePath);
saveFolder = new Folder(saveFilePath);
if(!saveFolder.exists) {
saveFolder.create();
}
if(openFolder!=null && saveFolder!=null) {
iterate(openFolder, saveFolder);
}
function iterate(openPath, savePath) {
var files = openPath.getFiles();
var f;
for (var i=0; i<files.length; i++) {
f = files[i];
if(f instanceof Folder) {
iterate(f, savePath);
} else if (f instanceof File) {
//if proper file type, then open resize and save in new location
if(isAllowedFileType(f)) {
app.open(f);
resizeMyImages(savePath);
}
} else {
alert("Unknown Data Type found. Exiting.")
exit();
}
}
}
function isAllowedFileType(file) {
for (var i=0; i<FILE_TYPES.length; i++) {
var filename = file.name.toLowerCase();
var ext = filename.substr(filename.lastIndexOf('.'));
if(ext == FILE_TYPES[i]) {
return true;
}
}
return false;
}
function resizeMyImages(savePath) {
var docs = app.documents;
var count = docs.length;
var maxWidth = new UnitValue (MAX_WIDTH, "px");
var maxHeight = new UnitValue (MAX_HEIGHT, "px");
for (var i = 0; i < count; i++) {
doc = docs[i];
fname = doc.name;
fname = File(savePath + "/" + fname);
app.activeDocument = doc;
if (doc.width.as("px") < maxWidth.as("px") && doc.height.as("px") < maxHeight.as("px")) {
//No resize needed
} else {
if (doc.width.as("px") > doc.height.as("px")) {
doc.resizeImage(maxWidth);
} else {
ratio = doc.width.as("px") / doc.height.as("px");
width = maxHeight * ratio;
doc.resizeImage(width,maxHeight);
}
}
//ensure RGB mode
if(doc.mode!="RGB") {
doc.changeMode(ChangeMode.RGB);
}
// Save as JPEG
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 8;
app.activeDocument.saveAs(savePath, jpgSaveOptions, false, Extension.LOWERCASE);
}
//Close all open images
while (app.documents.length) {
app.activeDocument.close();
}
}