Image Resizer Tool
body {
font-family: Arial, sans-serif;
background: linear-gradient(120deg, #6a11cb, #2575fc);
color: #fff;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #fff;
color: #333;
border-radius: 10px;
padding: 20px;
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
width: 90%;
max-width: 700px;
text-align: center;
}
h1 {
font-size: 2rem;
margin-bottom: 20px;
color: #6a11cb;
}
.uploader {
margin-bottom: 20px;
}
.upload-btn {
background: #6a11cb;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
}
.upload-btn:hover {
background: #2575fc;
}
#imageInput {
display: none;
}
.image-container {
max-height: 400px;
overflow: hidden;
margin: 20px 0;
}
.image-container img {
max-width: 100%;
display: none;
border: 2px solid #ddd;
border-radius: 10px;
}
.controls {
display: flex;
justify-content: center;
gap: 15px;
}
.btn {
background: #2575fc;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
}
.btn:hover {
background: #6a11cb;
}
const imageInput = document.getElementById('imageInput');
const imageElement = document.getElementById('image');
const cropButton = document.getElementById('cropButton');
const downloadButton = document.getElementById('downloadButton');
let cropper;
// Handle image upload
imageInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
imageElement.src = e.target.result;
imageElement.style.display = 'block';
// Initialize Cropper.js
if (cropper) {
cropper.destroy();
}
cropper = new Cropper(imageElement, {
aspectRatio: 16 / 9,
viewMode: 1,
movable: true,
scalable: true,
zoomable: true,
});
};
reader.readAsDataURL(file);
}
});
// Handle crop and resize
cropButton.addEventListener('click', () => {
if (cropper) {
const canvas = cropper.getCroppedCanvas({
width: 800, // Resized width
height: 450, // Resized height
});
imageElement.src = canvas.toDataURL('image/jpeg');
cropper.destroy();
cropper = null;
}
});
// Handle download
downloadButton.addEventListener('click', () => {
if (imageElement.src) {
const link = document.createElement('a');
link.href = imageElement.src;
link.download = 'resized-image.jpg';
link.click();
}
});