Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// This val creates a kitten image generator using the Val Town image generation API.
// It supports generating square images with a single dimension parameter or rectangular images with two dimension parameters.
export default async function server(request: Request): Promise<Response> {
const url = new URL(request.url);
const parts = url.pathname.split('/').filter(Boolean);
if (parts.length === 0) {
const welcomeHtml = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kitten Image Generator</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #2c3e50;
text-align: center;
}
p {
margin-bottom: 20px;
}
.example-image {
display: block;
max-width: 100%;
height: auto;
margin: 20px auto;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.instructions {
background-color: #fff;
border-left: 4px solid #3498db;
padding: 15px;
margin-bottom: 20px;
border-radius: 0 5px 5px 0;
}
code {
background-color: #e7e7e7;
padding: 2px 4px;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Welcome to the Kitten Image Generator!</h1>
<div class="instructions">
<p>Use this service to generate cute kitten images of various sizes:</p>
<p>For square images: <code>/width</code> (e.g., <code>/400</code> for a 400x400 image)</p>
<p>For rectangular images: <code>/width/height</code> (e.g., <code>/400/300</code> for a 400x300 image)</p>
</div>
<p>Here's an example of a generated 400x400 kitten image:</p>
<img src="/400" alt="Example 400x400 kitten" class="example-image">
<p>Start generating your own kitten images by modifying the URL!</p>
</body>
</html>
`;
return new Response(welcomeHtml, { headers: { 'Content-Type': 'text/html' } });
}
let width: number, height: number;
if (parts.length === 1) {
// Square image
width = height = parseInt(parts[0]);
} else if (parts.length === 2) {
// Rectangular image
width = parseInt(parts[0]);
height = parseInt(parts[1]);
} else {
return new Response('Invalid URL format. Use /width for square images or /width/height for rectangular images.',
{ status: 400, headers: { 'Content-Type': 'text/plain' } });
}
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0) {
return new Response('Invalid dimensions. Please use positive integers.',
{ status: 400, headers: { 'Content-Type': 'text/plain' } });
}
// Ensure dimensions are within acceptable range
width = Math.min(Math.max(width, 64), 1024);
height = Math.min(Math.max(height, 64), 1024);
const prompt = `A cute kitten, high quality, detailed`;
const imageGenerationUrl = `https://maxm-imggenurl.web.val.run/${encodeURIComponent(prompt)}?width=${width}&height=${height}`;
try {
console.log(`Generating image with dimensions: ${width}x${height}`);
const imageResponse = await fetch(imageGenerationUrl);
console.log(`Response status: ${imageResponse.status}`);
shawnbasquiat-placeholderimages.web.val.run
August 30, 2024