Readme

URL to AI Image

Source code credit: @maxm on val.town

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
import { generateImage } from "https://esm.town/v/isidentical/falImageGen?v=11";
import { blob } from "https://esm.town/v/std/blob?v=12";
const genKey = (key: string): string => {
return "genImageCache-v2-" + key;
};
export default async function(req: Request): Promise<Response> {
let key = new URL(req.url).pathname;
if (key === "/") {
return new Response(
"This site generates high-quality images based on the URL path. Simply append a description of the desired image to the URL to generate a corresponding image.",
{
headers: { "content-type": "text/plain" },
},
);
}
let cacheData = await blob.getJSON(genKey(key));
if (cacheData && cacheData.expiration > Date.now()) {
return new Response((await fetch(cacheData.url)).body, { headers: { "content-type": "image/jpg" } });
}
let resp = await generateImage(`${key}, high resolution, high quality photo, 8k canon camera`);
let url = resp.url;
let expiration = Date.now() + 24 * 60 * 60 * 1000; // Cache for 24 hours
await blob.setJSON(genKey(key), { url, expiration });
return new Response((await fetch(url)).body, {
headers: {
"content-type": "image/jpg",
"cache-control": "public, max-age=86400", // Set cache control header
},
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
andrewgao-imggenurl.web.val.run
v5
May 25, 2024