-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathimage-resize-quality.html
202 lines (185 loc) · 7.11 KB
/
image-resize-quality.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image resize and quality comparison</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
#drop-zone {
border: 2px dashed #ccc;
border-radius: 20px;
width: 100%;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
#drop-zone.dragover {
background-color: #e0e0e0;
}
#color-picker-container {
margin: 20px 0;
display: none;
}
#output {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.image-container {
text-align: center;
margin-bottom: 20px;
}
.image-container img {
max-width: 80%;
height: auto;
cursor: pointer;
transition: max-width 0.3s ease;
}
.image-container img.full-width {
max-width: unset;
}
.image-info {
margin-top: 10px;
}
.color-picker-label {
margin-right: 10px;
}
</style>
</head>
<body>
<h1>Image resize and quality comparison</h1>
<div id="drop-zone">
<p>Drop an image here, click to select, or paste an image</p>
</div>
<input type="file" id="file-input" accept="image/*" style="display: none;">
<div id="color-picker-container">
<label class="color-picker-label" for="background-color">Background Color:</label>
<input type="color" id="background-color" value="#ffffff">
</div>
<div id="output"></div>
<script>
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const output = document.getElementById('output');
const colorPicker = document.getElementById('background-color');
const colorPickerContainer = document.getElementById('color-picker-container');
let currentImage = null;
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
dropZone.addEventListener('drop', handleDrop);
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
document.addEventListener('paste', handlePaste);
colorPicker.addEventListener('input', () => {
if (currentImage) {
processImage(currentImage);
}
});
function handleDrop(e) {
e.preventDefault();
dropZone.classList.remove('dragover');
const file = e.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
handleFile(file);
}
}
function handleFile(file) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
currentImage = img;
checkTransparency(img);
processImage(img);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
function handlePaste(e) {
const items = e.clipboardData.items;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const blob = items[i].getAsFile();
handleFile(blob);
break;
}
}
}
function checkTransparency(img) {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
for (let i = 3; i < data.length; i += 4) {
if (data[i] < 255) {
colorPickerContainer.style.display = 'block';
return;
}
}
colorPickerContainer.style.display = 'none';
}
function processImage(img) {
output.innerHTML = '';
const qualities = [1, 0.9, 0.7, 0.5, 0.3];
const widths = [img.width, img.width / 2];
widths.forEach(width => {
const heightRatio = width / img.width;
const height = img.height * heightRatio;
qualities.forEach(quality => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// Fill background if color picker is visible
if (colorPickerContainer.style.display === 'block') {
ctx.fillStyle = colorPicker.value;
ctx.fillRect(0, 0, width, height);
}
ctx.drawImage(img, 0, 0, width, height);
canvas.toBlob(blob => {
const url = URL.createObjectURL(blob);
const container = document.createElement('div');
container.className = 'image-container';
const resultImg = document.createElement('img');
resultImg.src = url;
resultImg.addEventListener('click', () => {
resultImg.classList.toggle('full-width');
});
const infoDiv = document.createElement('div');
infoDiv.className = 'image-info';
infoDiv.innerHTML = `
Width: ${width}px<br>
Quality: ${quality.toFixed(1)}<br>
Size: ${(blob.size / 1024).toFixed(2)} KB
`;
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = `image_w${width}_q${quality.toFixed(1)}.jpg`;
downloadLink.textContent = 'Download';
container.appendChild(resultImg);
container.appendChild(infoDiv);
container.appendChild(downloadLink);
output.appendChild(container);
}, 'image/jpeg', quality);
});
});
}
</script>
</body>
</html>