2021-12-12 16:50:58 +00:00
|
|
|
import FileSaver from "file-saver";
|
2021-12-13 12:54:54 +00:00
|
|
|
import { useEffect } from "react";
|
2021-12-12 16:50:58 +00:00
|
|
|
import "./ImageOutput.css";
|
|
|
|
|
|
|
|
interface OutputProps {
|
2021-12-13 12:54:54 +00:00
|
|
|
imageData: Blob;
|
2021-12-12 16:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ImageOutput({ imageData }: OutputProps) {
|
2021-12-13 12:54:54 +00:00
|
|
|
const imageUrl = URL.createObjectURL(imageData);
|
|
|
|
|
2021-12-13 22:15:11 +00:00
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
2021-12-13 12:54:54 +00:00
|
|
|
URL.revokeObjectURL(imageUrl);
|
2021-12-13 22:15:11 +00:00
|
|
|
};
|
2021-12-13 12:54:54 +00:00
|
|
|
}, [imageUrl]);
|
2021-12-12 16:50:58 +00:00
|
|
|
|
|
|
|
const handleClick = () => {
|
2021-12-13 12:54:54 +00:00
|
|
|
const extension = imageData.type.split("/")[1];
|
|
|
|
FileSaver.saveAs(imageData, `image.${extension}`);
|
2021-12-12 16:50:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-12-13 22:15:11 +00:00
|
|
|
<div className="max-w-4xl mx-auto">
|
2021-12-12 16:50:58 +00:00
|
|
|
<img
|
|
|
|
alt="dithering output"
|
|
|
|
src={imageUrl}
|
2021-12-13 22:15:11 +00:00
|
|
|
className="object-cover min-w-[70%] my-4 px-2 mx-auto"
|
2021-12-12 16:50:58 +00:00
|
|
|
/>
|
|
|
|
<button
|
|
|
|
onClick={handleClick}
|
2021-12-12 17:27:10 +00:00
|
|
|
className="block w-48 mx-auto mt-1 py-2 px-4 text-sm text-center text-nord-0 bg-nord-4 hover:bg-nord-5"
|
2021-12-12 16:50:58 +00:00
|
|
|
>
|
|
|
|
Download
|
|
|
|
</button>
|
|
|
|
<a
|
|
|
|
href={imageUrl}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2021-12-12 17:27:10 +00:00
|
|
|
className="block w-48 mx-auto mt-1 py-2 px-4 text-sm text-center text-nord-0 bg-nord-4 hover:bg-nord-5"
|
2021-12-12 16:50:58 +00:00
|
|
|
>
|
|
|
|
Open in new tab
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ImageOutput;
|