pakdeveloper commited on
Commit
e5660eb
·
verified ·
1 Parent(s): 0664138

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +545 -19
index.html CHANGED
@@ -1,19 +1,545 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ReFixIMG | Online Image Tools (Compress, Resize, Convert)</title>
7
+
8
+ <script src="https://cdn.tailwindcss.com"></script>
9
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
10
+ <style>
11
+ /* Custom styles for a cleaner interface */
12
+ body {
13
+ font-family: 'Inter', sans-serif;
14
+ background-color: #f4f7fa; /* Light background */
15
+ }
16
+ .tool-card {
17
+ transition: all 0.2s ease-in-out;
18
+ cursor: pointer;
19
+ }
20
+ .tool-card:hover {
21
+ transform: translateY(-2px);
22
+ box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
23
+ }
24
+ .active-tool {
25
+ border-color: #ef4444; /* Red border for active tool */
26
+ background-color: #fef2f2; /* Light red background */
27
+ }
28
+ #drop-zone {
29
+ min-height: 250px;
30
+ border: 4px dashed #9ca3af;
31
+ transition: all 0.3s ease;
32
+ /* Added cursor style to indicate interactivity */
33
+ cursor: pointer;
34
+ }
35
+ #drop-zone.drag-over {
36
+ border-color: #ef4444;
37
+ background-color: #fef2f2;
38
+ }
39
+ #preview-canvas {
40
+ max-width: 100%;
41
+ height: auto;
42
+ border-radius: 0.5rem;
43
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
44
+ }
45
+ .btn-primary {
46
+ background-image: linear-gradient(to right, #ef4444, #dc2626);
47
+ transition: transform 0.1s;
48
+ }
49
+ .btn-primary:hover {
50
+ transform: scale(1.02);
51
+ box-shadow: 0 4px 6px -1px rgba(220, 38, 38, 0.4);
52
+ }
53
+ </style>
54
+ </head>
55
+ <body class="p-4 md:p-8">
56
+
57
+
58
+ <header class="text-center mb-10">
59
+ <h1 class="text-5xl font-extrabold text-gray-800">ReFix<span class="text-red-600">IMG</span></h1>
60
+ <p class="text-xl text-gray-500 mt-2">The fastest, free web app for easy image modification.</p>
61
+ </header>
62
+
63
+
64
+ <section class="max-w-7xl mx-auto mb-10">
65
+ <h2 class="text-2xl font-semibold text-gray-700 mb-4">Choose Your Tool</h2>
66
+ <div id="tool-selector" class="grid grid-cols-1 sm:grid-cols-3 gap-6">
67
+
68
+
69
+ <div id="tool-compress" class="tool-card p-6 border-2 border-gray-200 rounded-xl shadow-lg bg-white active-tool" data-tool="compress">
70
+ <svg class="w-8 h-8 text-red-600 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
71
+ <h3 class="font-bold text-lg text-gray-800">Compress IMAGE</h3>
72
+ <p class="text-sm text-gray-500">Reduce JPG, PNG, and WebP file size while maintaining quality.</p>
73
+ </div>
74
+
75
+
76
+ <div id="tool-resize" class="tool-card p-6 border-2 border-gray-200 rounded-xl shadow-lg bg-white" data-tool="resize">
77
+ <svg class="w-8 h-8 text-red-600 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7v10m0 0h4m-4 0l4-4m-4 4l4 4m8-12v10m0 0h4m-4 0l4-4m-4 4l4 4"></path></svg>
78
+ <h3 class="font-bold text-lg text-gray-800">Resize IMAGE</h3>
79
+ <p class="text-sm text-gray-500">Change the width and height of your images in pixels.</p>
80
+ </div>
81
+
82
+
83
+ <div id="tool-convert" class="tool-card p-6 border-2 border-gray-200 rounded-xl shadow-lg bg-white" data-tool="convert">
84
+ <svg class="w-8 h-8 text-red-600 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path></svg>
85
+ <h3 class="font-bold text-lg text-gray-800">Convert IMAGE</h3>
86
+ <p class="text-sm text-gray-500">Convert images to JPG, PNG, or WebP format easily.</p>
87
+ </div>
88
+ </div>
89
+ </section>
90
+
91
+
92
+ <main class="max-w-7xl mx-auto bg-white p-8 rounded-xl shadow-2xl">
93
+
94
+
95
+ <div id="drop-zone" class="flex flex-col items-center justify-center p-8 border-gray-400 rounded-xl text-center mb-8"
96
+ onclick="document.getElementById('file-input').click()">
97
+
98
+ <svg class="w-12 h-12 text-gray-500 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"></path></svg>
99
+ <p class="text-xl text-gray-600 font-semibold mb-3">Drop images here or select them from your device.</p>
100
+ <p class="text-sm text-gray-400 mb-6">Supported formats: JPG, PNG, GIF, WebP (Only the first image will be processed).</p>
101
+ <input type="file" id="file-input" accept="image/*" class="hidden" multiple>
102
+ <button class="btn-primary text-white font-bold py-3 px-8 rounded-lg shadow-md hover:shadow-lg focus:outline-none focus:ring-4 focus:ring-red-500 focus:ring-opacity-50"
103
+ onclick="event.stopPropagation(); document.getElementById('file-input').click()">
104
+
105
+ SELECT IMAGE
106
+ </button>
107
+ </div>
108
+
109
+
110
+ <div id="processing-area" class="hidden grid grid-cols-1 lg:grid-cols-3 gap-8">
111
+
112
+
113
+ <div class="lg:col-span-1 p-6 bg-gray-50 rounded-xl border border-gray-200">
114
+ <h3 class="text-xl font-bold text-gray-700 mb-4">Current Tool Settings</h3>
115
+ <p id="file-info" class="text-sm text-gray-500 mb-4"></p>
116
+
117
+
118
+ <div id="settings-compress" class="tool-settings">
119
+ <label for="quality-slider" class="block text-sm font-medium text-gray-700 mb-2">JPEG/WebP Quality (<span id="quality-value">85</span>%)</label>
120
+ <input type="range" id="quality-slider" min="10" max="100" value="85" step="5" class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer">
121
+
122
+ <label for="compress-format" class="block text-sm font-medium text-gray-700 mt-4 mb-2">Output Format</label>
123
+ <select id="compress-format" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-red-500 focus:border-red-500">
124
+ <option value="image/jpeg">JPG (Best Compression)</option>
125
+ <option value="image/webp">WebP</option>
126
+ <option value="image/png">PNG (Lossless, less compression)</option>
127
+ </select>
128
+ </div>
129
+
130
+
131
+ <div id="settings-resize" class="tool-settings hidden">
132
+ <div class="flex space-x-4 mb-4">
133
+ <div class="flex-1">
134
+ <label for="resize-width" class="block text-sm font-medium text-gray-700 mb-2">Width (px)</label>
135
+ <input type="number" id="resize-width" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-red-500 focus:border-red-500" value="0" min="1">
136
+ </div>
137
+ <div class="flex-1">
138
+ <label for="resize-height" class="block text-sm font-medium text-gray-700 mb-2">Height (px)</label>
139
+ <input type="number" id="resize-height" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-red-500 focus:border-red-500" value="0" min="1">
140
+ </div>
141
+ </div>
142
+ <div class="flex items-center">
143
+ <input type="checkbox" id="aspect-lock" checked class="h-4 w-4 text-red-600 border-gray-300 rounded focus:ring-red-500">
144
+ <label for="aspect-lock" class="ml-2 text-sm text-gray-700">Lock Aspect Ratio</label>
145
+ </div>
146
+ <p class="text-xs text-gray-500 mt-2">Enter 0 in one field to calculate the dimension based on aspect ratio.</p>
147
+ </div>
148
+
149
+
150
+ <div id="settings-convert" class="tool-settings hidden">
151
+ <label for="target-format" class="block text-sm font-medium text-gray-700 mb-2">Target Format</label>
152
+ <select id="target-format" class="w-full p-2 border border-gray-300 rounded-lg focus:ring-red-500 focus:border-red-500">
153
+ <option value="image/jpeg">JPG</option>
154
+ <option value="image/png">PNG</option>
155
+ <option value="image/webp">WebP</option>
156
+ </select>
157
+ </div>
158
+
159
+
160
+ <button id="process-button" class="btn-primary text-white font-bold py-3 w-full rounded-lg shadow-md mt-6" onclick="ImageToolApp.processImage()">
161
+ APPLY CHANGES
162
+ </button>
163
+ </div>
164
+
165
+
166
+ <div class="lg:col-span-2 p-6 bg-gray-50 rounded-xl border border-gray-200">
167
+ <h3 class="text-xl font-bold text-gray-700 mb-4">Preview & Download</h3>
168
+
169
+ <div id="loading-indicator" class="hidden text-center p-10">
170
+ <div class="animate-spin inline-block w-8 h-8 border-4 border-red-500 border-t-transparent rounded-full"></div>
171
+ <p class="mt-3 text-red-500">Processing image...</p>
172
+ </div>
173
+
174
+ <div id="preview-container" class="relative flex justify-center items-center bg-gray-200 p-4 rounded-lg">
175
+ <canvas id="preview-canvas" class="max-h-96"></canvas>
176
+ </div>
177
+
178
+ <!-- UPDATED RESULTS PANEL START -->
179
+ <div id="results-panel" class="hidden mt-6 flex flex-col sm:flex-row items-center justify-between p-4 rounded-xl">
180
+ <!-- Left: Size Information -->
181
+ <div class="text-center sm:text-left mb-4 sm:mb-0">
182
+ <p id="original-size" class="text-base text-gray-600">Original File Size: -</p>
183
+ <p id="new-size" class="text-xl font-bold text-gray-800 mt-1">New Size: -</p>
184
+ </div>
185
+
186
+ <!-- Right: Action Buttons -->
187
+ <div class="flex flex-col sm:flex-row space-y-3 sm:space-y-0 sm:space-x-4 w-full sm:w-auto">
188
+ <button id="download-button" class="btn-primary text-white font-bold py-3 px-6 rounded-lg shadow-md w-full sm:w-auto" onclick="ImageToolApp.downloadImage()">
189
+ DOWNLOAD MODIFIED IMAGE
190
+ </button>
191
+ <button id="new-image-button" class="btn-primary text-white font-bold py-3 px-6 rounded-lg shadow-md w-full sm:w-auto" onclick="ImageToolApp.startNewImage()">
192
+ New Image
193
+ </button>
194
+ </div>
195
+ </div>
196
+ <!-- UPDATED RESULTS PANEL END -->
197
+
198
+ </div>
199
+ </div>
200
+
201
+ </main>
202
+
203
+ <script>
204
+ // Global utility to convert file size in bytes to a human-readable string
205
+ function formatBytes(bytes, decimals = 2) {
206
+ if (bytes === 0) return '0 Bytes';
207
+ const k = 1024;
208
+ const dm = decimals < 0 ? 0 : decimals;
209
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
210
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
211
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
212
+ }
213
+
214
+ // --- ImageToolApp Core Application Logic ---
215
+ const ImageToolApp = {
216
+ activeTool: 'compress',
217
+ originalImage: null, // Stores the original Image object
218
+ originalFile: null, // Stores the original File object (for size info)
219
+ canvas: document.getElementById('preview-canvas'),
220
+ ctx: null,
221
+
222
+ init() {
223
+ this.ctx = this.canvas.getContext('2d');
224
+ this.setupToolSelection();
225
+ this.setupFileInput();
226
+ this.setupDragAndDrop();
227
+ this.setupResizeListeners();
228
+ this.setupCompressListener();
229
+
230
+ // Initial state setup for resize input fields
231
+ document.getElementById('resize-width').value = 0;
232
+ document.getElementById('resize-height').value = 0;
233
+ },
234
+
235
+ setupToolSelection() {
236
+ document.querySelectorAll('.tool-card').forEach(card => {
237
+ card.addEventListener('click', () => {
238
+ this.setActiveTool(card.dataset.tool);
239
+ });
240
+ });
241
+ },
242
+
243
+ setActiveTool(toolName) {
244
+ this.activeTool = toolName;
245
+
246
+ // Update UI for active tool card
247
+ document.querySelectorAll('.tool-card').forEach(card => {
248
+ card.classList.remove('active-tool', 'border-red-600');
249
+ card.classList.add('border-gray-200');
250
+ });
251
+ document.getElementById(`tool-${toolName}`).classList.add('active-tool', 'border-red-600');
252
+
253
+ // Update UI for settings panel
254
+ document.querySelectorAll('.tool-settings').forEach(panel => {
255
+ panel.classList.add('hidden');
256
+ });
257
+ document.getElementById(`settings-${toolName}`).classList.remove('hidden');
258
+
259
+ // If an image is loaded, re-draw it to reset any previous changes
260
+ if (this.originalImage) {
261
+ this.loadImageOnCanvas(this.originalImage);
262
+ document.getElementById('results-panel').classList.add('hidden');
263
+ document.getElementById('process-button').textContent = 'APPLY CHANGES';
264
+ }
265
+ },
266
+
267
+ setupFileInput() {
268
+ const fileInput = document.getElementById('file-input');
269
+ fileInput.addEventListener('change', (event) => {
270
+ if (event.target.files.length > 0) {
271
+ this.handleFile(event.target.files[0]);
272
+ }
273
+ });
274
+ },
275
+
276
+ setupDragAndDrop() {
277
+ const dropZone = document.getElementById('drop-zone');
278
+
279
+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
280
+ dropZone.addEventListener(eventName, (e) => {
281
+ e.preventDefault();
282
+ e.stopPropagation();
283
+ }, false);
284
+ });
285
+
286
+ ['dragenter', 'dragover'].forEach(eventName => {
287
+ dropZone.addEventListener(eventName, () => dropZone.classList.add('drag-over'), false);
288
+ });
289
+
290
+ ['dragleave', 'drop'].forEach(eventName => {
291
+ dropZone.addEventListener(eventName, () => dropZone.classList.remove('drag-over'), false);
292
+ });
293
+
294
+ dropZone.addEventListener('drop', (e) => {
295
+ const files = e.dataTransfer.files;
296
+ if (files.length > 0) {
297
+ this.handleFile(files[0]);
298
+ }
299
+ }, false);
300
+ },
301
+
302
+ setupCompressListener() {
303
+ const qualitySlider = document.getElementById('quality-slider');
304
+ const qualityValue = document.getElementById('quality-value');
305
+ qualitySlider.addEventListener('input', (e) => {
306
+ qualityValue.textContent = e.target.value;
307
+ });
308
+ },
309
+
310
+ setupResizeListeners() {
311
+ const widthInput = document.getElementById('resize-width');
312
+ const heightInput = document.getElementById('resize-height');
313
+ const aspectLock = document.getElementById('aspect-lock');
314
+
315
+ const calculateHeight = (newWidth) => {
316
+ if (this.originalImage && aspectLock.checked && newWidth > 0) {
317
+ return Math.round(newWidth * (this.originalImage.height / this.originalImage.width));
318
+ }
319
+ return 0;
320
+ };
321
+
322
+ const calculateWidth = (newHeight) => {
323
+ if (this.originalImage && aspectLock.checked && newHeight > 0) {
324
+ return Math.round(newHeight * (this.originalImage.width / this.originalImage.height));
325
+ }
326
+ return 0;
327
+ };
328
+
329
+ widthInput.addEventListener('input', () => {
330
+ const w = parseInt(widthInput.value) || 0;
331
+ if (aspectLock.checked) {
332
+ heightInput.value = calculateHeight(w);
333
+ }
334
+ });
335
+
336
+ heightInput.addEventListener('input', () => {
337
+ const h = parseInt(heightInput.value) || 0;
338
+ if (aspectLock.checked) {
339
+ widthInput.value = calculateWidth(h);
340
+ }
341
+ });
342
+ },
343
+
344
+ handleFile(file) {
345
+ if (!file || !file.type.startsWith('image/')) {
346
+ console.error("Not a valid image file.");
347
+ return;
348
+ }
349
+
350
+ this.originalFile = file;
351
+ const reader = new FileReader();
352
+ reader.onload = (e) => {
353
+ const img = new Image();
354
+ img.onload = () => {
355
+ this.originalImage = img;
356
+ this.loadImageOnCanvas(img);
357
+ this.updateFileInfo(file, img);
358
+ document.getElementById('processing-area').classList.remove('hidden');
359
+ document.getElementById('drop-zone').classList.add('hidden');
360
+ document.getElementById('results-panel').classList.add('hidden');
361
+
362
+ // Reset resize inputs to original dimensions
363
+ document.getElementById('resize-width').value = img.width;
364
+ document.getElementById('resize-height').value = img.height;
365
+ };
366
+ img.src = e.target.result;
367
+ };
368
+ reader.readAsDataURL(file);
369
+ },
370
+
371
+ loadImageOnCanvas(img) {
372
+ // Resize canvas to original image dimensions for accurate drawing
373
+ this.canvas.width = img.width;
374
+ this.canvas.height = img.height;
375
+ // Clear and draw the image
376
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
377
+ this.ctx.drawImage(img, 0, 0, img.width, img.height);
378
+ // Set max height for visual preview
379
+ this.canvas.style.maxHeight = '400px';
380
+ this.canvas.style.maxWidth = '100%';
381
+ },
382
+
383
+ updateFileInfo(file, img) {
384
+ const infoText = `File: ${file.name} | Type: ${file.type} | Original Size: ${formatBytes(file.size)} | Dimensions: ${img.width}x${img.height}px`;
385
+ document.getElementById('file-info').textContent = infoText;
386
+ document.getElementById('original-size').textContent = `Original File Size: ${formatBytes(file.size)}`;
387
+ document.getElementById('new-size').textContent = `New Size: Calculating...`;
388
+ },
389
+
390
+ showLoading(isLoading) {
391
+ const loading = document.getElementById('loading-indicator');
392
+ if (isLoading) {
393
+ loading.classList.remove('hidden');
394
+ document.getElementById('process-button').disabled = true;
395
+ document.getElementById('process-button').textContent = 'Processing...';
396
+ } else {
397
+ loading.classList.add('hidden');
398
+ document.getElementById('process-button').disabled = false;
399
+ document.getElementById('process-button').textContent = 'APPLY CHANGES';
400
+ }
401
+ },
402
+
403
+ async processImage() {
404
+ if (!this.originalImage) return;
405
+
406
+ this.showLoading(true);
407
+ document.getElementById('results-panel').classList.add('hidden');
408
+
409
+ // We use a temporary canvas for manipulation to keep the main canvas clean for redrawing
410
+ const tempCanvas = document.createElement('canvas');
411
+ const tempCtx = tempCanvas.getContext('2d');
412
+
413
+ let targetWidth = this.originalImage.width;
414
+ let targetHeight = this.originalImage.height;
415
+ let mimeType = 'image/jpeg';
416
+ let quality = 0.85;
417
+
418
+ // --- 1. Apply Tool-Specific Settings ---
419
+ try {
420
+ if (this.activeTool === 'compress') {
421
+ quality = parseInt(document.getElementById('quality-slider').value) / 100;
422
+ mimeType = document.getElementById('compress-format').value;
423
+ // For compression, dimensions remain original
424
+ tempCanvas.width = this.originalImage.width;
425
+ tempCanvas.height = this.originalImage.height;
426
+ tempCtx.drawImage(this.originalImage, 0, 0);
427
+
428
+ } else if (this.activeTool === 'resize') {
429
+ targetWidth = parseInt(document.getElementById('resize-width').value) || 0;
430
+ targetHeight = parseInt(document.getElementById('resize-height').value) || 0;
431
+
432
+ // Maintain aspect ratio logic if one field is 0
433
+ if (targetWidth === 0 && targetHeight > 0) {
434
+ targetWidth = Math.round(targetHeight * (this.originalImage.width / this.originalImage.height));
435
+ } else if (targetHeight === 0 && targetWidth > 0) {
436
+ targetHeight = Math.round(targetWidth * (this.originalImage.height / this.originalImage.width));
437
+ } else if (targetWidth === 0 && targetHeight === 0) {
438
+ // If both are 0, use original dimensions
439
+ targetWidth = this.originalImage.width;
440
+ targetHeight = this.originalImage.height;
441
+ }
442
+
443
+ if (targetWidth <= 0 || targetHeight <= 0) {
444
+ throw new Error("Invalid resize dimensions.");
445
+ }
446
+
447
+ tempCanvas.width = targetWidth;
448
+ tempCanvas.height = targetHeight;
449
+ tempCtx.drawImage(this.originalImage, 0, 0, targetWidth, targetHeight);
450
+ mimeType = `image/${this.originalFile.type.split('/')[1] || 'png'}`; // Keep original type unless it's a resize-only operation
451
+
452
+ } else if (this.activeTool === 'convert') {
453
+ mimeType = document.getElementById('target-format').value;
454
+ // For conversion, dimensions remain original
455
+ tempCanvas.width = this.originalImage.width;
456
+ tempCanvas.height = this.originalImage.height;
457
+ tempCtx.drawImage(this.originalImage, 0, 0);
458
+ }
459
+ } catch (error) {
460
+ this.showLoading(false);
461
+ console.error("Processing Error:", error.message);
462
+ console.log(`User facing error: ${error.message} Please check your settings.`);
463
+ return;
464
+ }
465
+
466
+ // --- 2. Convert Canvas to Blob/URL and Update UI ---
467
+ tempCanvas.toBlob(async (blob) => {
468
+ if (!blob) {
469
+ this.showLoading(false);
470
+ console.error("Failed to create blob from canvas.");
471
+ return;
472
+ }
473
+
474
+ // Update main preview canvas to show the result
475
+ this.canvas.width = tempCanvas.width;
476
+ this.canvas.height = tempCanvas.height;
477
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
478
+ this.ctx.drawImage(tempCanvas, 0, 0);
479
+
480
+ // Update results panel
481
+ this.downloadBlob = blob;
482
+ this.downloadMimeType = mimeType;
483
+ // Use the original filename as the download name
484
+ this.downloadFileName = this.originalFile.name;
485
+
486
+ document.getElementById('new-size').textContent = `New Size: ${formatBytes(blob.size)}`;
487
+ document.getElementById('results-panel').classList.remove('hidden');
488
+ this.showLoading(false);
489
+
490
+ }, mimeType, quality);
491
+ },
492
+
493
+ // Function to reset the application state and go back to the drop zone
494
+ startNewImage() {
495
+ this.originalImage = null;
496
+ this.originalFile = null;
497
+
498
+ // Reset file input value to allow the same file to be selected again
499
+ document.getElementById('file-input').value = null;
500
+
501
+ // Hide processing area, results, and show drop zone
502
+ document.getElementById('processing-area').classList.add('hidden');
503
+ document.getElementById('drop-zone').classList.remove('hidden');
504
+ document.getElementById('results-panel').classList.add('hidden');
505
+
506
+ // Clear file info
507
+ document.getElementById('file-info').textContent = '';
508
+ document.getElementById('original-size').textContent = 'Original File Size: -';
509
+ document.getElementById('new-size').textContent = 'New Size: -';
510
+
511
+ // Optional: Clear canvas visually (though it will be hidden)
512
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
513
+ },
514
+
515
+ // NOTE: generateFileName is no longer used, but kept for context if needed later.
516
+ generateFileName(mimeType) {
517
+ const parts = this.originalFile.name.split('.');
518
+ const baseName = parts.slice(0, -1).join('.');
519
+ const extension = mimeType.split('/')[1].replace('jpeg', 'jpg');
520
+ return `${baseName}-clonimg.${extension}`;
521
+ },
522
+
523
+ downloadImage() {
524
+ if (!this.downloadBlob) return;
525
+
526
+ const url = URL.createObjectURL(this.downloadBlob);
527
+ const a = document.createElement('a');
528
+ a.href = url;
529
+ // Use the name stored during processing (which is the original file name)
530
+ a.download = this.downloadFileName;
531
+ document.body.appendChild(a);
532
+ a.click();
533
+ document.body.removeChild(a);
534
+ URL.revokeObjectURL(url);
535
+ }
536
+ };
537
+
538
+ // Initialize the app when the window loads
539
+ window.onload = () => {
540
+ ImageToolApp.init();
541
+ };
542
+
543
+ </script>
544
+ </body>
545
+ </html>