-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
421 lines (354 loc) · 15.5 KB
/
Copy pathscript.js
File metadata and controls
421 lines (354 loc) · 15.5 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
document.addEventListener('DOMContentLoaded', () => {
// Device dimensions (width × height)
const deviceDimensions = {
iPhone: {
portrait: {
primary: { width: 1242, height: 2688 },
alternative: { width: 1284, height: 2778 }
},
landscape: {
primary: { width: 2688, height: 1242 },
alternative: { width: 2778, height: 1284 }
}
},
iPad: {
portrait: {
primary: { width: 2064, height: 2752 },
alternative: { width: 2048, height: 2732 }
},
landscape: {
primary: { width: 2752, height: 2064 },
alternative: { width: 2732, height: 2048 }
}
},
iPadPro11: {
portrait: {
primary: { width: 1668, height: 2388 },
alternative: { width: 1668, height: 2388 }
},
landscape: {
primary: { width: 2388, height: 1668 },
alternative: { width: 2388, height: 1668 }
}
}
};
// DOM elements
const imageInput = document.getElementById('imageInput');
const uploadSection = document.querySelector('.upload-section');
const iPhoneBtn = document.getElementById('iPhoneBtn');
const iPadBtn = document.getElementById('iPadBtn');
const iPadPro11Btn = document.getElementById('iPadPro11Btn');
const portraitBtn = document.getElementById('portraitBtn');
const landscapeBtn = document.getElementById('landscapeBtn');
const dimensionsDisplay = document.getElementById('dimensionsDisplay');
const originalImageContainer = document.getElementById('originalImageContainer');
const resizedImageContainer = document.getElementById('resizedImageContainer');
const originalSizeEl = document.getElementById('originalSize');
const newSizeEl = document.getElementById('newSize');
const resizeBtn = document.getElementById('resizeBtn');
const downloadBtn = document.getElementById('downloadBtn');
const batchProcessBtn = document.getElementById('batchProcessBtn');
const processingStatusEl = document.getElementById('processingStatus');
const multipleFilesInput = document.getElementById('multipleFilesInput');
const singleModeBtn = document.getElementById('singleModeBtn');
const batchModeBtn = document.getElementById('batchModeBtn');
const singleModeDiv = document.getElementById('singleMode');
const batchModeDiv = document.getElementById('batchMode');
// State
let currentDevice = 'iPhone';
let currentOrientation = 'portrait';
let originalImage = null;
let resizedImage = null;
let batchImages = [];
let processedCount = 0;
let totalImagesToProcess = 0;
let currentMode = 'single'; // 'single' or 'batch'
// Initialize display dimensions
updateDimensionsDisplay();
// Event Listeners
imageInput.addEventListener('change', handleImageSelect);
uploadSection.addEventListener('dragover', handleDragOver);
uploadSection.addEventListener('dragleave', handleDragLeave);
uploadSection.addEventListener('drop', handleDrop);
iPhoneBtn.addEventListener('click', () => switchDevice('iPhone'));
iPadBtn.addEventListener('click', () => switchDevice('iPad'));
iPadPro11Btn.addEventListener('click', () => switchDevice('iPadPro11'));
portraitBtn.addEventListener('click', () => switchOrientation('portrait'));
landscapeBtn.addEventListener('click', () => switchOrientation('landscape'));
resizeBtn.addEventListener('click', resizeImage);
downloadBtn.addEventListener('click', downloadImage);
if (multipleFilesInput) {
multipleFilesInput.addEventListener('change', handleMultipleFiles);
}
if (batchProcessBtn) {
batchProcessBtn.addEventListener('click', processBatchImages);
}
if (singleModeBtn) {
singleModeBtn.addEventListener('click', () => switchMode('single'));
}
if (batchModeBtn) {
batchModeBtn.addEventListener('click', () => switchMode('batch'));
}
// Functions
function switchMode(mode) {
currentMode = mode;
if (mode === 'single') {
updateActiveButton(singleModeBtn, batchModeBtn, true);
singleModeDiv.classList.remove('hidden');
batchModeDiv.classList.add('hidden');
} else {
updateActiveButton(singleModeBtn, batchModeBtn, false);
singleModeDiv.classList.add('hidden');
batchModeDiv.classList.remove('hidden');
}
}
function handleImageSelect(e) {
const file = e.target.files[0];
if (file && file.type.match('image.*')) {
processImageFile(file);
}
}
function handleDragOver(e) {
e.preventDefault();
e.stopPropagation();
uploadSection.classList.add('dragover');
}
function handleDragLeave(e) {
e.preventDefault();
e.stopPropagation();
uploadSection.classList.remove('dragover');
}
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
uploadSection.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file && file.type.match('image.*')) {
imageInput.files = e.dataTransfer.files;
processImageFile(file);
}
}
function handleMultipleFiles(e) {
const files = e.target.files;
if (files.length > 0) {
batchImages = [];
for (let i = 0; i < files.length; i++) {
if (files[i].type.match('image.*')) {
const deviceType = detectDeviceType(files[i].name);
const orientation = detectOrientation(files[i]);
batchImages.push({
file: files[i],
deviceType: deviceType,
orientation: orientation
});
}
}
totalImagesToProcess = batchImages.length;
processingStatusEl.textContent = `Ready to process ${totalImagesToProcess} images`;
batchProcessBtn.disabled = batchImages.length === 0;
}
}
function detectDeviceType(filename) {
// Detect if iPad or iPhone based on filename
if (filename.toLowerCase().includes('ipad')) {
// Detect iPad type (11-inch vs 12.9-inch)
if (filename.toLowerCase().includes('11-inch')) {
return 'iPadPro11';
} else {
return 'iPad';
}
} else {
return 'iPhone';
}
}
function detectOrientation(file) {
// We'll determine orientation after loading the image
// For now, default to portrait
return 'portrait';
}
function processImageFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
originalImage = img;
// Display original image
originalImageContainer.innerHTML = '';
originalImageContainer.appendChild(img.cloneNode());
originalSizeEl.textContent = `Original dimensions: ${img.width} × ${img.height} pixels`;
// Enable resize button
resizeBtn.disabled = false;
// Clear resized image
resizedImageContainer.innerHTML = '';
newSizeEl.textContent = '';
downloadBtn.disabled = true;
// Auto-detect orientation
if (img.width > img.height) {
switchOrientation('landscape');
} else {
switchOrientation('portrait');
}
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function switchDevice(device) {
currentDevice = device;
if (device === 'iPhone') {
updateActiveButton(iPhoneBtn, iPadBtn, true);
iPadPro11Btn.classList.remove('active');
} else if (device === 'iPad') {
updateActiveButton(iPhoneBtn, iPadBtn, false);
iPadPro11Btn.classList.remove('active');
} else if (device === 'iPadPro11') {
iPhoneBtn.classList.remove('active');
iPadBtn.classList.remove('active');
iPadPro11Btn.classList.add('active');
}
updateDimensionsDisplay();
// If an image is loaded, clear the resized version
if (originalImage) {
resizedImageContainer.innerHTML = '';
newSizeEl.textContent = '';
downloadBtn.disabled = true;
}
}
function switchOrientation(orientation) {
currentOrientation = orientation;
updateActiveButton(portraitBtn, landscapeBtn, orientation === 'portrait');
updateDimensionsDisplay();
// If an image is loaded, clear the resized version
if (originalImage) {
resizedImageContainer.innerHTML = '';
newSizeEl.textContent = '';
downloadBtn.disabled = true;
}
}
function updateActiveButton(btn1, btn2, isBtn1Active) {
if (isBtn1Active) {
btn1.classList.add('active');
btn2.classList.remove('active');
} else {
btn1.classList.remove('active');
btn2.classList.add('active');
}
}
function updateDimensionsDisplay() {
const dimensions = deviceDimensions[currentDevice][currentOrientation].primary;
const altDimensions = deviceDimensions[currentDevice][currentOrientation].alternative;
dimensionsDisplay.textContent = `${dimensions.width} × ${dimensions.height}px (or ${altDimensions.width} × ${altDimensions.height}px)`;
}
function resizeImage() {
if (!originalImage) return;
const dimensions = deviceDimensions[currentDevice][currentOrientation].primary;
// Create canvas for resizing
const canvas = document.createElement('canvas');
canvas.width = dimensions.width;
canvas.height = dimensions.height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Calculate scaling and position to maintain aspect ratio
const originalAspect = originalImage.width / originalImage.height;
const targetAspect = dimensions.width / dimensions.height;
let drawWidth, drawHeight, drawX, drawY;
if (originalAspect > targetAspect) {
// Original image is wider than target
drawHeight = dimensions.height;
drawWidth = drawHeight * originalAspect;
drawY = 0;
drawX = (dimensions.width - drawWidth) / 2;
} else {
// Original image is taller than target
drawWidth = dimensions.width;
drawHeight = drawWidth / originalAspect;
drawX = 0;
drawY = (dimensions.height - drawHeight) / 2;
}
// Draw the image on canvas
ctx.drawImage(originalImage, drawX, drawY, drawWidth, drawHeight);
// Convert canvas to image
const resizedImageUrl = canvas.toDataURL('image/jpeg', 0.9);
resizedImage = resizedImageUrl;
// Display resized image
const img = new Image();
img.onload = function() {
resizedImageContainer.innerHTML = '';
resizedImageContainer.appendChild(img);
newSizeEl.textContent = `Resized dimensions: ${dimensions.width} × ${dimensions.height} pixels`;
downloadBtn.disabled = false;
};
img.src = resizedImageUrl;
}
function resizeImageFromFile(file, deviceType, orientation, callback) {
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
const dimensions = deviceDimensions[deviceType][orientation].primary;
const canvas = document.createElement('canvas');
canvas.width = dimensions.width;
canvas.height = dimensions.height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
const originalAspect = img.width / img.height;
const targetAspect = dimensions.width / dimensions.height;
let drawWidth, drawHeight, drawX, drawY;
if (originalAspect > targetAspect) {
drawHeight = dimensions.height;
drawWidth = drawHeight * originalAspect;
drawY = 0;
drawX = (dimensions.width - drawWidth) / 2;
} else {
drawWidth = dimensions.width;
drawHeight = drawWidth / originalAspect;
drawX = 0;
drawY = (dimensions.height - drawHeight) / 2;
}
ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);
const resizedImageUrl = canvas.toDataURL('image/jpeg', 0.9);
callback(resizedImageUrl, file.name);
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
}
function downloadImage() {
if (!resizedImage) return;
const link = document.createElement('a');
link.download = `resized-${currentDevice}-${currentOrientation}.jpg`;
link.href = resizedImage;
link.click();
}
function processBatchImages() {
if (batchImages.length === 0) return;
processedCount = 0;
processingStatusEl.textContent = `Processing images: 0/${totalImagesToProcess}`;
batchImages.forEach((imageInfo, index) => {
// Determine orientation based on image dimensions after loading
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
const actualOrientation = img.width > img.height ? 'landscape' : 'portrait';
resizeImageFromFile(imageInfo.file, imageInfo.deviceType, actualOrientation, (resizedUrl, filename) => {
// Download the resized image
const link = document.createElement('a');
link.download = `resized-${filename}`;
link.href = resizedUrl;
link.click();
processedCount++;
processingStatusEl.textContent = `Processing images: ${processedCount}/${totalImagesToProcess}`;
if (processedCount === totalImagesToProcess) {
processingStatusEl.textContent = `All ${totalImagesToProcess} images processed!`;
}
});
};
img.src = event.target.result;
};
reader.readAsDataURL(imageInfo.file);
});
}
});