-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-compressor.html
More file actions
118 lines (103 loc) · 2.8 KB
/
Copy pathimage-compressor.html
File metadata and controls
118 lines (103 loc) · 2.8 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Compressor</title>
<link rel="icon" href="favicon.png">
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f8ff;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
h1 {
color: #007bff;
}
.container {
background: #ffffff;
border-radius: 12px;
box-shadow: 0 0 10px rgba(0, 123, 255, 0.1);
padding: 20px;
max-width: 500px;
width: 100%;
text-align: center;
}
input[type="file"] {
margin: 15px 0;
}
img {
max-width: 100%;
margin-top: 15px;
border-radius: 10px;
}
button {
margin-top: 15px;
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
@media (max-width: 600px) {
.container {
padding: 15px;
}
}
</style>
</head>
<body>
<h1>Image Compressor</h1>
<div class="container">
<input type="file" id="upload" accept="image/*" />
<div id="previewContainer">
<img id="preview" src="" alt="Preview" style="display:none;" />
</div>
<button id="compressBtn" style="display:none;">Compress & Download</button>
</div>
<script>
const upload = document.getElementById('upload');
const preview = document.getElementById('preview');
const compressBtn = document.getElementById('compressBtn');
upload.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
preview.src = event.target.result;
preview.style.display = 'block';
compressBtn.style.display = 'inline-block';
};
reader.readAsDataURL(file);
}
});
compressBtn.addEventListener('click', () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = preview.src;
img.onload = () => {
const scale = 0.5; // Compress to 50% size
canvas.width = img.width * scale;
canvas.height = img.height * scale;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'compressed-image.jpg';
link.click();
}, 'image/jpeg', 0.7); // JPEG format with 70% quality
};
});
</script>
</body>
</html>