Spaces:
Running
Running
File size: 1,348 Bytes
1efb9e6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import { decode } from "blurhash"
const SIZE = 32;
export function blurHashToDataURL(hash) {
if (!hash) return undefined
const pixels = decode(hash, SIZE, SIZE)
const canvas = document.createElement("canvas");
canvas.width = SIZE;
canvas.height = SIZE;
const ctx = canvas.getContext("2d");
const imageData = ctx.createImageData(SIZE, SIZE);
imageData.data.set(pixels);
ctx.putImageData(imageData, 0, 0);
return canvas.toDataURL();
}
function downloadData(url, filename) {
// Create an anchor element with the data URL as the href attribute
const downloadLink = document.createElement('a');
downloadLink.href = url;
// Set the download attribute to specify the desired filename for the downloaded image
downloadLink.download = filename;
// Trigger the download
downloadLink.click();
// Clean up: remove the anchor element from the DOM
downloadLink.remove();
}
export function downloadImage(url, filename) {
fetch(url, {
headers: new Headers({
Origin: location.origin,
}),
mode: 'cors',
})
.then((response) => response.blob())
.then((blob) => {
let blobUrl = window.URL.createObjectURL(blob)
downloadData(blobUrl, filename)
})
.catch((e) => console.error(e))
}
|