|
| 1 | +document.addEventListener("DOMContentLoaded", () => { |
| 2 | + const inputImage = document.getElementById("inputImage"); |
| 3 | + const image = document.getElementById("image"); |
| 4 | + const cropper = document.getElementById("cropper"); |
| 5 | + const exportBtn = document.getElementById("exportBtn"); |
| 6 | + |
| 7 | + let cropperInstance; |
| 8 | + |
| 9 | + inputImage.addEventListener("change", (e) => { |
| 10 | + const files = e.target.files; |
| 11 | + const done = (url) => { |
| 12 | + inputImage.value = ""; |
| 13 | + image.src = url; |
| 14 | + image.style.display = "block"; |
| 15 | + |
| 16 | + if (cropperInstance) { |
| 17 | + cropperInstance.destroy(); |
| 18 | + } |
| 19 | + |
| 20 | + cropperInstance = new Cropper(image, { |
| 21 | + aspectRatio: 3 / 4, |
| 22 | + viewMode: 1, |
| 23 | + dragMode: "move", |
| 24 | + }); |
| 25 | + }; |
| 26 | + |
| 27 | + const reader = new FileReader(); |
| 28 | + reader.onload = (e) => done(reader.result); |
| 29 | + reader.readAsDataURL(files[0]); |
| 30 | + }); |
| 31 | + |
| 32 | + exportBtn.addEventListener("click", () => { |
| 33 | + const canvas = cropperInstance.getCroppedCanvas({ |
| 34 | + width: 150, |
| 35 | + height: 200, |
| 36 | + }); |
| 37 | + |
| 38 | + canvas.toBlob( |
| 39 | + (blob) => { |
| 40 | + const reader = new FileReader(); |
| 41 | + reader.onload = (e) => { |
| 42 | + const imgData = e.target.result; |
| 43 | + |
| 44 | + // Compress image using pica library |
| 45 | + const targetCanvas = document.createElement("canvas"); |
| 46 | + targetCanvas.width = 150; |
| 47 | + targetCanvas.height = 200; |
| 48 | + |
| 49 | + const picaImg = pica(); |
| 50 | + const unsharpAmount = |
| 51 | + +document.getElementById("unsharpAmount")?.value || 0; |
| 52 | + const unsharpThreshold = |
| 53 | + +document.getElementById("unsharpThreshold")?.value || 0; |
| 54 | + console.log(unsharpAmount, unsharpThreshold); |
| 55 | + picaImg |
| 56 | + .resize(canvas, targetCanvas, { |
| 57 | + unsharpAmount: unsharpAmount, |
| 58 | + unsharpThreshold: unsharpThreshold, |
| 59 | + }) |
| 60 | + .then((result) => { |
| 61 | + const compressedBlob = dataURLtoBlob( |
| 62 | + targetCanvas.toDataURL("image/jpeg"), |
| 63 | + "image/jpeg" |
| 64 | + ); |
| 65 | + |
| 66 | + // Ensure size is below 64KB |
| 67 | + if (compressedBlob.size <= 64 * 1024) { |
| 68 | + // Create a download link |
| 69 | + const link = document.createElement("a"); |
| 70 | + link.href = URL.createObjectURL(compressedBlob); |
| 71 | + link.download = "cropped-image.jpg"; |
| 72 | + link.click(); |
| 73 | + } else { |
| 74 | + alert( |
| 75 | + "Compressed image size is still above 64KB. Please try again." |
| 76 | + ); |
| 77 | + } |
| 78 | + }) |
| 79 | + .catch((err) => console.error(err)); |
| 80 | + }; |
| 81 | + reader.readAsDataURL(blob); |
| 82 | + }, |
| 83 | + "image/jpeg", |
| 84 | + 0.9 |
| 85 | + ); // Adjust quality here, 0.7 is 70% quality |
| 86 | + }); |
| 87 | + |
| 88 | + function dataURLtoBlob(dataUrl, mimeType) { |
| 89 | + const byteString = atob(dataUrl.split(",")[1]); |
| 90 | + const ab = new ArrayBuffer(byteString.length); |
| 91 | + const ia = new Uint8Array(ab); |
| 92 | + for (let i = 0; i < byteString.length; i++) { |
| 93 | + ia[i] = byteString.charCodeAt(i); |
| 94 | + } |
| 95 | + return new Blob([ab], { type: mimeType }); |
| 96 | + } |
| 97 | +}); |
0 commit comments