-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path[IG]_Resize_Image.jsx
205 lines (176 loc) · 5.81 KB
/
[IG]_Resize_Image.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// **************************** Photoshop Script ************************* //
// *********************************************************************** //
//
// ** [IG]_Resize_Image
// ** @description Scale image down if its maximum height or width is
// bigger than user defined.
// Script will:
// - Preserve image proportions
// - Trim transparent pixels
// - Trim image by top left color
//
// ** @author Igor Grinchesku <igor.grinchesku@gmail.com>
// ** @github https://github.com/Arahnoid/IG-Photoshop-Scripts
// ** @date August 7, 2014
// ** @require Adobe Photoshop CS5, or higher
// ** @instalation https://github.com/Arahnoid/IG-Photoshop-Scripts
//
// *********************************************************************** //
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/[IG]_Resize_Image/Menu=[IG] Resize Image</name>
<category>IG</category>
<enableinfo>true</enableinfo>
<eventid>5be806f7-1621-4e9a-bf3b-9559bf21f1a1</eventid>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
// #target photoshop
app.bringToFront();
// ********************************** START ****************************** //
// Define here maximum Width and Height of image
var maxW = 40, //px
maxH = 50, //px
trimImage = true; // true | false
/** Store Units Settings ===================================================**/
var docUnits;
if (preferences.rulerUnits != Units.PIXELS) {
var startRulerUnits = app.preferences.rulerUnits,
// startTypeUnits = app.preferences.typeUnits,
startDisplayDialogs = app.displayDialogs;
// Set Adobe Photoshop to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS;
// app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;
}
/** End - Store Units Settings ============================================ **/
var doc = app.activeDocument,
file = getFileNameAndExtension(doc.name),
ext = file[1];
/** Trim and resize images ================================================ **/
// convert gif color mode to rgb
if (doc.mode == DocumentMode.INDEXEDCOLOR && ext.toLowerCase() == 'gif') {
doc.changeMode(ChangeMode.RGB);
}
if (trimImage) {
trim();
}
scaleDownW(maxW);
scaleDownH(maxW);
/** Save For Web ========================================================== **/
switch (ext.toLowerCase()) {
case 'png':
{
savePNG(file[0]);
break;
}
case 'jpg':
{
saveJPG(file[0]);
break;
}
case 'gif':
{
saveGIF(file[0]);
break;
}
}
// ********************************** END ******************************** //
/** Restore Units Settings ================================================ **/
if (docUnits !== undefined) {
app.preferences.rulerUnits = startRulerUnits;
// app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;
}
/** End - Restored Units Settings ========================================= **/
///////////////////////////////////////////////////////////////////////////////
// Functions //
///////////////////////////////////////////////////////////////////////////////
function trim() {
try { // Trim transparent pixel
doc.trim(TrimType.TRANSPARENT, true, true, true, true);
} catch (e) {}
try { // Trim by color of top left pixel
doc.trim(TrimType.TOPLEFT, true, true, true, true);
} catch (e) {}
}
function scaleDownH(maxH) {
// Scale down by Height
var dHeith = doc.height.value;
if (dHeith > maxH) {
doc.resizeImage(undefined, maxH, undefined, ResampleMethod.BICUBIC);
}
}
function scaleDownW(maxW) {
// Scale down by Width
var dWidth = doc.width.value;
if (dWidth > maxW) {
doc.resizeImage(maxW, undefined, undefined, ResampleMethod.BICUBIC);
}
}
function getFileNameAndExtension(filename) {
return filename.split('.');
}
function savePNG(filename) {
var path, folder, savefile, opts;
path = doc.path;
folder = Folder(path + '/assets/');
if (!folder.exists) {
folder.create();
}
var saveFile = File(folder + '/' + filename + '.png');
// Options
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
opts.transparency = true;
opts.interlaced = false;
opts.quality = 100;
opts.includeProfile = false;
// Export
doc.exportDocument(saveFile, ExportType.SAVEFORWEB, opts);
// Close document
doc.close(SaveOptions.DONOTSAVECHANGES);
}
function saveGIF(filename) {
var path, folder, savefile, opts;
path = doc.path;
folder = Folder(path + '/assets/');
if (!folder.exists) {
folder.create();
}
var saveFile = File(folder + '/' + filename + '.gif');
// Options
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.COMPUSERVEGIF;
opts.colors = 128;
opts.dither = Dither.PATTERN;
opts.ditherAmount = 100;
opts.interlaced = false;
opts.matte = MatteType.NONE;
opts.palette = Palette.LOCALSELECTIVE;
opts.transparency = true;
// Export
doc.exportDocument(saveFile, ExportType.SAVEFORWEB, opts);
// Close document
doc.close(SaveOptions.DONOTSAVECHANGES);
}
function saveJPG(filename) {
var path, folder, savefile, opts;
path = doc.path;
folder = Folder(path + '/assets/');
if (!folder.exists) {
folder.create();
}
var saveFile = File(folder + '/' + filename + '.jpg');
// Options
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.JPEG;
opts.PNG8 = false;
opts.interlaced = false;
opts.quality = 66;
opts.includeProfile = false;
// Export
doc.exportDocument(saveFile, ExportType.SAVEFORWEB, opts);
// Close document
doc.close(SaveOptions.DONOTSAVECHANGES);
}