Edit IMG
Image Crop and Watermark
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
margin: 0;
padding: 0;
text-align: center;
}
h1 {
margin-top: 20px;
}
.container {
width: 100%;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.upload-section,
.watermark-section {
margin-bottom: 20px;
}
.upload-section input,
.watermark-section input {
width: 100%;
max-width: 400px;
margin: 10px 0;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
}
#image {
display: block;
max-width: 100%;
height: auto;
margin: 20px auto;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
}
#outputCanvas {
margin-top: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fff;
display: block;
max-width: 100%;
margin: 20px auto;
}
.btn {
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #0056b3;
}
.watermark-preview {
display: block;
max-width: 100px;
max-height: 100px;
}
.size-info {
font-size: 14px;
color: #555;
margin-top: 10px;
}
#compressionMessage {
color: red;
font-size: 16px;
display: none;
margin-top: 10px;
}
Process Image
Download Image
let cropper;
let canvas = document.getElementById('outputCanvas');
let ctx = canvas.getContext('2d');
let imgElement = document.getElementById('image');
let watermarkImageElement = document.getElementById('watermarkImage');
let processButton = document.getElementById('processButton');
let downloadButton = document.getElementById('downloadButton');
let imageUpload = document.getElementById('imageUpload');
let imageWatermarkInput = document.getElementById('imageWatermark');
let imageWatermarkSizeInput = document.getElementById('imageWatermarkSize');
let initialSizeInfo = document.getElementById('initialSize');
let finalSizeInfo = document.getElementById('finalSize');
let compressionMessage = document.getElementById('compressionMessage');
let finalBlob;
// Initialize cropper on page load
window.onload = function () {
imgElement.onload = function () {
cropper = new Cropper(imgElement, {
aspectRatio: 1,
viewMode: 2,
autoCropArea: 0.5,
});
imgElement.style.display = 'block';
};
};
// Update cropper and display initial size when a new image is uploaded
imageUpload.addEventListener('change', function (e) {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = function (event) {
imgElement.src = event.target.result;
// Show initial size in MB
let initialSizeMB = (file.size / 1024 / 1024).toFixed(2);
initialSizeInfo.textContent = `Initial Size: ${initialSizeMB} MB`;
};
reader.readAsDataURL(file);
});
// Load image watermark
imageWatermarkInput.addEventListener('change', function (e) {
let reader = new FileReader();
reader.onload = function (event) {
watermarkImageElement.src = event.target.result;
watermarkImageElement.style.display = 'block';
};
reader.readAsDataURL(e.target.files[0]);
});
// Process image and display final size in MB
processButton.addEventListener('click', function () {
if (cropper) {
let croppedCanvas = cropper.getCroppedCanvas();
let croppedImage = croppedCanvas.toDataURL();
let img = new Image();
img.src = croppedImage;
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
// Image watermark
let imageSize = parseInt(imageWatermarkSizeInput.value) || 20;
let watermarkWidth = img.width * (imageSize / 100);
let watermarkHeight = watermarkWidth * (watermarkImageElement.height / watermarkImageElement.width);
ctx.drawImage(watermarkImageElement, img.width - watermarkWidth - 10, img.height - watermarkHeight - 10, watermarkWidth, watermarkHeight);
// Show final size in MB
canvas.toBlob(function (blob) {
finalBlob = blob;
let finalSizeMB = (blob.size / 1024 / 1024).toFixed(2);
finalSizeInfo.textContent = `Final Size: ${finalSizeMB} MB`;
// Check if image size exceeds 1MB
if (finalSizeMB > 1) {
compressionMessage.style.display = 'none'; // Hide message initially
canvas.toBlob(function (compressedBlob) {
finalBlob = compressedBlob;
let compressedSizeMB = (compressedBlob.size / 1024 / 1024).toFixed(2);
finalSizeInfo.textContent = `Compressed Size: ${compressedSizeMB} MB`;
// Check if compressed image still exceeds 1MB
if (compressedSizeMB > 1) {
compressionMessage.style.display = 'block'; // Show the message if compressed image is more than 1MB
} else {
compressionMessage.style.display = 'none'; // Hide the message if compressed image is below 1MB
}
// Enable download button after compression
downloadButton.style.display = 'inline-block';
}, 'image/jpeg', 0.7); // Compress with 70% quality
} else {
compressionMessage.style.display = 'none'; // Hide compression message if image is already under 1MB
downloadButton.style.display = 'inline-block'; // Show download button
}
// Show download button only after processing
downloadButton.addEventListener('click', function () {
let link = document.createElement('a');
link.href = URL.createObjectURL(finalBlob);
link.download = 'final-image.jpg';
link.click();
});
}, 'image/jpeg');
};
}
});
Image Crop and Watermark
Upload Your Watermark logo
Process Image
Download Image
Image needs compression (more than 1MB).