1export async function replaceEmojisWithImages(
2req: express.Request,
3res: express.Response,
generateRAdioDjRssmain.tsx2 matches
5export const generateRAdioDjRss = async () => {
6const rssItems = previousDjs.map((dj) => {
7const djImgSrc = `https://r-a-d.io/api/dj-image/${
8encodeURIComponent(
9dj.djimage,
10)
11}`
hiraganaWordBuildermain.tsx18 matches
60const [isComplete, setIsComplete] = useState(false);
61const [inputStatus, setInputStatus] = useState([]);
62const [imageData, setImageData] = useState("");
63const [isLoading, setIsLoading] = useState(false);
64const [isTransitioning, setIsTransitioning] = useState(false);
88};
8990const fetchImage = async (romaji) => {
91setIsLoading(true);
92try {
93const response = await fetch(`/api/image/${romaji}`);
94if (!response.ok) {
95throw new Error("Failed to fetch image");
96}
97const data = await response.json();
98if (data && data.image) {
99const dataUrl = `data:image/png;base64,${data.image}`;
100setImageData(dataUrl);
101} else {
102throw new Error("Invalid image data received");
103}
104} catch (error) {
105console.error("Error fetching image:", error);
106setImageData("");
107} finally {
108setIsLoading(false);
119setInputStatus([]);
120setIsComplete(false);
121fetchImage(newWord.romaji);
122setIsTransitioning(false);
123}, 300);
178</div>
179)
180: imageData
181? <img src={imageData} alt="Vocabulary word" className="w-40 h-40 object-cover rounded-lg shadow-md" />
182: (
183<div className="w-40 h-40 flex items-center justify-center bg-gray-200 rounded-lg">
184<span className="text-gray-500">No image available</span>
185</div>
186)}
264const url = new URL(request.url);
265266if (url.pathname.startsWith("/api/image/")) {
267const romaji = url.pathname.split("/").pop();
268try {
269// This is a placeholder for the actual image retrieval from Val Town blob storage
270// You'll need to implement the actual retrieval logic here
271const vocabularyJson = await blob.getJSON(romaji);
275});
276} catch (error) {
277console.error("Error fetching image:", error);
278return new Response("Image not found", { status: 404 });
279}
280}
getJsonAndRenderAsImagemain.tsx16 matches
56function App() {
7const [imageData, setImageData] = useState<string | null>(null);
8const [error, setError] = useState<string | null>(null);
910useEffect(() => {
11async function fetchImage() {
12try {
13const response = await fetch("/image");
14if (!response.ok) {
15throw new Error(`HTTP error! status: ${response.status}`);
16}
17const data = await response.json();
18if (data && data.image) {
19setImageData(`data:image/png;base64,${data.image}`);
20} else {
21throw new Error("Invalid image data received");
22}
23} catch (error) {
24console.error("Error fetching image:", error);
25setError(error instanceof Error ? error.message : "Error loading image");
26}
27}
28fetchImage();
29}, []);
3031return (
32<div>
33<h1>Image from Blob Storage</h1>
34{imageData
35? (
36<img
37src={imageData}
38alt="Fetched from blob storage"
39style={{ maxWidth: "100%" }}
42: error
43? <p>Error: {error}</p>
44: <p>Loading image...</p>}
45</div>
46);
5657export default async function server(request: Request): Promise<Response> {
58if (request.url.endsWith("/image")) {
59try {
60const jsonData = await blob.getJSON("image-test");
61return new Response(JSON.stringify(jsonData), {
62headers: { "Content-Type": "application/json" },
76<html>
77<head>
78<title>Image from Blob Storage</title>
79<style>${css}</style>
80</head>
getBlobAndRenderAsImagemain.tsx14 matches
56function App() {
7const [imageData, setImageData] = useState<string | null>(null);
8const [error, setError] = useState<string | null>(null);
910useEffect(() => {
11async function fetchImage() {
12try {
13const response = await fetch("/image");
14if (!response.ok) {
15throw new Error(`HTTP error! status: ${response.status}`);
22});
2324setImageData(dataUrl);
25} catch (error) {
26console.error("Error fetching image:", error);
27setError(error instanceof Error ? error.message : "Error loading image");
28}
29}
30fetchImage();
31}, []);
3233return (
34<div>
35<h1>Image from Blob Storage</h1>
36{imageData
37? (
38<img
39src={imageData}
40alt="Fetched from blob storage"
41style={{ maxWidth: "100%" }}
44: error
45? <p>Error: {error}</p>
46: <p>Loading image...</p>}
47</div>
48);
5859export default async function server(request: Request): Promise<Response> {
60if (request.url.endsWith("/image")) {
61try {
62const response = await blob.get("test.png");
6364return new Response(response.body, {
65headers: { "Content-Type": "image/png" },
66});
67} catch (error) {
79<html>
80<head>
81<title>Image from Blob Storage</title>
82<style>${css}</style>
83</head>
getJsonAndRenderAsImageREADME.md13 matches
1# getJsonAndRenderAsImage
23Shows how to get a JSON object containing a base64 imaged stored in Val Town blob storage and render it as an image in the DOM with React.
45## Setup
67Make sure you have a JSON object named `image-test` in your Val Town blob storage (alternatively, you can change the key name in the blob getter in the script).
89The object should look like this:
11```json
12{
13"image": "base64stringgoeshere"
14}
15```
1617To easily upload an image to your blob storage, [fork this val](
18getBlobAndRenderAsImage), run it, and enter your API key in the password input.
1920## How it works
221. Fetching the JSON:
2324- The client-side React component makes a fetch request to "/image".
25- This request is handled by our server function.
26282. Server-side handling:
2930- The server function calls `blob.getJSON("image-test")`.
31- This `blob.getJSON()` method makes an HTTP request to the Val Town API.
32- The API returns a Response object containing the JSON data.
546. Creating a data URL:
5556- We extract the image data from the JSON object at `data.image`.
57- We prepend the data URL prefix to the image data.
58- This data URL is a base64-encoded string representing the image.
59606465668. Rendering the image:
6768- We use the data URL as the src attribute of an `<img>` tag.
69- The browser decodes the base64 data and renders the image.
7071## Further reading on Val Town blobs
hackerNewsDigestREADME.md1 match
456<img width="400px" src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/2661d748-d7a7-4d1e-85a4-f60fae262000/public" />
7
1011<div align="center">
12<img src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/67a1d35e-c37c-41a4-0e5a-03a9ba585d00/public" width="500px"/>
13</div>
45<div align="center">
6<img src="https://imagedelivery.net/iHX6Ovru0O7AjmyT5yZRoA/67a1d35e-c37c-41a4-0e5a-03a9ba585d00/public" width="700px"/>
7</div>
placeholderKittenImagesmain.tsx28 matches
1// This val creates a publicly accessible kitten image generator using the Val Town image generation API.
2// It supports generating square images with a single dimension parameter or rectangular images with two dimension parameters.
34export default async function server(request: Request): Promise<Response> {
13<meta charset="UTF-8">
14<meta name="viewport" content="width=device-width, initial-scale=1.0">
15<title>Kitten Image Generator</title>
16<style>
17body {
31margin-bottom: 20px;
32}
33.example-image {
34display: block;
35max-width: 100%;
75</head>
76<body>
77<h1>Welcome to the Kitten Image Generator!</h1>
78<div class="instructions">
79<p>Use this service to generate cute kitten images of various sizes:</p>
80<p>For square images: <code>/width</code> (e.g., <code>/400</code> for a 400x400 image)</p>
81<p>For rectangular images: <code>/width/height</code> (e.g., <code>/400/300</code> for a 400x300 image)</p>
82</div>
83<div class="form-container">
84<h2>Generate a Kitten Image</h2>
85<form id="kittenForm">
86<label for="width">Width (64-1024):</label>
91</form>
92</div>
93<p>Here's an example of a generated 400x400 kitten image:</p>
94<img src="/400" alt="Example 400x400 kitten" class="example-image">
95<p>Start generating your own kitten images by using the form above or modifying the URL!</p>
96<script>
97document.getElementById('kittenForm').addEventListener('submit', function(e) {
111112if (parts.length === 1) {
113// Square image
114width = height = parseInt(parts[0]);
115} else if (parts.length === 2) {
116// Rectangular image
117width = parseInt(parts[0]);
118height = parseInt(parts[1]);
119} else {
120return new Response('Invalid URL format. Use /width for square images or /width/height for rectangular images.',
121{ status: 400, headers: { 'Content-Type': 'text/plain' } });
122}
132133const prompt = `A cute kitten, high quality, detailed`;
134const imageGenerationUrl = `https://maxm-imggenurl.web.val.run/${encodeURIComponent(prompt)}?width=${width}&height=${height}`;
135136try {
137console.log(`Generating image with dimensions: ${width}x${height}`);
138const imageResponse = await fetch(imageGenerationUrl);
139console.log(`Response status: ${imageResponse.status}`);
140141if (!imageResponse.ok) {
142throw new Error(`Failed to generate image: ${imageResponse.status} ${imageResponse.statusText}`);
143}
144145const imageBlob = await imageResponse.blob();
146console.log(`Successfully generated image, size: ${imageBlob.size} bytes`);
147148// Return the image as-is, without any processing
149return new Response(imageBlob, {
150headers: {
151'Content-Type': 'image/png',
152'Cache-Control': 'public, max-age=86400',
153},
154});
155} catch (error) {
156console.error('Error generating image:', error.message);
157return new Response(`Failed to generate image: ${error.message}`, { status: 500, headers: { 'Content-Type': 'text/plain' } });
158}
159}