Image Resizer Tool
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.container {
width: 90%;
max-width: 600px;
background-color: white;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
text-align: center;
}
h1 {
margin-bottom: 20px;
color: #333;
}
input[type="file"] {
margin: 10px 0;
}
.resize-options {
display: flex;
justify-content: space-between;
margin: 10px 0;
}
input[type="number"] {
width: 45%;
padding: 10px;
font-size: 16px;
border-radius: 5px;
border: 1px solid #ddd;
}
canvas {
display: none;
margin: 20px 0;
border: 1px solid #ddd;
}
.download-button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: white;
text-decoration: none;
cursor: pointer;
display: none;
margin-top: 20px;
}
.download-button:hover {
background-color: #45a049;
}
let img = new Image();
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
function loadImage(event) {
let reader = new FileReader();
reader.onload = function() {
img.src = reader.result;
img.onload = function() {
document.getElementById('width').value = img.width;
document.getElementById('height').value = img.height;
resizeImage();
}
}
reader.readAsDataURL(event.target.files[0]);
}
function resizeImage() {
let width = document.getElementById('width').value;
let height = document.getElementById('height').value;
if (width && !height) {
height = img.height * (width / img.width);
document.getElementById('height').value = Math.round(height);
} else if (!width && height) {
width = img.width * (height / img.height);
document.getElementById('width').value = Math.round(width);
}
canvas.width = width;
canvas.height = height;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, width, height);
document.getElementById('downloadLink').href = canvas.toDataURL('image/png');
document.getElementById('downloadLink').style.display = 'inline-block';
canvas.style.display = 'block';
}
No comments:
Post a Comment