<!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>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
body {
background: linear-gradient(135deg, #ff9a9e, #fad0c4);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
text-align: center;
padding: 20px;
}
.container {
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 100%;
}
h2 {
color: #ff4081;
margin-bottom: 15px;
}
input, button {
width: 100%;
margin: 10px 0;
padding: 10px;
border: none;
border-radius: 5px;
font-size: 16px;
}
input[type="file"] {
background: #ff4081;
color: white;
cursor: pointer;
}
input[type="number"] {
border: 2px solid #ff4081;
text-align: center;
}
button {
background: #2196F3;
color: white;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #1976D2;
}
canvas {
max-width: 100%;
display: block;
margin: 10px auto;
border: 2px dashed #ff4081;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h2>Image Resizer</h2>
<input type="file" id="imageInput" accept="image/*">
<input type="number" id="widthInput" placeholder="Set Width (px)">
<input type="number" id="heightInput" placeholder="Set Height (px)">
<button onclick="resizeImage()">Resize Image</button>
<canvas id="canvas"></canvas>
<button id="downloadBtn" style="display:none;" onclick="downloadImage()">Download Resized Image</button>
</div>
<script>
let originalImage = new Image();
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
document.getElementById("imageInput").addEventListener("change", function(event) {
let file = event.target.files[0];
if (file) {
let reader = new FileReader();
reader.onload = function(e) {
originalImage.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
originalImage.onload = function() {
canvas.width = originalImage.width;
canvas.height = originalImage.height;
ctx.drawImage(originalImage, 0, 0);
};
function resizeImage() {
let newWidth = document.getElementById("widthInput").value;
let newHeight = document.getElementById("heightInput").value;
if (!newWidth && !newHeight) {
alert("Please enter a width or height.");
return;
}
if (!newWidth) {
newWidth = (originalImage.width * newHeight) / originalImage.height;
}
if (!newHeight) {
newHeight = (originalImage.height * newWidth) / originalImage.width;
}
canvas.width = newWidth;
canvas.height = newHeight;
ctx.drawImage(originalImage, 0, 0, newWidth, newHeight);
document.getElementById("downloadBtn").style.display = "block";
}
function downloadImage() {
let link = document.createElement("a");
link.download = "resized-image.png";
link.href = canvas.toDataURL();
link.click();
}
</script>
</body>
</html>
No comments:
Post a Comment