-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage Resizer.html
More file actions
121 lines (110 loc) · 3.18 KB
/
Image Resizer.html
File metadata and controls
121 lines (110 loc) · 3.18 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
119
120
121
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Resizer</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
input[type="file"] {
margin-bottom: 15px;
}
input[type="number"] {
width: 100%;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
display: block;
width: 100%;
padding: 10px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.image-container {
text-align: center;
margin-top: 20px;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>Image Resizer</h1>
<input type="file" id="imageInput" accept="image/*">
<label for="widthInput">Width (px):</label>
<input type="number" id="widthInput" placeholder="Enter width">
<label for="heightInput">Height (px):</label>
<input type="number" id="heightInput" placeholder="Enter height">
<button onclick="resizeImage()">Resize Image</button>
<div class="image-container" id="imageContainer"></div>
</div>
<script>
function resizeImage() {
var input = document.getElementById('imageInput');
var width = document.getElementById('widthInput').value;
var height = document.getElementById('heightInput').value;
if (!input.files || !input.files[0]) {
alert('Please select an image.');
return;
}
var file = input.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var image = new Image();
image.src = e.target.result;
image.onload = function() {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
if (width && height) {
canvas.width = width;
canvas.height = height;
} else if (width) {
canvas.width = width;
canvas.height = (width / image.width) * image.height;
} else if (height) {
canvas.width = (height / image.height) * image.width;
canvas.height = height;
}
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
var resizedImage = canvas.toDataURL('image/jpeg');
var imageContainer = document.getElementById('imageContainer');
imageContainer.innerHTML = '<img src="' + resizedImage + '" alt="Resized Image">';
var downloadLink = document.createElement('a');
downloadLink.href = resizedImage;
downloadLink.download = 'resized_image.jpg';
downloadLink.innerHTML = '<button>Download Resized Image</button>';
imageContainer.appendChild(downloadLink);
};
};
reader.readAsDataURL(file);
}
</script>
</body>
</html>