Skip to content

Commit d8de639

Browse files
dcpenaOxyjunns476
authored
[Images] Added tutorials and examples (#21345)
* Added tutorials and examples * Apply suggestions from code review Co-authored-by: Nicholas Skehin <[email protected]> * Using WranglerConfig component --------- Co-authored-by: Jun Lee <[email protected]> Co-authored-by: Nicholas Skehin <[email protected]>
1 parent c87eebe commit d8de639

File tree

5 files changed

+424
-0
lines changed

5 files changed

+424
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
type: overview
3+
hideChildren: true
4+
pcx_content_type: navigation
5+
title: Examples
6+
sidebar:
7+
order: 11
8+
---
9+
10+
import { ListExamples } from "~/components";
11+
12+
<ListExamples directory="images/examples/" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
type: example
3+
summary: Transcode an image from Workers AI before uploading to R2
4+
pcx_content_type: example
5+
title: Transcode images
6+
sidebar:
7+
order: 5
8+
description: Transcode an image from Workers AI before uploading to R2
9+
---
10+
11+
```js
12+
const stream = await env.AI.run(
13+
"@cf/bytedance/stable-diffusion-xl-lightning",
14+
{
15+
prompt: YOUR_PROMPT_HERE
16+
}
17+
);
18+
19+
// Convert to AVIF
20+
const image = (
21+
await env.IMAGES.input(stream)
22+
.output({format: "image/avif"})
23+
).response();
24+
25+
const fileName = "image.avif";
26+
27+
// Upload to R2
28+
await env.R2.put(fileName, image.body);
29+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
type: example
3+
summary: Draw a watermark from KV on an image from R2
4+
pcx_content_type: example
5+
title: Watermarks
6+
sidebar:
7+
order: 5
8+
description: Draw a watermark from KV on an image from R2
9+
---
10+
11+
```ts
12+
interface Env {
13+
BUCKET: R2Bucket,
14+
NAMESPACE: KVNamespace,
15+
IMAGES: ImagesBinding,
16+
}
17+
export default {
18+
async fetch(request, env, ctx): Promise<Response> {
19+
const watermarkKey = "my-watermark";
20+
const sourceKey = "my-source-image";
21+
22+
const cache = await caches.open("transformed-images");
23+
const cacheKey = new URL(sourceKey + "/" + watermarkKey, request.url);
24+
const cacheResponse = await cache.match(cacheKey);
25+
26+
if (cacheResponse) {
27+
return cacheResponse;
28+
}
29+
30+
let watermark = await env.NAMESPACE.get(watermarkKey, "stream");
31+
let source = await env.BUCKET.get(sourceKey);
32+
33+
if (!watermark || !source) {
34+
return new Response("Not found", { status: 404 });
35+
}
36+
37+
const result = await env.IMAGES.input(source.body)
38+
.draw(watermark)
39+
.output({ format: "image/jpeg" });
40+
41+
const response = result.response();
42+
43+
ctx.waitUntil(cache.put(cacheKey, response.clone()));
44+
45+
return result.response();
46+
},
47+
} satisfies ExportedHandler<Env>;
48+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Tutorials
3+
pcx_content_type: navigation
4+
sidebar:
5+
order: 10
6+
group:
7+
hideIndex: true
8+
---
9+
10+
import { DirectoryListing } from "~/components";
11+
12+
<DirectoryListing />

0 commit comments

Comments
 (0)