Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: generate pic for gallery.

thorwebdevc9d2934b819b
diff --git a/apps/www/supabase/functions/lw7-ticket-gallery/handler.tsx b/apps/www/supabase/functions/lw7-ticket-gallery/handler.tsx
new file mode 100644
index 00000000000..3e212477403
--- /dev/null
+++ b/apps/www/supabase/functions/lw7-ticket-gallery/handler.tsx
@@ -0,0 +1,224 @@
+import React from 'https://esm.sh/[email protected]?deno-std=0.140.0'
+import { ImageResponse } from 'https://deno.land/x/[email protected]/mod.ts'
+import { createClient } from 'https://esm.sh/@supabase/[email protected]'
+import { corsHeaders } from '../_shared/cors.ts'
+
+const STORAGE_URL = 'https://obuldanrptloktxcffvn.supabase.co/storage/v1/object/public/images/lw7'
+
+// Load custom font
+const FONT_URL = `${STORAGE_URL}/CircularStd-Book.otf`
+const font = fetch(new URL(FONT_URL, import.meta.url)).then((res) => res.arrayBuffer())
+
+export async function handler(req: Request) {
+ try {
+ if (req.method !== 'POST') throw new Error('method not supported')
+ const {
+ username = 'thorwebdev',
+ name = 'Thorsten Schaeff',
+ ticketNumber = 1234,
+ golden = true,
+ bg_image_id = 80,
+ }: {
+ username: string
+ name: string
+ ticketNumber: number
+ golden: boolean
+ bg_image_id: number
+ } = await req.json()
+
+ // Else, generate image ad upload to storage.
+ const BACKGROUND = {
+ REG: {
+ AI: `${STORAGE_URL}/tickets_bg/reg_bg_${bg_image_id}.png`,
+ TICKET: `${STORAGE_URL}/reg_ticket.png`,
+ },
+ GOLD: {
+ // TODO: swap to bg_image_id when golden ticket backgrounds have been generated
+ AI: `${STORAGE_URL}/tickets_bg/golden/gold_bg_${Math.floor(Math.random() * 56)}.png`,
+ TICKET: `${STORAGE_URL}/gold_ticket.png`,
+ },
+ }
+
+ const fontData = await font
+ const numDigits = `${Number(ticketNumber)}`.length
+ const prefix = `00000000`.slice(numDigits)
+
+ const generatedImage = new ImageResponse(
+ (
+ <>
+ <div
+ style={{
+ width: '1200px',
+ height: '630px',
+ backgroundColor: '#000',
+ color: '#F8F9FA',
+ fontFamily: '"Circular"',
+ overflow: 'hidden',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ }}
+ >
+ {/* Background ai */}
+ <img
+ width="1027"
+ height="524"
+ style={{
+ borderRadius: '8px',
+ position: 'absolute',
+ top: '53',
+ left: '87',
+ zIndex: '-8000',
+ }}
+ src={golden ? BACKGROUND['GOLD']['AI'] : BACKGROUND['REG']['AI']}
+ />
+ {/* Background ticket */}
+ <img
+ width="1089"
+ height="586"
+ style={{
+ position: 'absolute',
+ top: '22',
+ left: '56',
+ zIndex: '-7000',
+ }}
+ src={golden ? BACKGROUND['GOLD']['TICKET'] : BACKGROUND['REG']['TICKET']}
+ />
+ {/* GitHub Avatar image */}
+ <img
+ width="166"
+ height="166"
+ style={{
+ position: 'absolute',
+ top: '210',
+ left: '484',
+ borderRadius: 83,
+ }}
+ src={`https://github.com/${username}.png`}
+ />
+ {/* Name & username */}
+ <div
+ style={{
+ // border: '3px solid red',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ flexDirection: 'column',
+ position: 'absolute',
+ top: '350',
+ left: '210',
+ width: '727',
+ height: '200',
+ overflow: 'hidden',
+ textOverflow: 'clip',
+ textAlign: 'center',
+ }}
+ >
+ <div
+ style={{
+ // border: '3px solid green',
+ display: 'flex',
+ color: 'transparent',
+ backgroundImage:
+ 'linear-gradient(180deg, rgba(248, 249, 250, 0.66) -31.69%, #F8F9FA 22.14%, rgba(248, 249, 250, 0.5) 122.78%)',
+ backgroundClip: 'text',
+ }}
+ >
+ <p
+ style={{
+ margin: '0',
+ fontSize: '50px',
+ lineHeight: '65px',
+ }}
+ >
+ {name ?? username}
+ </p>{' '}
+ </div>
+
+ {/* Username */}
+ <div
+ style={{
+ // border: '3px solid blue',/
+ color: '#EDEDED',
+ opacity: 0.8,
+ display: 'flex',
+ fontSize: '20',
+ marginTop: '0px',
+ }}
+ >
+ <span>{`@${username}`}</span>
+ </div>
+ </div>
+ </div>
+ {/* Ticket No */}
+ <div
+ style={{
+ // border: '3px solid red',
+ color: '#F8F9FA',
+ display: 'flex',
+ alignItems: 'center',
+ justifyContent: 'center',
+ position: 'absolute',
+ bottom: '225',
+ right: '-130',
+ width: '575',
+ height: '175',
+ transform: 'rotate(90deg)',
+ opacity: 0.8,
+ }}
+ >
+ <p
+ style={{
+ fontSize: '60',
+ }}
+ >
+ {`No ${prefix}${ticketNumber}`}
+ </p>
+ </div>
+ </>
+ ),
+ {
+ width: 1200,
+ height: 608,
+ fonts: [
+ {
+ name: 'Circular',
+ data: fontData,
+ style: 'normal',
+ },
+ ],
+ headers: {
+ 'content-type': 'image/png',
+ 'cache-control': 'public, max-age=31536000, s-maxage=31536000, no-transform, immutable',
+ 'cdn-cache-control': 'max-age=31536000',
+ },
+ }
+ )
+
+ // Upload image to storage.
+ const supabaseAdminClient = createClient(
+ // Supabase API URL - env var exported by default when deployed.
+ Deno.env.get('SUPABASE_URL') ?? '',
+ // Supabase API SERVICE ROLE KEY - env var exported by default when deployed.
+ Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
+ )
+ const { error: storageError } = await supabaseAdminClient.storage
+ .from('images')
+ .upload(`lw7/tickets/gallery/${username}.png`, generatedImage.body!, {
+ contentType: 'image/png',
+ cacheControl: '31536000',
+ upsert: true,
+ })
+ if (storageError) throw new Error(`storageError: ${storageError.message}`)
+
+ return new Response(JSON.stringify({ success: true }), {
+ headers: { ...corsHeaders, 'Content-Type': 'application/json' },
+ status: 200,
+ })
+ } catch (error) {
+ return new Response(JSON.stringify({ error: error.message }), {
+ headers: { ...corsHeaders, 'Content-Type': 'application/json' },
+ status: 400,
+ })
+ }
+}
diff --git a/apps/www/supabase/functions/lw7-ticket-gallery/index.ts b/apps/www/supabase/functions/lw7-ticket-gallery/index.ts
new file mode 100644
index 00000000000..c24f7c7b2c8
--- /dev/null
+++ b/apps/www/supabase/functions/lw7-ticket-gallery/index.ts
@@ -0,0 +1,11 @@
+// Follow this setup guide to integrate the Deno language server with your editor:
+// https://deno.land/manual/getting_started/setup_your_environment
+// This enables autocomplete, go to definition, etc.
+
+import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+
+import { handler } from './handler.tsx'
+
+console.log(`Function "lw7-ticket-gallery" up and running!`)
+
+serve(handler)
diff --git a/apps/www/supabase/functions/lw7-ticket-og/handler.tsx b/apps/www/supabase/functions/lw7-ticket-og/handler.tsx
index b552e7aa42c..49894b16988 100644
--- a/apps/www/supabase/functions/lw7-ticket-og/handler.tsx
+++ b/apps/www/supabase/functions/lw7-ticket-og/handler.tsx
@@ -252,6 +252,22 @@ export async function handler(req: Request) {
upsert: false,
})
if (storageError) throw new Error(`storageError: ${storageError.message}`)
+ // Generate imgae for gallery
+ fetch('https://obuldanrptloktxcffvn.functions.supabase.co/lw7-ticket-gallery', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization:
+ 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im9idWxkYW5ycHRsb2t0eGNmZnZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE2Njk3MjcwMTIsImV4cCI6MTk4NTMwMzAxMn0.SZLqryz_-stF8dgzeVXmzZWPOqdOrBwqJROlFES8v3I',
+ },
+ body: JSON.stringify({
+ username,
+ name,
+ ticketNumber,
+ bg_image_id,
+ golden,
+ }),
+ })
return await fetch(`${STORAGE_URL}/tickets/${golden ? `golden/${username}` : username}.png`)
} catch (error) {