Upload images to crop, rotate, flip, and adjust dimensions. Perfect for social media profiles and banners.
Drop Image Here
Supports JPG, PNG, WEBP, GIF (Max 10MB)
X: 0Y: 0W: 0pxH: 0pxR: 0°
Aspect Ratio
Preview
// — JAVASCRIPT SEPARATION START —// State
let cropper = null;
let isDarkMode = false;
let flipX = 1;
let flipY = 1;// DOM Elements
const fileInput = document.getElementById(‘fileInput’);
const dropzone = document.getElementById(‘dropzone’);
const uploadView = document.getElementById(‘upload-view’);
const editorView = document.getElementById(‘editor-view’);
const imageElement = document.getElementById(‘image’);
// Data Displays
const dataX = document.getElementById(‘data-x’);
const dataY = document.getElementById(‘data-y’);
const dataWidth = document.getElementById(‘data-width’);
const dataHeight = document.getElementById(‘data-height’);
const dataRotate = document.getElementById(‘data-rotate’);// — Event Listeners —
dropzone.addEventListener(‘click’, () => fileInput.click());
dropzone.addEventListener(‘dragover’, (e) => {
e.preventDefault();
dropzone.classList.add(‘border-green-500’, ‘bg-green-50’, ‘dark:bg-green-900/20’);
});
dropzone.addEventListener(‘dragleave’, () => {
dropzone.classList.remove(‘border-green-500’, ‘bg-green-50’, ‘dark:bg-green-900/20’);
});
dropzone.addEventListener(‘drop’, (e) => {
e.preventDefault();
dropzone.classList.remove(‘border-green-500’, ‘bg-green-50’, ‘dark:bg-green-900/20’);
if(e.dataTransfer.files.length) handleFile(e.dataTransfer.files[0]);
});
fileInput.addEventListener(‘change’, (e) => {
if(e.target.files.length) handleFile(e.target.files[0]);
});// — Core Functions —function handleFile(file) {
if (!file || !file.type.startsWith(‘image/’)) {
alert(‘Please upload a valid image file.’);
return;
}const reader = new FileReader();
reader.onload = (e) => {
imageElement.src = e.target.result;
// Switch Views
uploadView.classList.add(‘hidden’);
editorView.classList.remove(‘hidden’);
// Init Cropper (Needs small delay for DOM to render image)
setTimeout(initCropper, 100);
};
reader.readAsDataURL(file);
}function initCropper() {
if(cropper) cropper.destroy();cropper = new Cropper(imageElement, {
viewMode: 1, // Restrict crop box to canvas size
dragMode: ‘move’,
autoCropArea: 0.8,
restore: false,
guides: true,
center: true,
highlight: false,
cropBoxMovable: true,
cropBoxResizable: true,
toggleDragModeOnDblclick: false,
preview: ‘.preview-lg’, // Preview selector
crop(event) {
// Update Info Bar
dataX.innerText = `X: ${Math.round(event.detail.x)}`;
dataY.innerText = `Y: ${Math.round(event.detail.y)}`;
dataWidth.innerText = `W: ${Math.round(event.detail.width)}px`;
dataHeight.innerText = `H: ${Math.round(event.detail.height)}px`;
dataRotate.innerText = `R: ${Math.round(event.detail.rotate)}°`;
},
});
}function setMode(mode) {
if(!cropper) return;
cropper.setDragMode(mode);
// UI Toggle
document.querySelectorAll(‘.tool-btn’).forEach(btn => btn.classList.remove(‘active’, ‘text-green-600’, ‘border-green-500’));
const btn = document.querySelector(`button[onclick=”setMode(‘${mode}’)”]`);
if(btn) btn.classList.add(‘active’, ‘text-green-600’, ‘border-green-500’);
}function setAspectRatio(ratio) {
if(!cropper) return;
cropper.setAspectRatio(ratio);// UI Toggle
document.querySelectorAll(‘.ratio-btn’).forEach(btn => {
btn.classList.remove(‘active’, ‘border-green-500’, ‘bg-green-50’, ‘text-green-700’);
btn.classList.add(‘text-gray-600’, ‘dark:text-gray-300’);
});
// Find the clicked button (event target wrapper logic needed if cleaner code, but we can rely on active class for now)
// Or just check which button triggered this. For simplicity in this vanilla JS structure:
const btns = document.querySelectorAll(‘.ratio-btn’);
// This is a bit lazy matching, ideally pass `this`
// Let’s rely on user click state which we don’t capture here easily without `this`.
// Instead, manual update:
// (We would need to pass ‘this’ in onclick to do it cleanly, but keeping code simple)
}
// Monkey patch for button active states
document.querySelectorAll(‘.ratio-btn’).forEach(btn => {
btn.addEventListener(‘click’, function() {
document.querySelectorAll(‘.ratio-btn’).forEach(b => {
b.classList.remove(‘active’, ‘border-green-500’, ‘bg-green-50’, ‘text-green-700’);
b.classList.add(‘text-gray-600’, ‘dark:text-gray-300’);
});
this.classList.add(‘active’, ‘border-green-500’, ‘bg-green-50’, ‘text-green-700’);
this.classList.remove(‘text-gray-600’, ‘dark:text-gray-300’);
});
});function rotate(deg) {
if(!cropper) return;
cropper.rotate(deg);
}function flip(dir) {
if(!cropper) return;
if(dir === ‘h’) {
flipX = -flipX;
cropper.scaleX(flipX);
} else {
flipY = -flipY;
cropper.scaleY(flipY);
}
}function zoom(ratio) {
if(!cropper) return;
cropper.zoom(ratio);
}function resetEditor() {
if(!cropper) return;
cropper.reset();
flipX = 1;
flipY = 1;
}function downloadImage() {
if(!cropper) return;
// Get Canvas
const canvas = cropper.getCroppedCanvas({
maxWidth: 4096,
maxHeight: 4096,
fillColor: ‘#fff’, // White background for JPG if transparency exists
imageSmoothingEnabled: true,
imageSmoothingQuality: ‘high’,
});if(!canvas) {
alert(‘Could not crop image. Check limits.’);
return;
}// Convert to Blob and Download
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob);
const a = document.createElement(‘a’);
a.href = url;
a.download = ‘cropped-image.png’; // Default to PNG for quality
a.click();
URL.revokeObjectURL(url);
}, ‘image/png’);
}function closeEditor() {
if(cropper) {
cropper.destroy();
cropper = null;
}
imageElement.src = ”;
uploadView.classList.remove(‘hidden’);
editorView.classList.add(‘hidden’);
fileInput.value = ”;
}// — Dark Mode —
function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
if(isDarkMode) {
icon.classList.remove(‘fa-moon’);
icon.classList.add(‘fa-sun’);
} else {
icon.classList.remove(‘fa-sun’);
icon.classList.add(‘fa-moon’);
}
}
// — JAVASCRIPT SEPARATION END —
Using Image Cropper
The Image Cropper utility is designed for image and graphics tasks. Check the on-page controls for available formats, limits and options. Always review the output for accuracy before sharing or publishing it.
Practical tips
Keep your original files or source information as a backup.
Choose suitable options using the controls provided by this tool.
Check the generated result and retry with different settings if needed.