Skip to content

Commit 5b41e77

Browse files
authored
Create image-resize-quality.html
Adapted from this Claude artifact https://claude.site/artifacts/45ecf75e-d8e2-4d2a-a3b9-d8c07c7bd757
1 parent 687d450 commit 5b41e77

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

image-resize-quality.html

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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>Image resize and quality comparison</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
max-width: 1200px;
11+
margin: 0 auto;
12+
padding: 20px;
13+
}
14+
#drop-zone {
15+
border: 2px dashed #ccc;
16+
border-radius: 20px;
17+
width: 100%;
18+
height: 200px;
19+
display: flex;
20+
justify-content: center;
21+
align-items: center;
22+
cursor: pointer;
23+
}
24+
#drop-zone.dragover {
25+
background-color: #e0e0e0;
26+
}
27+
#output {
28+
margin-top: 20px;
29+
display: flex;
30+
flex-wrap: wrap;
31+
gap: 20px;
32+
}
33+
.image-container {
34+
text-align: center;
35+
margin-bottom: 20px;
36+
}
37+
.image-container img {
38+
max-width: 80%;
39+
height: auto;
40+
cursor: pointer;
41+
transition: max-width 0.3s ease;
42+
}
43+
.image-container img.full-width {
44+
max-width: unset;
45+
}
46+
.image-info {
47+
margin-top: 10px;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<h1>Image resize and quality comparison</h1>
53+
<div id="drop-zone">
54+
<p>Drop an image here or click to select</p>
55+
</div>
56+
<input type="file" id="file-input" accept="image/*" style="display: none;">
57+
<div id="output"></div>
58+
59+
<script>
60+
const dropZone = document.getElementById('drop-zone');
61+
const fileInput = document.getElementById('file-input');
62+
const output = document.getElementById('output');
63+
64+
dropZone.addEventListener('click', () => fileInput.click());
65+
dropZone.addEventListener('dragover', (e) => {
66+
e.preventDefault();
67+
dropZone.classList.add('dragover');
68+
});
69+
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
70+
dropZone.addEventListener('drop', handleDrop);
71+
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
72+
73+
function handleDrop(e) {
74+
e.preventDefault();
75+
dropZone.classList.remove('dragover');
76+
const file = e.dataTransfer.files[0];
77+
if (file && file.type.startsWith('image/')) {
78+
handleFile(file);
79+
}
80+
}
81+
82+
function handleFile(file) {
83+
const reader = new FileReader();
84+
reader.onload = (e) => {
85+
const img = new Image();
86+
img.onload = () => processImage(img);
87+
img.src = e.target.result;
88+
};
89+
reader.readAsDataURL(file);
90+
}
91+
92+
function processImage(img) {
93+
output.innerHTML = '';
94+
const qualities = [1, 0.9, 0.7, 0.5, 0.3];
95+
const widths = [img.width, img.width / 2];
96+
97+
widths.forEach(width => {
98+
const heightRatio = width / img.width;
99+
const height = img.height * heightRatio;
100+
101+
qualities.forEach(quality => {
102+
const canvas = document.createElement('canvas');
103+
canvas.width = width;
104+
canvas.height = height;
105+
const ctx = canvas.getContext('2d');
106+
ctx.drawImage(img, 0, 0, width, height);
107+
108+
canvas.toBlob(blob => {
109+
const url = URL.createObjectURL(blob);
110+
const container = document.createElement('div');
111+
container.className = 'image-container';
112+
const resultImg = document.createElement('img');
113+
resultImg.src = url;
114+
resultImg.addEventListener('click', () => {
115+
resultImg.classList.toggle('full-width');
116+
});
117+
118+
const infoDiv = document.createElement('div');
119+
infoDiv.className = 'image-info';
120+
infoDiv.innerHTML = `
121+
Width: ${width}px<br>
122+
Quality: ${quality.toFixed(1)}<br>
123+
Size: ${(blob.size / 1024).toFixed(2)} KB
124+
`;
125+
126+
const downloadLink = document.createElement('a');
127+
downloadLink.href = url;
128+
downloadLink.download = `image_w${width}_q${quality.toFixed(1)}.jpg`;
129+
downloadLink.textContent = 'Download';
130+
131+
container.appendChild(resultImg);
132+
container.appendChild(infoDiv);
133+
container.appendChild(downloadLink);
134+
output.appendChild(container);
135+
}, 'image/jpeg', quality);
136+
});
137+
});
138+
}
139+
</script>
140+
</body>
141+
</html>

0 commit comments

Comments
 (0)