-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpeg_to_png.html
More file actions
436 lines (375 loc) · 16.6 KB
/
jpeg_to_png.html
File metadata and controls
436 lines (375 loc) · 16.6 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>JPEG to PNG Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<style>
:root { --primary: #3b82f6; --bg: #f8fafc; --surface: #ffffff; --border: #e2e8f0; --text: #1e293b; --radius: 12px; }
body { font-family: 'Inter', system-ui, -apple-system, sans-serif; margin: 0; background: var(--bg); color: var(--text); -webkit-tap-highlight-color: transparent; }
/* Layout */
.container { max-width: 800px; margin: 0 auto; padding: 20px; box-sizing: border-box; }
.header { text-align: center; margin-bottom: 25px; }
h2 { margin: 0; color: var(--primary); font-size: 24px; }
p { color: #64748b; font-size: 14px; margin-top: 5px; }
/* 1. Upload Box */
.upload-area {
border: 2px dashed var(--primary);
background: #eff6ff;
border-radius: var(--radius);
padding: 30px 20px;
text-align: center;
cursor: pointer;
transition: 0.2s;
margin-bottom: 20px;
user-select: none;
}
.upload-area:active { transform: scale(0.98); }
.upload-icon { font-size: 36px; margin-bottom: 10px; display: block; }
.upload-text { font-weight: 600; color: var(--primary); font-size: 16px; }
.upload-sub { font-size: 12px; color: #94a3b8; margin-top:5px; }
input[type="file"] { display: none; }
/* 2. Horizontal Scroll Layout */
.preview-grid {
display: flex;
overflow-x: auto;
gap: 12px;
padding: 5px 2px 15px 2px;
margin-bottom: 20px;
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
min-height: 10px;
}
.preview-grid::-webkit-scrollbar { height: 6px; }
.preview-grid::-webkit-scrollbar-track { background: transparent; }
.preview-grid::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }
.file-card {
flex: 0 0 110px;
width: 110px;
scroll-snap-align: start;
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 10px 5px;
position: relative;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
.file-icon { font-size: 28px; margin-bottom: 5px; color: #94a3b8; }
.file-name {
font-size: 11px; font-weight: 600;
white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
width: 90%; margin-bottom: 2px;
}
.file-size { font-size: 10px; color: #64748b; margin-bottom: 8px; }
.view-btn {
background: transparent; border: 1px solid var(--primary);
color: var(--primary); border-radius: 4px;
padding: 4px 10px; font-size: 10px; cursor: pointer; width: 80%;
}
.remove-btn {
position: absolute; top: -6px; right: -6px;
background: #ef4444; color: white; border: none;
border-radius: 50%; width: 22px; height: 22px;
cursor: pointer; font-size: 14px; font-weight: bold;
display: flex; align-items: center; justify-content: center;
box-shadow: 0 2px 4px rgba(0,0,0,0.1); z-index: 5;
}
/* Info Box */
.info-box { background: var(--surface); padding: 15px; border-radius: var(--radius); box-shadow: 0 2px 5px rgba(0,0,0,0.05); margin-bottom: 20px; text-align: center; color: #64748b; font-size: 14px; }
/* Main Action Button */
.action-btn {
width: 100%; padding: 16px;
background: linear-gradient(135deg, var(--primary), #2563eb);
color: white; border: none; border-radius: 50px;
font-size: 16px; font-weight: 700; cursor: pointer;
box-shadow: 0 4px 10px rgba(59,130,246,0.3);
transition: transform 0.1s;
}
.action-btn:active { transform: scale(0.98); }
/* Modals */
.modal-overlay {
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.85); backdrop-filter: blur(4px);
display: none; align-items: center; justify-content: center; z-index: 100;
}
.modal-overlay.active { display: flex; }
.modal-box {
background: white; width: 90%; max-width: 450px;
border-radius: 16px; padding: 25px; max-height: 85vh;
display: flex; flex-direction: column; position: relative;
}
/* Results List */
.results-list { overflow-y: auto; margin: 15px 0; border-top: 1px solid var(--border); }
.result-item {
display: flex; align-items: center; justify-content: space-between;
border-bottom: 1px solid var(--border); padding: 12px 0; gap: 10px;
}
.result-info { font-size: 13px; flex: 1; overflow: hidden; }
/* NEW: Rename Input Style */
.filename-input {
width: 95%;
padding: 5px;
border: 1px solid #cbd5e1;
border-radius: 4px;
font-size: 13px;
font-family: inherit;
color: #1e293b;
font-weight: 600;
margin-bottom: 4px;
}
.filename-input:focus {
outline: none;
border-color: var(--primary);
box-shadow: 0 0 0 2px rgba(59,130,246,0.1);
}
.size-arrow { color: #94a3b8; margin: 0 4px; }
.size-final { color: #16a34a; font-weight: 700; }
.dl-mini-btn {
background: var(--primary); color: white; border: none;
padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 600; white-space: nowrap;
}
/* Progress Bar */
.progress-container { width: 100%; margin: 15px 0; }
.progress-track { height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); width: 0%; transition: width 0.2s; }
.progress-text { text-align: center; font-size: 12px; color: #64748b; margin-top: 5px; }
/* Image Viewer */
.viewer-wrapper { text-align: center; position: relative; width: 100%; }
.viewer-img { max-width: 100%; max-height: 70vh; border-radius: 8px; box-shadow: 0 5px 20px rgba(0,0,0,0.5); }
.viewer-close {
position: absolute; top: -40px; right: 0; color: white;
font-size: 28px; cursor: pointer; font-weight: bold; background: none; border: none;
}
/* Custom Alert Styles */
.alert-icon { font-size: 40px; margin-bottom: 10px; text-align: center; display: block; }
.alert-btns { display: flex; gap: 10px; margin-top: 20px; justify-content: center; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2>JPEG to PNG Converter</h2>
<p>Convert JPG/JPEG images to PNG format.</p>
</div>
<!-- Upload Area -->
<div class="upload-area" onclick="document.getElementById('fileInput').click()">
<span class="upload-icon">🔁</span>
<div class="upload-text">Tap to Select Images</div>
<div class="upload-sub">Files remain on your device</div>
</div>
<!-- Accepts only JPEG/JPG -->
<input type="file" id="fileInput" multiple accept=".jpg, .jpeg, image/jpeg" onchange="handleSelection(this)">
<!-- Selected Files Horizontal Scroll -->
<div id="previewGrid" class="preview-grid" style="display:none;"></div>
<!-- Info Box -->
<div class="info-box">
Output format: <strong>.png</strong>
</div>
<button class="action-btn" onclick="startConversion()">Convert to PNG</button>
</div>
<!-- Progress Modal -->
<div id="loadingModal" class="modal-overlay">
<div class="modal-box" style="align-items: center; justify-content: center; text-align: center;">
<h3 style="margin:0 0 10px 0;">Converting...</h3>
<div class="progress-container">
<div class="progress-track"><div id="pFill" class="progress-fill"></div></div>
<div id="pText" class="progress-text">0%</div>
</div>
<div style="font-size:12px; color:#94a3b8;">Processing...</div>
</div>
</div>
<!-- Results Modal -->
<div id="resultModal" class="modal-overlay">
<div class="modal-box">
<h3 style="margin:0;">Done!</h3>
<p style="font-size:12px; color:#94a3b8; margin:5px 0 0 0;">You can rename files before downloading.</p>
<div class="results-list" id="resultsContainer"></div>
<button class="action-btn" onclick="closeResults()" style="padding:12px; font-size:14px; background: #64748b; margin-top: auto;">Close</button>
</div>
</div>
<!-- Custom Alert Modal -->
<div id="alertModal" class="modal-overlay">
<div class="modal-box" style="max-width: 320px; text-align: center;">
<div class="alert-icon">⚠️</div>
<h3 id="alertTitle" style="margin:0 0 10px 0;">Notice</h3>
<p id="alertMsg" style="color:#64748b; margin:0; font-size:14px; line-height:1.5;"></p>
<div class="alert-btns">
<button class="action-btn" onclick="closeAlert()" style="padding:10px 25px;">OK</button>
</div>
</div>
</div>
<!-- Image Viewer Modal -->
<div id="imageViewerModal" class="modal-overlay" onclick="closeViewer()">
<div class="viewer-wrapper" onclick="event.stopPropagation()">
<button class="viewer-close" onclick="closeViewer()">×</button>
<img id="viewerImage" class="viewer-img" src="">
<div id="viewerName" style="color:white; margin-top:10px; font-size:14px;"></div>
</div>
</div>
<script>
let selectedFiles = [];
// --- Custom Alert System ---
function showAlert(msg) {
document.getElementById('alertMsg').innerText = msg;
document.getElementById('alertModal').classList.add('active');
}
function closeAlert() {
document.getElementById('alertModal').classList.remove('active');
}
const formatSize = b => {
if(b === 0) return '0 B';
const units = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(b) / Math.log(1024));
return (b / Math.pow(1024, i)).toFixed(2) + ' ' + units[i];
};
// 1. Handle Selection
function handleSelection(input) {
if(input.files.length === 0) return;
const files = Array.from(input.files);
// Filter purely to ensure only images are added if browser check fails
const validFiles = files.filter(f => f.type.startsWith('image/'));
if (validFiles.length < files.length) {
showAlert("Some non-image files were skipped.");
}
selectedFiles = [...selectedFiles, ...validFiles];
renderGrid();
input.value = '';
}
// 2. Render Horizontal Grid
function renderGrid() {
const grid = document.getElementById('previewGrid');
grid.innerHTML = '';
if(selectedFiles.length === 0) {
grid.style.display = 'none';
return;
}
grid.style.display = 'flex';
selectedFiles.forEach((file, index) => {
const div = document.createElement('div');
div.className = 'file-card';
div.innerHTML = `
<button class="remove-btn" onclick="removeFile(${index})">×</button>
<div class="file-icon">📄</div>
<div class="file-name">${file.name}</div>
<div class="file-size">${formatSize(file.size)}</div>
<button class="view-btn" onclick="openViewer(${index})">View</button>
`;
grid.appendChild(div);
});
}
function removeFile(index) {
selectedFiles.splice(index, 1);
renderGrid();
}
function openViewer(index) {
if(index >= selectedFiles.length) return;
const file = selectedFiles[index];
const url = URL.createObjectURL(file);
document.getElementById('viewerImage').src = url;
document.getElementById('viewerName').innerText = file.name;
document.getElementById('imageViewerModal').classList.add('active');
}
function closeViewer() {
document.getElementById('imageViewerModal').classList.remove('active');
setTimeout(() => { document.getElementById('viewerImage').src = ""; }, 200);
}
// 3. Conversion Logic (JPG to PNG)
async function startConversion() {
if(selectedFiles.length === 0) {
showAlert("Please select JPG images first.");
return;
}
document.getElementById('loadingModal').classList.add('active');
const pFill = document.getElementById('pFill');
const pText = document.getElementById('pText');
const results = [];
const total = selectedFiles.length;
for(let i=0; i<total; i++) {
const pct = Math.round(((i) / total) * 100);
pFill.style.width = pct + '%';
pText.innerText = `Converting ${i+1}/${total}`;
await new Promise(r => setTimeout(r, 20));
try {
const res = await convertToPng(selectedFiles[i]);
results.push(res);
} catch(e) {
console.error(e);
}
}
pFill.style.width = '100%';
pText.innerText = 'Finalizing...';
setTimeout(() => {
document.getElementById('loadingModal').classList.remove('active');
showResults(results);
}, 500);
}
function convertToPng(file) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
// Draw image onto canvas
ctx.drawImage(img, 0, 0);
// Convert canvas to PNG blob
canvas.toBlob((blob) => {
if(!blob) { reject("Conversion failed"); return; }
resolve({
originalName: file.name,
originalSize: file.size,
blob: blob
});
}, 'image/png');
};
img.onerror = reject;
img.src = URL.createObjectURL(file);
});
}
function showResults(data) {
const container = document.getElementById('resultsContainer');
container.innerHTML = '';
data.forEach((item) => {
const row = document.createElement('div');
row.className = 'result-item';
// Generate default name (original base + .png)
const lastDotIndex = item.originalName.lastIndexOf('.');
const baseName = lastDotIndex !== -1 ? item.originalName.substring(0, lastDotIndex) : item.originalName;
const defaultName = `${baseName}.png`;
// Insert Input field for renaming
row.innerHTML = `
<div class="result-info">
<input type="text" class="filename-input" value="${defaultName}">
<div style="color:#64748b">${formatSize(item.originalSize)} <span class="size-arrow">➝</span> <span class="size-final">${formatSize(item.blob.size)}</span></div>
</div>
<button class="dl-mini-btn">Download</button>
`;
// Handle Download Click
const inputField = row.querySelector('.filename-input');
const dlBtn = row.querySelector('.dl-mini-btn');
dlBtn.onclick = () => {
let finalName = inputField.value.trim();
// Ensure extension is always .png if user deleted it
if (!finalName.toLowerCase().endsWith('.png')) {
finalName += '.png';
}
saveAs(item.blob, finalName);
};
container.appendChild(row);
});
document.getElementById('resultModal').classList.add('active');
}
function closeResults() {
document.getElementById('resultModal').classList.remove('active');
}
</script>
<script src="tool-ads.js"></script>
</body>
</html>