-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare-image-with-padding.jsx
123 lines (99 loc) · 4.4 KB
/
square-image-with-padding.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
#target photoshop
// Function to trim the image based on transparent pixels
function trimTransparentPixels() {
var doc = app.activeDocument;
doc.trim(TrimType.TRANSPARENT, false, true, true, true); // Trims based on transparent pixels
}
function applyBottomPadding(padding){
var doc = app.activeDocument;
var width = doc.width.as('px');
var height = doc.height.as('px');
var newHeight = height + padding;
doc.resizeCanvas(width, newHeight, AnchorPosition.TOPCENTER);
}
// Function to make the canvas square and resize to user-defined size
function makeSquareAndResize(squareSize, sidePadding) {
var doc = app.activeDocument;
// Get the current width and height
var width = doc.width.as('px');
var height = doc.height.as('px');
// Determine the larger dimension
var maxDimension = Math.max(width, height);
// Set the anchor to the bottom center and extend the canvas
doc.resizeCanvas((maxDimension + sidePadding), (maxDimension + sidePadding), AnchorPosition.BOTTOMCENTER);
// Resize the image to the user-defined size
doc.resizeImage(squareSize, squareSize);
}
// Function to save PNG for web
function savePNGForWeb(outputPath) {
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.format = SaveDocumentType.PNG;
saveOptions.PNG8 = false; // False for PNG-24, true for PNG-8
saveOptions.transparency = true;
saveOptions.interlaced = false;
saveOptions.optimized = true;
// Save the file
app.activeDocument.exportDocument(new File(outputPath), ExportType.SAVEFORWEB, saveOptions);
}
// Function to save JPEG for web with adjustable quality
function saveJPEGForWeb(outputPath, quality) {
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.format = SaveDocumentType.JPEG;
saveOptions.quality = quality;
saveOptions.optimized = true;
// Save the file
app.activeDocument.exportDocument(new File(outputPath), ExportType.SAVEFORWEB, saveOptions);
}
// Function to save TIFF
function saveTIFF(outputPath) {
var saveOptions = new TiffSaveOptions();
saveOptions.imageCompression = TIFFEncoding.NONE; // No compression, can be adjusted if needed
saveOptions.layers = false;
// Save the file
app.activeDocument.saveAs(new File(outputPath), saveOptions, true);
}
// Prompt user for square size
var squareSize = parseInt(prompt("Enter the desired square size (default is 1024):", "1024"), 10);
if (isNaN(squareSize) || squareSize <= 0) {
squareSize = 1024; // Default value if input is invalid
}
var bottomPadding = parseInt(prompt("Enter the desired bottom padding (default is 110):", "110"), 10);
if (isNaN(bottomPadding) || bottomPadding <= 0) {
bottomPadding = 110; // Default value if input is invalid
}
var sidePadding = parseInt(prompt("Enter the desired side padding (default is 110):", "120"), 10);
if (isNaN(sidePadding) || sidePadding <= 0) {
sidePadding = 120; // Default value if input is invalid
}
// Open all files in the folder
var inputFolder = Folder.selectDialog("Select the folder containing images to process");
if (inputFolder != null) {
var files = inputFolder.getFiles(/\.(jpg|jpeg|png|tif|tiff)$/i);
// Adjustable JPEG quality variable
var jpegQuality = 85; // Adjust this value as needed
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof File) {
app.open(file);
trimTransparentPixels(); // Trim the transparent pixels before resizing
applyBottomPadding(bottomPadding);
makeSquareAndResize(squareSize, sidePadding);
// Get the file name and create the output path
var fileName = file.name;
var extension = fileName.split('.').pop().toLowerCase();
var outputPath = inputFolder + "/square_" + fileName;
if (extension === 'jpg' || extension === 'jpeg') {
// Save the image as optimized JPEG for web
saveJPEGForWeb(outputPath, jpegQuality);
} else if (extension === 'png') {
// Save the image as optimized PNG for web
savePNGForWeb(outputPath);
} else if (extension === 'tif' || extension === 'tiff') {
// Save the image as TIFF
saveTIFF(outputPath);
}
// Close the document without saving changes
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
}