import { useState, useRef, useEffect } from 'react' import { useTranslations } from 'next-intl' import Button from '@tw/Button' export default function ImageUploader() { const [uploadedImage, setUploadedImage] = useState(null) const [processedImage, setProcessedImage] = useState(null) const t = useTranslations('ReturnToMonke') const fileInput = useRef(null) const imgRef = useRef(null) useEffect(() => { if (uploadedImage) { applyImageOverlay() } }, [uploadedImage]) function handleFile(file) { setUploadedImage(file) } function handleDragOver(event) { event.preventDefault() } function handleOnDrop(event) { event.preventDefault() event.stopPropagation() handleFile(event.dataTransfer.files[0]) console.log('File drop was a success') } const applyImageOverlay = () => { const canvas = imgRef.current const ctx = canvas.getContext('2d') // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height) // Load the uploaded image const img = new Image() img.src = URL.createObjectURL(uploadedImage) img.onload = () => { if (img.width >= img.height) { canvas.width = img.width canvas.height = img.width } else { canvas.width = img.height canvas.height = img.height } // Calculate the start positions to center the image const startX = (canvas.width - img.width) / 2 const startY = (canvas.height - img.height) / 2 // Draw the uploaded image centered ctx.drawImage(img, startX, startY) // Load the overlay image const overlay = new Image() overlay.src = './synpfpborder.png' overlay.onload = () => { // Draw the overlay image on top of the uploaded image ctx.drawImage(overlay, 0, 0, canvas.width, canvas.height) // Convert the canvas to a Blob and update the state canvas.toBlob((blob) => { setProcessedImage(blob) }, 'image/jpeg') } } } return (
fileInput.current.click()} >

{!processedImage && ( <> {t('Click to Upload or')}
{t('Drag and drop image here')}... )} {processedImage && <>{t('Select different image?')}}

handleFile(e.target.files[0])} />
{processedImage && ( preview )} {uploadedImage && ( )}
) } function downloadBase64File(dataStr, fileName) { const downloadLink = document.createElement('a') downloadLink.href = dataStr downloadLink.download = fileName downloadLink.click() console.log('evolving into a synape') }