Skip to content

Commit

Permalink
Generate blur image on import
Browse files Browse the repository at this point in the history
  • Loading branch information
ertrzyiks committed Jan 5, 2025
1 parent 14b22f7 commit f7cf226
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions prisma/migrations/20230527140804_init/migration.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CREATE TABLE "Recipe" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"slug" TEXT NOT NULL,
"coverImage" TEXT NOT NULL,
"coverImageBlurDataUrl" TEXT,
"title" TEXT NOT NULL,
"headline" TEXT NOT NULL,
"preparationTime" INTEGER NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ model Recipe {
id Int @id @default(autoincrement())
slug String @unique
coverImage String
coverImageBlurDataUrl String?
title String
headline String
preparationTime Int
Expand Down
24 changes: 24 additions & 0 deletions scripts/import.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async function createOrUpdateRecipes({
slug,
category,
coverImage,
coverImageBlurDataUrl,
headline,
preparationTime,
publishedAt,
Expand Down Expand Up @@ -134,6 +135,7 @@ async function createOrUpdateRecipes({
headline,
preparationTime,
coverImage,
coverImageBlurDataUrl,
publishedAt,
category: {
connect: {
Expand All @@ -160,6 +162,7 @@ async function createOrUpdateRecipes({
slug,
headline,
coverImage,
coverImageBlurDataUrl,
preparationTime,
publishedAt,
category: {
Expand Down Expand Up @@ -206,6 +209,15 @@ function loadRecipes(after = null, limit) {
}
cover {
url
blurPlaceholder: url(
transformation: {
document: { output: { format: jpg } }
image: {
resize: { width: 30, height: 30 }
blur: { amount: 20 }
}
}
)
}
category {
id
Expand All @@ -226,6 +238,15 @@ function loadRecipes(after = null, limit) {
);
}

async function imageToDataUrl(url) {
const res = await fetch(url);
const blob = await res.blob();
const buffer = await blob.arrayBuffer();
const base64 = Buffer.from(buffer).toString("base64");
const mimeType = blob.type;
return `data:${mimeType};base64,${base64}`;
}

async function main() {
const { data, errors } = await loadCategories();
const categoryMap = {};
Expand Down Expand Up @@ -273,6 +294,9 @@ async function main() {
coverImage:
recipe.cover?.url ??
"https://media.graphassets.com/eiXZ15TaNlZO4H8DQQQB",
coverImageBlurDataUrl: recipe.cover?.blurPlaceholder
? await imageToDataUrl(recipe.cover.blurPlaceholder)
: null,
headline: recipe.headline,
preparationTime: recipe.preparationTime,
category: { id: categoryMap[recipe.category.slug].id },
Expand Down
1 change: 1 addition & 0 deletions src/app/[categorySlug]/[recipeSlug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default async function Page({ params }: Props) {
<Recipe
title={recipe.title}
coverImage={recipe.coverImage}
coverImageBlurDataUrl={recipe.coverImageBlurDataUrl}
ingredients={recipe.ingredients}
instructions={recipe.instructions}
/>
Expand Down
8 changes: 6 additions & 2 deletions src/components/recipe/recipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import { markdownToHtml } from "@/lib/markdown";
interface Props {
title: string;
coverImage: string;
coverImageBlurDataUrl: string | null;
ingredients: { id: number; content: string }[];
instructions: { id: number; content: string }[];
}

export async function Recipe({
title,
coverImage,
coverImageBlurDataUrl,
ingredients,
instructions,
}: Props) {
return (
<div className="flex flex-col w-full">
<div className="my-12">
<div className="mt-8 mb-8">
<h1 className="text-gradient uppercase inline text-4xl">{title}</h1>
</div>

<div className="md:w-1/2">
<div className="mb-4 md:w-1/2">
<Image
className="w-full max-h-32 object-cover"
placeholder={coverImageBlurDataUrl ? "blur" : "empty"}
blurDataURL={coverImageBlurDataUrl ?? undefined}
src={coverImage}
width={600}
height={400}
Expand Down

0 comments on commit f7cf226

Please sign in to comment.