-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-cropper.html
More file actions
154 lines (130 loc) · 3.59 KB
/
Copy pathimage-cropper.html
File metadata and controls
154 lines (130 loc) · 3.59 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
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
<!DOCTYPE html>
<html> lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Cropper Tool</title>
<link rel="icon" href="favicon.png">
<style>
body {
font-family: Arial, sans-serif;
background-color: #e6f0ff;
color: #003366;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 2rem auto;
padding: 1rem;
background-color: #ffffff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #007bff;
}
input[type="file"],
select {
display: block;
width: 100%;
margin: 1rem 0;
padding: 0.5rem;
border: 1px solid #ccc;
border-radius: 5px;
}
canvas {
max-width: 100%;
margin-top: 1rem;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
padding: 0.7rem;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
margin-top: 1rem;
}
button:hover {
background-color: #0056b3;
}
a#downloadLink {
display: none;
text-align: center;
background-color: #007bff;
color: white;
padding: 0.7rem;
border: none;
border-radius: 5px;
cursor: pointer;
text-decoration: none;
margin-top: 1rem;
}
a#downloadLink:hover {
background-color: #0056b3;
}
@media (max-width: 600px) {
.container {
margin: 1rem;
padding: 1rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Image Cropper</h1>
<input type="file" id="upload" accept="image/*" />
<select id="aspectRatio">
<option value="1">1:1 (Square)</option>
<option value="16/9">16:9</option>
<option value="4/3">4:3</option>
<option value="3/2">3:2</option>
<option value="21/9">21:9</option>
</select>
<button onclick="resizeImage()">Apply Aspect Ratio</button>
<canvas id="canvas"></canvas>
<a id="downloadLink" download="cropped-image.png">Download Cropped Image</a>
</div>
<script>
let originalImage = new Image();
document.getElementById('upload').addEventListener('change', function(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(e) {
originalImage.src = e.target.result;
originalImage.onload = () => resizeImage();
};
reader.readAsDataURL(file);
});
function resizeImage() {
const aspectValue = document.getElementById('aspectRatio').value;
const aspectRatio = eval(aspectValue);
const originalWidth = originalImage.width;
const originalHeight = originalImage.height;
let newWidth = originalWidth;
let newHeight = Math.round(newWidth / aspectRatio);
if (newHeight > originalHeight) {
newHeight = originalHeight;
newWidth = Math.round(newHeight * aspectRatio);
}
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = newWidth;
canvas.height = newHeight;
ctx.clearRect(0, 0, newWidth, newHeight);
ctx.drawImage(originalImage, 0, 0, newWidth, newHeight);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = canvas.toDataURL();
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Cropped Image';
}
</script>
</body>
</html>