Transform user-uploaded images before uploading to R2
In this guide, you will build an app that accepts image uploads, overlays the image with a visual watermark, then stores the transformed image in your R2 bucket.
With Images, you have the flexibility to choose where your original images are stored. You can transform images that are stored outside of the Images product, like in R2.
When you store user-uploaded media in R2, you may want to optimize or manipulate images before they are uploaded to your R2 bucket.
You will learn how to connect Developer Platform services to your Worker through bindings, as well as use various optimization features in the Images API.
Before you begin, you will need to do the following:
- Add an Images Paid subscription to your account. This allows you to bind the Images API to your Worker.
- Create an R2 bucket, where the transformed images will be uploaded.
- Create a new Worker project.
If you are new, review how to create your first Worker.
To start, you will need to set up your project to use the following resources on the Developer Platform:
- Images to transform, resize, and encode images directly from your Worker.
- R2 to connect the bucket for storing transformed images.
- Assets to access a static image that will be used as the visual watermark.
Configure your wrangler.toml
file to add the Images, R2, and Assets bindings:
{ "images": { "binding": "IMAGES" }, "r2_buckets": [ { "binding": "R2", "bucket_name": "<BUCKET>" } ], "assets": { "directory": "./<DIRECTORY>", "binding": "ASSETS" }}
[images]binding = "IMAGES"
[[r2_buckets]]binding = "R2"bucket_name = "<BUCKET>"
[assets]directory = "./<DIRECTORY>"binding = "ASSETS"
Replace <BUCKET>
with the name of the R2 bucket where you will upload the images after they are transformed. In your Worker code, you will be able to refer to this bucket using env.R2.
Replace ./<DIRECTORY>
with the name of the project's directory where the overlay image will be stored. In your Worker code, you will be able to refer to these assets using env.ASSETS
.
Because we want to apply a visual watermark to every uploaded image, you need a place to store the overlay image.
The assets directory of your project lets you upload static assets as part of your Worker. When you deploy your project, these uploaded files, along with your Worker code, are deployed to Cloudflare's infrastructure in a single operation.
After you configure your Wrangler file, upload the overlay image to the specified directory. In our example app, the directory ./assets
contains the overlay image.
You will need to build the interface for the app that lets users upload images.
In this example, the frontend is rendered directly from the Worker script.
To do this, make a new html
variable, which contains a form
element for accepting uploads. In fetch
, construct a new Response
with a Content-Type: text/html
header to serve your static HTML site to the client:
const html = `<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Upload Image</title> </head> <body> <h1>Upload an image</h1> <form method="POST" enctype="multipart/form-data"> <input type="file" name="image" accept="image/*" required /> <button type="submit">Upload</button> </form> </body> </html>`;
export default { async fetch(request, env) { if (request.method === "GET") { return new Response(html, {headers:{'Content-Type':'text/html'},}) } if (request.method ==="POST") { // This is called when the user submits the form } }};