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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
<!DOCTYPE html>
|
| 3 |
+
<html lang="en">
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8">
|
| 6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 7 |
+
<title>Image Cropper with Rounded Corners</title>
|
| 8 |
+
<style>
|
| 9 |
+
body {
|
| 10 |
+
font-family: Arial, sans-serif;
|
| 11 |
+
max-width: 800px;
|
| 12 |
+
margin: 0 auto;
|
| 13 |
+
padding: 20px;
|
| 14 |
+
background-color: #f5f5f5;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
.container {
|
| 18 |
+
background-color: white;
|
| 19 |
+
padding: 20px;
|
| 20 |
+
border-radius: 8px;
|
| 21 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
h1 {
|
| 25 |
+
text-align: center;
|
| 26 |
+
color: #333;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
.upload-section {
|
| 30 |
+
text-align: center;
|
| 31 |
+
margin-bottom: 20px;
|
| 32 |
+
padding: 20px;
|
| 33 |
+
border: 2px dashed #ccc;
|
| 34 |
+
border-radius: 5px;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
.preview-container {
|
| 38 |
+
position: relative;
|
| 39 |
+
margin: 20px auto;
|
| 40 |
+
max-width: 100%;
|
| 41 |
+
overflow: hidden;
|
| 42 |
+
border: 1px solid #ddd;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
#image-preview {
|
| 46 |
+
display: block;
|
| 47 |
+
max-width: 100%;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
.crop-box {
|
| 51 |
+
position: absolute;
|
| 52 |
+
border: 2px dashed rgba(255, 255, 255, 0.8);
|
| 53 |
+
box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
|
| 54 |
+
cursor: move;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.controls {
|
| 58 |
+
margin-top: 20px;
|
| 59 |
+
display: flex;
|
| 60 |
+
flex-wrap: wrap;
|
| 61 |
+
gap: 15px;
|
| 62 |
+
justify-content: center;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.control-group {
|
| 66 |
+
display: flex;
|
| 67 |
+
flex-direction: column;
|
| 68 |
+
min-width: 200px;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
label {
|
| 72 |
+
margin-bottom: 5px;
|
| 73 |
+
font-weight: bold;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
input[type="range"], input[type="number"] {
|
| 77 |
+
padding: 8px;
|
| 78 |
+
border: 1px solid #ddd;
|
| 79 |
+
border-radius: 4px;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
button {
|
| 83 |
+
background-color: #4CAF50;
|
| 84 |
+
color: white;
|
| 85 |
+
border: none;
|
| 86 |
+
padding: 10px 15px;
|
| 87 |
+
border-radius: 4px;
|
| 88 |
+
cursor: pointer;
|
| 89 |
+
font-size: 16px;
|
| 90 |
+
transition: background-color 0.3s;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
button:hover {
|
| 94 |
+
background-color: #45a049;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.result {
|
| 98 |
+
margin-top: 20px;
|
| 99 |
+
text-align: center;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
#cropped-result {
|
| 103 |
+
max-width: 100%;
|
| 104 |
+
border: 1px solid #ddd;
|
| 105 |
+
margin-top: 10px;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
.resize-handle {
|
| 109 |
+
position: absolute;
|
| 110 |
+
width: 10px;
|
| 111 |
+
height: 10px;
|
| 112 |
+
background-color: white;
|
| 113 |
+
border: 1px solid #333;
|
| 114 |
+
z-index: 10;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
.resize-handle-nw {
|
| 118 |
+
top: -5px;
|
| 119 |
+
left: -5px;
|
| 120 |
+
cursor: nw-resize;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
.resize-handle-ne {
|
| 124 |
+
top: -5px;
|
| 125 |
+
right: -5px;
|
| 126 |
+
cursor: ne-resize;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.resize-handle-sw {
|
| 130 |
+
bottom: -5px;
|
| 131 |
+
left: -5px;
|
| 132 |
+
cursor: sw-resize;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
.resize-handle-se {
|
| 136 |
+
bottom: -5px;
|
| 137 |
+
right: -5px;
|
| 138 |
+
cursor: se-resize;
|
| 139 |
+
}
|
| 140 |
+
</style>
|
| 141 |
+
</head>
|
| 142 |
+
<body>
|
| 143 |
+
<div class="container">
|
| 144 |
+
<h1>Image Cropper with Rounded Corners</h1>
|
| 145 |
+
|
| 146 |
+
<div class="upload-section">
|
| 147 |
+
<input type="file" id="image-upload" accept="image/*">
|
| 148 |
+
<p>or drag and drop image here</p>
|
| 149 |
+
</div>
|
| 150 |
+
|
| 151 |
+
<div class="preview-container" id="preview-container" style="display: none;">
|
| 152 |
+
<img id="image-preview">
|
| 153 |
+
<div class="crop-box" id="crop-box">
|
| 154 |
+
<div class="resize-handle resize-handle-nw"></div>
|
| 155 |
+
<div class="resize-handle resize-handle-ne"></div>
|
| 156 |
+
<div class="resize-handle resize-handle-sw"></div>
|
| 157 |
+
<div class="resize-handle resize-handle-se"></div>
|
| 158 |
+
</div>
|
| 159 |
+
</div>
|
| 160 |
+
|
| 161 |
+
<div class="controls">
|
| 162 |
+
<div class="control-group">
|
| 163 |
+
<label for="edge-reduction">Edge Reduction (%)</label>
|
| 164 |
+
<input type="range" id="edge-reduction" min="0" max="50" value="0" step="1">
|
| 165 |
+
<input type="number" id="edge-reduction-value" min="0" max="50" value="0" step="1">
|
| 166 |
+
</div>
|
| 167 |
+
|
| 168 |
+
<div class="control-group">
|
| 169 |
+
<label for="corner-radius">Corner Radius (%)</label>
|
| 170 |
+
<input type="range" id="corner-radius" min="0" max="50" value="0" step="1">
|
| 171 |
+
<input type="number" id="corner-radius-value" min="0" max="50" value="0" step="1">
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
<div class="control-group">
|
| 175 |
+
<label for="aspect-ratio">Aspect Ratio</label>
|
| 176 |
+
<select id="aspect-ratio">
|
| 177 |
+
<option value="free">Free</option>
|
| 178 |
+
<option value="1">1:1 (Square)</option>
|
| 179 |
+
<option value="4/3">4:3</option>
|
| 180 |
+
<option value="16/9">16:9</option>
|
| 181 |
+
<option value="3/2">3:2</option>
|
| 182 |
+
<option value="custom">Custom</option>
|
| 183 |
+
</select>
|
| 184 |
+
</div>
|
| 185 |
+
|
| 186 |
+
<div class="control-group" id="custom-aspect-group" style="display: none;">
|
| 187 |
+
<label for="custom-aspect-width">Custom Ratio</label>
|
| 188 |
+
<div style="display: flex; gap: 5px;">
|
| 189 |
+
<input type="number" id="custom-aspect-width" min="1" value="1" style="width: 50px;">
|
| 190 |
+
<span>:</span>
|
| 191 |
+
<input type="number" id="custom-aspect-height" min="1" value="1" style="width: 50px;">
|
| 192 |
+
</div>
|
| 193 |
+
</div>
|
| 194 |
+
</div>
|
| 195 |
+
|
| 196 |
+
<div class="controls" style="justify-content: center; margin-top: 10px;">
|
| 197 |
+
<button id="crop-button">Crop Image</button>
|
| 198 |
+
<button id="download-button" disabled>Download Cropped Image</button>
|
| 199 |
+
</div>
|
| 200 |
+
|
| 201 |
+
<div class="result" id="result" style="display: none;">
|
| 202 |
+
<h3>Cropped Result</h3>
|
| 203 |
+
<img id="cropped-result">
|
| 204 |
+
</div>
|
| 205 |
+
</div>
|
| 206 |
+
|
| 207 |
+
<script>
|
| 208 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 209 |
+
// DOM elements
|
| 210 |
+
const imageUpload = document.getElementById('image-upload');
|
| 211 |
+
const previewContainer = document.getElementById('preview-container');
|
| 212 |
+
const imagePreview = document.getElementById('image-preview');
|
| 213 |
+
const cropBox = document.getElementById('crop-box');
|
| 214 |
+
const edgeReduction = document.getElementById('edge-reduction');
|
| 215 |
+
const edgeReductionValue = document.getElementById('edge-reduction-value');
|
| 216 |
+
const cornerRadius = document.getElementById('corner-radius');
|
| 217 |
+
const cornerRadiusValue = document.getElementById('corner-radius-value');
|
| 218 |
+
const aspectRatio = document.getElementById('aspect-ratio');
|
| 219 |
+
const customAspectGroup = document.getElementById('custom-aspect-group');
|
| 220 |
+
const customAspectWidth = document.getElementById('custom-aspect-width');
|
| 221 |
+
const customAspectHeight = document.getElementById('custom-aspect-height');
|
| 222 |
+
const cropButton = document.getElementById('crop-button');
|
| 223 |
+
const downloadButton = document.getElementById('download-button');
|
| 224 |
+
const result = document.getElementById('result');
|
| 225 |
+
const croppedResult = document.getElementById('cropped-result');
|
| 226 |
+
|
| 227 |
+
// Variables
|
| 228 |
+
let imageLoaded = false;
|
| 229 |
+
let currentImage = null;
|
| 230 |
+
let cropBoxX = 0;
|
| 231 |
+
let cropBoxY = 0;
|
| 232 |
+
let cropBoxWidth = 0;
|
| 233 |
+
let cropBoxHeight = 0;
|
| 234 |
+
let isDragging = false;
|
| 235 |
+
let isResizing = false;
|
| 236 |
+
let resizeHandle = null;
|
| 237 |
+
let startX, startY;
|
| 238 |
+
let startWidth, startHeight;
|
| 239 |
+
let startLeft, startTop;
|
| 240 |
+
let currentAspectRatio = null;
|
| 241 |
+
|
| 242 |
+
// Event listeners
|
| 243 |
+
imageUpload.addEventListener('change', handleImageUpload);
|
| 244 |
+
edgeReduction.addEventListener('input', updateEdgeReductionValue);
|
| 245 |
+
edgeReductionValue.addEventListener('input', updateEdgeReductionSlider);
|
| 246 |
+
cornerRadius.addEventListener('input', updateCornerRadiusValue);
|
| 247 |
+
cornerRadiusValue.addEventListener('input', updateCornerRadiusSlider);
|
| 248 |
+
aspectRatio.addEventListener('change', updateAspectRatio);
|
| 249 |
+
customAspectWidth.addEventListener('input', updateCustomAspectRatio);
|
| 250 |
+
customAspectHeight.addEventListener('input', updateCustomAspectRatio);
|
| 251 |
+
cropButton.addEventListener('click', cropImage);
|
| 252 |
+
downloadButton.addEventListener('click', downloadCroppedImage);
|
| 253 |
+
|
| 254 |
+
// Drag and drop
|
| 255 |
+
const uploadSection = document.querySelector('.upload-section');
|
| 256 |
+
|
| 257 |
+
uploadSection.addEventListener('dragover', (e) => {
|
| 258 |
+
e.preventDefault();
|
| 259 |
+
uploadSection.style.borderColor = '#4CAF50';
|
| 260 |
+
uploadSection.style.backgroundColor = '#f0fff0';
|
| 261 |
+
});
|
| 262 |
+
|
| 263 |
+
uploadSection.addEventListener('dragleave', () => {
|
| 264 |
+
uploadSection.style.borderColor = '#ccc';
|
| 265 |
+
uploadSection.style.backgroundColor = '';
|
| 266 |
+
});
|
| 267 |
+
|
| 268 |
+
uploadSection.addEventListener('drop', (e) => {
|
| 269 |
+
e.preventDefault();
|
| 270 |
+
uploadSection.style.borderColor = '#ccc';
|
| 271 |
+
uploadSection.style.backgroundColor = '';
|
| 272 |
+
|
| 273 |
+
if (e.dataTransfer.files.length) {
|
| 274 |
+
imageUpload.files = e.dataTransfer.files;
|
| 275 |
+
handleImageUpload();
|
| 276 |
+
}
|
| 277 |
+
});
|
| 278 |
+
|
| 279 |
+
// Crop box interaction
|
| 280 |
+
cropBox.addEventListener('mousedown', startDrag);
|
| 281 |
+
document.addEventListener('mousemove', handleDrag);
|
| 282 |
+
document.addEventListener('mouseup', stopDrag);
|
| 283 |
+
|
| 284 |
+
// Resize handles
|
| 285 |
+
const resizeHandles = document.querySelectorAll('.resize-handle');
|
| 286 |
+
resizeHandles.forEach(handle => {
|
| 287 |
+
handle.addEventListener('mousedown', startResize);
|
| 288 |
+
});
|
| 289 |
+
|
| 290 |
+
// Functions
|
| 291 |
+
function handleImageUpload() {
|
| 292 |
+
const file = imageUpload.files[0];
|
| 293 |
+
if (!file) return;
|
| 294 |
+
|
| 295 |
+
const reader = new FileReader();
|
| 296 |
+
reader.onload = function(e) {
|
| 297 |
+
imagePreview.src = e.target.result;
|
| 298 |
+
currentImage = new Image();
|
| 299 |
+
currentImage.onload = function() {
|
| 300 |
+
imageLoaded = true;
|
| 301 |
+
previewContainer.style.display = 'block';
|
| 302 |
+
initializeCropBox();
|
| 303 |
+
};
|
| 304 |
+
currentImage.src = e.target.result;
|
| 305 |
+
};
|
| 306 |
+
reader.readAsDataURL(file);
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
function initializeCropBox() {
|
| 310 |
+
const containerWidth = previewContainer.offsetWidth;
|
| 311 |
+
const containerHeight = previewContainer.offsetHeight;
|
| 312 |
+
|
| 313 |
+
// Set initial crop box size (80% of the smaller dimension)
|
| 314 |
+
const size = Math.min(containerWidth, containerHeight) * 0.8;
|
| 315 |
+
|
| 316 |
+
cropBoxWidth = size;
|
| 317 |
+
cropBoxHeight = size;
|
| 318 |
+
|
| 319 |
+
// Center the crop box
|
| 320 |
+
cropBoxX = (containerWidth - cropBoxWidth) / 2;
|
| 321 |
+
cropBoxY = (containerHeight - cropBoxHeight) / 2;
|
| 322 |
+
|
| 323 |
+
updateCropBox();
|
| 324 |
+
updateAspectRatio();
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
function updateCropBox() {
|
| 328 |
+
cropBox.style.width = `${cropBoxWidth}px`;
|
| 329 |
+
cropBox.style.height = `${cropBoxHeight}px`;
|
| 330 |
+
cropBox.style.left = `${cropBoxX}px`;
|
| 331 |
+
cropBox.style.top = `${cropBoxY}px`;
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
function startDrag(e) {
|
| 335 |
+
if (e.target.classList.contains('resize-handle')) return;
|
| 336 |
+
|
| 337 |
+
isDragging = true;
|
| 338 |
+
startX = e.clientX;
|
| 339 |
+
startY = e.clientY;
|
| 340 |
+
startLeft = cropBoxX;
|
| 341 |
+
startTop = cropBoxY;
|
| 342 |
+
e.preventDefault();
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
function handleDrag(e) {
|
| 346 |
+
if (isDragging) {
|
| 347 |
+
const dx = e.clientX - startX;
|
| 348 |
+
const dy = e.clientY - startY;
|
| 349 |
+
|
| 350 |
+
// Calculate new position with boundary checks
|
| 351 |
+
const newLeft = Math.max(0, Math.min(startLeft + dx, previewContainer.offsetWidth - cropBoxWidth));
|
| 352 |
+
const newTop = Math.max(0, Math.min(startTop + dy, previewContainer.offsetHeight - cropBoxHeight));
|
| 353 |
+
|
| 354 |
+
cropBoxX = newLeft;
|
| 355 |
+
cropBoxY = newTop;
|
| 356 |
+
updateCropBox();
|
| 357 |
+
} else if (isResizing) {
|
| 358 |
+
const dx = e.clientX - startX;
|
| 359 |
+
const dy = e.clientY - startY;
|
| 360 |
+
|
| 361 |
+
let newWidth = startWidth;
|
| 362 |
+
let newHeight = startHeight;
|
| 363 |
+
|
| 364 |
+
// Calculate new dimensions based on which handle is being used
|
| 365 |
+
if (resizeHandle.classList.contains('resize-handle-nw')) {
|
| 366 |
+
newWidth = Math.max(50, startWidth - dx);
|
| 367 |
+
newHeight = currentAspectRatio ? newWidth / currentAspectRatio : startHeight - dy;
|
| 368 |
+
if (currentAspectRatio) {
|
| 369 |
+
newHeight = newWidth / currentAspectRatio;
|
| 370 |
+
cropBoxX = startLeft + (startWidth - newWidth);
|
| 371 |
+
cropBoxY = startTop + (startHeight - newHeight);
|
| 372 |
+
} else {
|
| 373 |
+
newHeight = startHeight - dy;
|
| 374 |
+
cropBoxX = startLeft + (startWidth - newWidth);
|
| 375 |
+
cropBoxY = startTop + (startHeight - newHeight);
|
| 376 |
+
}
|
| 377 |
+
} else if (resizeHandle.classList.contains('resize-handle-ne')) {
|
| 378 |
+
newWidth = Math.max(50, startWidth + dx);
|
| 379 |
+
newHeight = currentAspectRatio ? newWidth / currentAspectRatio : startHeight - dy;
|
| 380 |
+
if (currentAspectRatio) {
|
| 381 |
+
cropBoxY = startTop + (startHeight - newHeight);
|
| 382 |
+
} else {
|
| 383 |
+
newHeight = startHeight - dy;
|
| 384 |
+
cropBoxY = startTop + (startHeight - newHeight);
|
| 385 |
+
}
|
| 386 |
+
} else if (resizeHandle.classList.contains('resize-handle-sw')) {
|
| 387 |
+
newWidth = Math.max(50, startWidth - dx);
|
| 388 |
+
newHeight = currentAspectRatio ? newWidth / currentAspectRatio : startHeight + dy;
|
| 389 |
+
if (currentAspectRatio) {
|
| 390 |
+
cropBoxX = startLeft + (startWidth - newWidth);
|
| 391 |
+
} else {
|
| 392 |
+
newHeight = startHeight + dy;
|
| 393 |
+
cropBoxX = startLeft + (startWidth - newWidth);
|
| 394 |
+
}
|
| 395 |
+
} else if (resizeHandle.classList.contains('resize-handle-se')) {
|
| 396 |
+
newWidth = Math.max(50, startWidth + dx);
|
| 397 |
+
newHeight = currentAspectRatio ? newWidth / currentAspectRatio : startHeight + dy;
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// Boundary checks
|
| 401 |
+
if (cropBoxX + newWidth > previewContainer.offsetWidth) {
|
| 402 |
+
newWidth = previewContainer.offsetWidth - cropBoxX;
|
| 403 |
+
if (currentAspectRatio) {
|
| 404 |
+
newHeight = newWidth / currentAspectRatio;
|
| 405 |
+
}
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
if (cropBoxY + newHeight > previewContainer.offsetHeight) {
|
| 409 |
+
newHeight = previewContainer.offsetHeight - cropBoxY;
|
| 410 |
+
if (currentAspectRatio) {
|
| 411 |
+
newWidth = newHeight * currentAspectRatio;
|
| 412 |
+
}
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
cropBoxWidth = newWidth;
|
| 416 |
+
cropBoxHeight = newHeight;
|
| 417 |
+
updateCropBox();
|
| 418 |
+
}
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
function stopDrag() {
|
| 422 |
+
isDragging = false;
|
| 423 |
+
isResizing = false;
|
| 424 |
+
resizeHandle = null;
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
function startResize(e) {
|
| 428 |
+
isResizing = true;
|
| 429 |
+
resizeHandle = e.target;
|
| 430 |
+
startX = e.clientX;
|
| 431 |
+
startY = e.clientY;
|
| 432 |
+
startWidth = cropBoxWidth;
|
| 433 |
+
startHeight = cropBoxHeight;
|
| 434 |
+
startLeft = cropBoxX;
|
| 435 |
+
startTop = cropBoxY;
|
| 436 |
+
e.preventDefault();
|
| 437 |
+
e.stopPropagation();
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
function updateEdgeReductionValue() {
|
| 441 |
+
edgeReductionValue.value = edgeReduction.value;
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
function updateEdgeReductionSlider() {
|
| 445 |
+
const value = Math.min(50, Math.max(0, parseInt(edgeReductionValue.value) || 0));
|
| 446 |
+
edgeReductionValue.value = value;
|
| 447 |
+
edgeReduction.value = value;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
function updateCornerRadiusValue() {
|
| 451 |
+
cornerRadiusValue.value = cornerRadius.value;
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
function updateCornerRadiusSlider() {
|
| 455 |
+
const value = Math.min(50, Math.max(0, parseInt(cornerRadiusValue.value) || 0));
|
| 456 |
+
cornerRadiusValue.value = value;
|
| 457 |
+
cornerRadius.value = value;
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
function updateAspectRatio() {
|
| 461 |
+
const ratio = aspectRatio.value;
|
| 462 |
+
|
| 463 |
+
if (ratio === 'custom') {
|
| 464 |
+
customAspectGroup.style.display = 'flex';
|
| 465 |
+
updateCustomAspectRatio();
|
| 466 |
+
} else {
|
| 467 |
+
customAspectGroup.style.display = 'none';
|
| 468 |
+
|
| 469 |
+
if (ratio === 'free') {
|
| 470 |
+
currentAspectRatio = null;
|
| 471 |
+
} else {
|
| 472 |
+
currentAspectRatio = eval(ratio); // Safe in this context as we control the options
|
| 473 |
+
applyAspectRatio();
|
| 474 |
+
}
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
function updateCustomAspectRatio() {
|
| 479 |
+
const width = parseInt(customAspectWidth.value) || 1;
|
| 480 |
+
const height = parseInt(customAspectHeight.value) || 1;
|
| 481 |
+
currentAspectRatio = width / height;
|
| 482 |
+
applyAspectRatio();
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
function applyAspectRatio() {
|
| 486 |
+
if (currentAspectRatio) {
|
| 487 |
+
// Adjust height based on current width to maintain aspect ratio
|
| 488 |
+
cropBoxHeight = cropBoxWidth / currentAspectRatio;
|
| 489 |
+
|
| 490 |
+
// Check if the new height exceeds container bounds
|
| 491 |
+
if (cropBoxY + cropBoxHeight > previewContainer.offsetHeight) {
|
| 492 |
+
cropBoxHeight = previewContainer.offsetHeight - cropBoxY;
|
| 493 |
+
cropBoxWidth = cropBoxHeight * currentAspectRatio;
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
updateCropBox();
|
| 497 |
+
}
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
function cropImage() {
|
| 501 |
+
if (!imageLoaded) return;
|
| 502 |
+
|
| 503 |
+
const reductionPercent = parseInt(edgeReduction.value) / 100;
|
| 504 |
+
const cornerRadiusPercent = parseInt(cornerRadius.value);
|
| 505 |
+
|
| 506 |
+
// Calculate the actual crop area on the original image
|
| 507 |
+
const scaleX = currentImage.width / imagePreview.offsetWidth;
|
| 508 |
+
const scaleY = currentImage.height / imagePreview.offsetHeight;
|
| 509 |
+
|
| 510 |
+
// Apply edge reduction
|
| 511 |
+
const reductionX = cropBoxWidth * reductionPercent;
|
| 512 |
+
const reductionY = cropBoxHeight * reductionPercent;
|
| 513 |
+
|
| 514 |
+
const cropX = cropBoxX * scaleX + reductionX * scaleX / 2;
|
| 515 |
+
const cropY = cropBoxY * scaleY + reductionY * scaleY / 2;
|
| 516 |
+
const cropWidth = cropBoxWidth * scaleX - reductionX * scaleX;
|
| 517 |
+
const cropHeight = cropBoxHeight * scaleY - reductionY * scaleY;
|
| 518 |
+
|
| 519 |
+
// Create a canvas to draw the cropped image
|
| 520 |
+
const canvas = document.createElement('canvas');
|
| 521 |
+
canvas.width = cropWidth;
|
| 522 |
+
canvas.height = cropHeight;
|
| 523 |
+
const ctx = canvas.getContext('2d');
|
| 524 |
+
|
| 525 |
+
// Draw the image first
|
| 526 |
+
ctx.drawImage(
|
| 527 |
+
currentImage,
|
| 528 |
+
cropX, cropY, cropWidth, cropHeight,
|
| 529 |
+
0, 0, cropWidth, cropHeight
|
| 530 |
+
);
|
| 531 |
+
|
| 532 |
+
// Apply rounded corners if radius is greater than 0
|
| 533 |
+
if (cornerRadiusPercent > 0) {
|
| 534 |
+
const radius = (Math.min(cropWidth, cropHeight) / 2) * (cornerRadiusPercent / 100);
|
| 535 |
+
|
| 536 |
+
// Create a clipping path with rounded corners
|
| 537 |
+
ctx.globalCompositeOperation = 'destination-in';
|
| 538 |
+
ctx.beginPath();
|
| 539 |
+
ctx.moveTo(radius, 0);
|
| 540 |
+
ctx.lineTo(cropWidth - radius, 0);
|
| 541 |
+
ctx.quadraticCurveTo(cropWidth, 0, cropWidth, radius);
|
| 542 |
+
ctx.lineTo(cropWidth, cropHeight - radius);
|
| 543 |
+
ctx.quadraticCurveTo(cropWidth, cropHeight, cropWidth - radius, cropHeight);
|
| 544 |
+
ctx.lineTo(radius, cropHeight);
|
| 545 |
+
ctx.quadraticCurveTo(0, cropHeight, 0, cropHeight - radius);
|
| 546 |
+
ctx.lineTo(0, radius);
|
| 547 |
+
ctx.quadraticCurveTo(0, 0, radius, 0);
|
| 548 |
+
ctx.closePath();
|
| 549 |
+
ctx.fill();
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
// Display the result
|
| 553 |
+
croppedResult.src = canvas.toDataURL('image/png');
|
| 554 |
+
result.style.display = 'block';
|
| 555 |
+
downloadButton.disabled = false;
|
| 556 |
+
|
| 557 |
+
// Scroll to result
|
| 558 |
+
result.scrollIntoView({ behavior: 'smooth' });
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
function downloadCroppedImage() {
|
| 562 |
+
if (!croppedResult.src) return;
|
| 563 |
+
|
| 564 |
+
const link = document.createElement('a');
|
| 565 |
+
link.download = 'cropped-image.png';
|
| 566 |
+
link.href = croppedResult.src;
|
| 567 |
+
link.click();
|
| 568 |
+
}
|
| 569 |
+
});
|
| 570 |
+
</script>
|
| 571 |
+
</body>
|
| 572 |
+
</html>
|