-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc58aaf
commit 07f5589
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
let cropper; | ||
const fileInput = document.getElementById('file-input'); | ||
const imageContainer = document.getElementById('image-container'); | ||
const resizeButton = document.getElementById('resize-button'); | ||
const downloadLink = document.getElementById('download-link'); | ||
const widthInput = document.getElementById('width'); | ||
const heightInput = document.getElementById('height'); | ||
|
||
fileInput.addEventListener('change', (e) => { | ||
const file = e.target.files[0]; | ||
if (file) { | ||
const reader = new FileReader(); | ||
reader.onload = (e) => { | ||
imageContainer.innerHTML = `<img src="${e.target.result}" id="image">`; | ||
const image = document.getElementById('image'); | ||
cropper = new Cropper(image, { | ||
aspectRatio: NaN, | ||
viewMode: 1, | ||
}); | ||
}; | ||
reader.readAsDataURL(file); | ||
} | ||
}); | ||
|
||
resizeButton.addEventListener('click', () => { | ||
if (cropper) { | ||
resizeButton.disabled = true; | ||
resizeButton.textContent = 'Processing...'; | ||
|
||
const width = parseInt(widthInput.value); | ||
const height = parseInt(heightInput.value); | ||
const canvas = cropper.getCroppedCanvas({ | ||
width: width, | ||
height: height | ||
}); | ||
|
||
canvas.toBlob((blob) => { | ||
const url = URL.createObjectURL(blob); | ||
downloadLink.href = url; | ||
downloadLink.download = 'resized-image.png'; | ||
downloadLink.style.display = 'inline-block'; | ||
resizeButton.disabled = false; | ||
resizeButton.textContent = 'Resize Image'; | ||
}); | ||
} | ||
}); |