Skip to content

Commit 9cd8ceb

Browse files
Merge pull request #35 from depatchedmode/image-bugs
Image bugs
2 parents 9d14058 + d781a6b commit 9cd8ceb

File tree

11 files changed

+45
-125
lines changed

11 files changed

+45
-125
lines changed

api/og-image.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

modules/processFrameRequest.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import satori from "satori";
2+
import sharp from "sharp";
3+
import { html } from "satori-html";
4+
import fonts from "../src/fonts.js";
5+
import mainLayout from "../src/layouts/main.js";
16
import frames from '../src/frames/index.js';
27
import { Frame, FrameActionDataParsed, GetFrameHtmlOptions, getFrameHtml } from "frames.js";
38
import landingPage from '../src/landing-page.js';
4-
import { objectToURLSearchParams } from '../modules/utils.js';
59
import { isFrameStolen } from './antitheft.js';
610

711
/**
@@ -63,7 +67,7 @@ const respondWithFrame = async (
6367
const host = process.env.URL;
6468
const frame: Frame = {
6569
version: 'vNext',
66-
image: handleImageSource(name, simpleFrame, message),
70+
image: await handleImageSource(simpleFrame, message),
6771
buttons: simpleFrame.buttons,
6872
inputText: simpleFrame.inputText,
6973
postUrl: `${host}/?${postVars.toString()}`
@@ -90,26 +94,43 @@ const respondWithFrame = async (
9094
);
9195
};
9296

93-
function handleImageSource(name, frame, message):string {
97+
async function handleImageSource(frame, message):Promise<string> {
9498
const dataUriPattern = /^data:image\/[a-zA-Z]+;base64,/;
9599
const absoluteUrlPattern = /^https?:\/\//;
96100
const host = process.env.URL;
97101

98-
const { image } = frame;
102+
const { imageURL, imageMarkup } = frame;
99103

100-
if (dataUriPattern.test(image)) {
101-
return `${host}/og-image?${objectToURLSearchParams({
102-
dataUri: image,
103-
})}`;
104-
} else if (absoluteUrlPattern.test(image)) {
105-
return `${host}/og-image?${objectToURLSearchParams({
106-
imageUrl: image,
107-
})}`;
108-
} else {
109-
return `${host}/og-image?${objectToURLSearchParams({
110-
t: new Date().valueOf(), // Current timestamp for cache busting.
111-
frameName: name || '',
112-
message
113-
})}`;
104+
if (imageMarkup) {
105+
const frameMarkupInLayout = mainLayout(imageMarkup, message)
106+
const svg = await satori(
107+
html(frameMarkupInLayout),
108+
{
109+
width: 1200,
110+
height: 630,
111+
fonts: fonts,
112+
}
113+
);
114+
const svgBuffer = Buffer.from(svg);
115+
const imgOutput = sharp(svgBuffer).webp();
116+
const imageBuffer = await imgOutput.toBuffer();
117+
return `data:image/png;base64,${imageBuffer.toString('base64')}`;
118+
}
119+
120+
// data URI
121+
else if (dataUriPattern.test(imageURL)) {
122+
return imageURL;
123+
}
124+
125+
// external image: need to proxy it
126+
else if (absoluteUrlPattern.test(imageURL)) {
127+
const ogImageResponse = await fetch(imageURL);
128+
const dataURI = await ogImageResponse.text(); // Assuming og-image returns the data URI in the response body
129+
return dataURI;
130+
}
131+
132+
// local image
133+
else {
134+
return `${host}/${imageURL}`;
114135
}
115136
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "simplest-frame",
33
"type": "module",
4-
"version": "0.11.0",
4+
"version": "0.11.1",
55
"dependencies": {
66
"@netlify/blobs": "^6.4.2",
77
"dompurify": "^3.0.8",

public/images/credits.png

-21.6 KB
Binary file not shown.

src/frames/count.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default {
99
const count = await getCount();
1010
const { username, taunt } = await getFramer() || {};
1111
return {
12-
image: html`
12+
imageMarkup: html`
1313
<div style="
1414
font-family: 'Redaction';
1515
display: flex;

src/frames/credits.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/frames/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import poster from "./poster.js";
22
import count from "./count.js";
3-
import credits from "./credits.js";
43
import mint from "./mint.js";
54
import transaction from "./transaction.js";
65
import stolen from "./stolen.js";
76

87
export default {
98
count,
109
poster,
11-
credits,
1210
transaction,
1311
mint,
1412
stolen,

src/frames/mint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
name: 'mint',
77
render: async () => {
88
return {
9-
image: `${process.env.URL}/images/poster-animated.gif`,
9+
imageURL: 'images/poster-animated.gif',
1010
buttons: [
1111
{
1212
action: 'post',

src/frames/poster.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
name: 'poster',
55
render: () => {
66
return {
7-
image: `${process.env.URL}/images/poster-animated.gif`,
7+
imageURL: 'images/poster-animated.gif',
88
buttons: [
99
{
1010
action: 'post',

src/frames/stolen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default {
22
name: 'stolen',
33
render: () => {
44
return {
5-
image: `${process.env.URL}/images/stolen.png`,
5+
imageURL: 'images/stolen.png',
66
buttons: [
77
{
88
action: 'post',

0 commit comments

Comments
 (0)