-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (118 loc) · 4.91 KB
/
script.js
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
function compressImages() {
const input = document.getElementById('imageInput');
const files = input.files;
const resizePercentage = document.getElementById('resizePercentage').value / 100;
const jpegQuality = document.getElementById('jpegQuality').value / 100;
const imagesContainer = document.getElementById('imagesContainer');
imagesContainer.innerHTML = ''; // Clear previous results
const compressedImages = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const fileContainer = document.createElement('div');
fileContainer.className = 'file-container';
imagesContainer.appendChild(fileContainer);
const imageContainer = document.createElement('div');
imageContainer.className = 'image-container';
fileContainer.appendChild(imageContainer);
// Original image
const originalDiv = createImageBox('Original', e.target.result, file.size / 1024);
imageContainer.appendChild(originalDiv);
// Compress image
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width * resizePercentage;
canvas.height = img.height * resizePercentage;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
const compressedDataUrl = canvas.toDataURL('image/jpeg', jpegQuality);
// Compressed image
const compressedSize = atob(compressedDataUrl.split(',')[1]).length / 1024;
const compressedDiv = createImageBox('Compressed', compressedDataUrl, compressedSize);
imageContainer.appendChild(compressedDiv);
// Store compressed image data
compressedImages.push({
name: `compressed_${file.name}`,
dataUrl: compressedDataUrl
});
// Download button
const downloadBtn = document.createElement('button');
downloadBtn.textContent = 'Download Compressed Image';
downloadBtn.className = 'download-btn';
downloadBtn.onclick = function() {
downloadImage(compressedDataUrl, `compressed_${file.name}`);
};
fileContainer.appendChild(downloadBtn);
// If all images are processed, add the "Download All" button only if more than one image
if (compressedImages.length === files.length && files.length > 1) {
addDownloadAllButton(compressedImages);
}
}
img.src = e.target.result;
}
reader.readAsDataURL(file);
}
// Track compression event
gtag('event', 'compress', {
'event_category': 'Image',
'event_label': 'Compress Images'
});
}
function createImageBox(title, src, size) {
const div = document.createElement('div');
div.className = 'image-box';
div.innerHTML = `
<h3>${title}</h3>
<img src="${src}" alt="${title} image">
<p>Size: ${size.toFixed(2)} KB</p>
`;
return div;
}
function addDownloadAllButton(images) {
const downloadAllBtn = document.createElement('button');
downloadAllBtn.textContent = 'Download All as ZIP';
downloadAllBtn.className = 'download-all-btn';
downloadAllBtn.onclick = function() {
downloadAllImagesAsZip(images);
};
document.getElementById('imagesContainer').prepend(downloadAllBtn);
}
function downloadAllImagesAsZip(images) {
const zip = new JSZip();
const promises = [];
images.forEach(image => {
const promise = fetch(image.dataUrl)
.then(response => response.blob())
.then(blob => {
zip.file(image.name, blob);
});
promises.push(promise);
});
Promise.all(promises).then(() => {
zip.generateAsync({type: "blob"}).then(content => {
const link = document.createElement('a');
link.download = 'compressed_images.zip';
link.href = URL.createObjectURL(content);
link.click();
URL.revokeObjectURL(link.href);
// Track download all as ZIP event
gtag('event', 'download_all_zip', {
'event_category': 'Image',
'event_label': 'Download All Images as ZIP'
});
});
});
}
function downloadImage(dataUrl, fileName) {
const link = document.createElement('a');
link.download = fileName;
link.href = dataUrl;
link.click();
// Track download event
gtag('event', 'download', {
'event_category': 'Image',
'event_label': fileName
});
}