-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
image-resize-quality.html
155 lines (142 loc) · 5.37 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
<!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;
}
#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;
}
</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="output"></div>
<script>
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const output = document.getElementById('output');
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]));
// Add paste event listener to the document
document.addEventListener('paste', handlePaste);
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 = () => 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 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');
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>