Skip to content

Commit

Permalink
refactor: move to fn
Browse files Browse the repository at this point in the history
  • Loading branch information
rqbazan committed Jul 17, 2024
1 parent 4c588ac commit d6fa4c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/ui/components/cover-img.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
---
import { Image } from "@unpic/astro";
import { getPixels } from "@unpic/pixels";
import { blurhashToDataUri } from "@unpic/placeholder";
import { encode } from "blurhash";
import { cn } from "~/utils/cn";
import { getImagePlaceholder } from "~/utils/get-image-placeholder";
interface Props {
src: string;
Expand All @@ -15,15 +13,7 @@ interface Props {
}
const { class: className, src, description, ...otherProps } = Astro.props;
let supportedSrc = src.replace(".webp", ".jpg");
supportedSrc = supportedSrc.replace("upload/", "upload/w_20,h_15/");
const jpgData = await getPixels(supportedSrc);
const data = Uint8ClampedArray.from(jpgData.data);
const blurhash = encode(data, jpgData.width, jpgData.height, 4, 4);
const placeholder = blurhashToDataUri(blurhash);
const placeholder = await getImagePlaceholder(src);
---

<div class={cn(className, "flex flex-col items-center")}>
Expand Down
18 changes: 18 additions & 0 deletions src/utils/get-image-placeholder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getPixels } from "@unpic/pixels";
import { blurhashToDataUri } from "@unpic/placeholder";
import { encode } from "blurhash";

/**
* @param src {string} - Cloudinary Image URL
* @returns {Promise<string>} - Blurhash Data URI
*/
export async function getImagePlaceholder(src: string): Promise<string> {
let supportedSrc = src.replace(".webp", ".jpg");
supportedSrc = supportedSrc.replace("upload/", "upload/w_20,h_15/");

const jpgData = await getPixels(supportedSrc);
const data = Uint8ClampedArray.from(jpgData.data);
const blurhash = encode(data, jpgData.width, jpgData.height, 4, 4);

return blurhashToDataUri(blurhash);
}

0 comments on commit d6fa4c6

Please sign in to comment.