Skip to content

Commit 41c4d15

Browse files
authored
Add background color feature to image-resize-quality
https://gist.github.com/simonw/04e9fd78af5f739d97deb69a75c9aa2a
1 parent a124524 commit 41c4d15

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

image-resize-quality.html

+50-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#drop-zone.dragover {
2525
background-color: #e0e0e0;
2626
}
27+
#color-picker-container {
28+
margin: 20px 0;
29+
display: none;
30+
}
2731
#output {
2832
margin-top: 20px;
2933
display: flex;
@@ -46,6 +50,9 @@
4650
.image-info {
4751
margin-top: 10px;
4852
}
53+
.color-picker-label {
54+
margin-right: 10px;
55+
}
4956
</style>
5057
</head>
5158
<body>
@@ -54,12 +61,19 @@ <h1>Image resize and quality comparison</h1>
5461
<p>Drop an image here, click to select, or paste an image</p>
5562
</div>
5663
<input type="file" id="file-input" accept="image/*" style="display: none;">
64+
<div id="color-picker-container">
65+
<label class="color-picker-label" for="background-color">Background Color:</label>
66+
<input type="color" id="background-color" value="#ffffff">
67+
</div>
5768
<div id="output"></div>
5869

5970
<script>
6071
const dropZone = document.getElementById('drop-zone');
6172
const fileInput = document.getElementById('file-input');
6273
const output = document.getElementById('output');
74+
const colorPicker = document.getElementById('background-color');
75+
const colorPickerContainer = document.getElementById('color-picker-container');
76+
let currentImage = null;
6377

6478
dropZone.addEventListener('click', () => fileInput.click());
6579
dropZone.addEventListener('dragover', (e) => {
@@ -69,9 +83,12 @@ <h1>Image resize and quality comparison</h1>
6983
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
7084
dropZone.addEventListener('drop', handleDrop);
7185
fileInput.addEventListener('change', (e) => handleFile(e.target.files[0]));
72-
73-
// Add paste event listener to the document
7486
document.addEventListener('paste', handlePaste);
87+
colorPicker.addEventListener('input', () => {
88+
if (currentImage) {
89+
processImage(currentImage);
90+
}
91+
});
7592

7693
function handleDrop(e) {
7794
e.preventDefault();
@@ -86,7 +103,11 @@ <h1>Image resize and quality comparison</h1>
86103
const reader = new FileReader();
87104
reader.onload = (e) => {
88105
const img = new Image();
89-
img.onload = () => processImage(img);
106+
img.onload = () => {
107+
currentImage = img;
108+
checkTransparency(img);
109+
processImage(img);
110+
};
90111
img.src = e.target.result;
91112
};
92113
reader.readAsDataURL(file);
@@ -103,6 +124,25 @@ <h1>Image resize and quality comparison</h1>
103124
}
104125
}
105126

127+
function checkTransparency(img) {
128+
const canvas = document.createElement('canvas');
129+
canvas.width = img.width;
130+
canvas.height = img.height;
131+
const ctx = canvas.getContext('2d');
132+
ctx.drawImage(img, 0, 0);
133+
134+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
135+
const data = imageData.data;
136+
137+
for (let i = 3; i < data.length; i += 4) {
138+
if (data[i] < 255) {
139+
colorPickerContainer.style.display = 'block';
140+
return;
141+
}
142+
}
143+
colorPickerContainer.style.display = 'none';
144+
}
145+
106146
function processImage(img) {
107147
output.innerHTML = '';
108148
const qualities = [1, 0.9, 0.7, 0.5, 0.3];
@@ -117,6 +157,13 @@ <h1>Image resize and quality comparison</h1>
117157
canvas.width = width;
118158
canvas.height = height;
119159
const ctx = canvas.getContext('2d');
160+
161+
// Fill background if color picker is visible
162+
if (colorPickerContainer.style.display === 'block') {
163+
ctx.fillStyle = colorPicker.value;
164+
ctx.fillRect(0, 0, width, height);
165+
}
166+
120167
ctx.drawImage(img, 0, 0, width, height);
121168

122169
canvas.toBlob(blob => {

0 commit comments

Comments
 (0)