Skip to content

Commit f4dfde1

Browse files
committed
Improved the sharp docs
1 parent 45254e6 commit f4dfde1

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

docs/guides/examples/sharp-image-processing.mdx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ import LocalDevelopment from "/snippets/local-development-extensions.mdx";
88

99
## Overview
1010

11-
This task optimizes and watermarks an image using the Sharp library, and then uploads the processed image to R2 storage.
11+
This task processes and watermarks an image using the Sharp library, and then uploads it to R2 storage.
1212

13-
## Adding build configurations
13+
## Prerequisites
14+
15+
- A project with [Trigger.dev initialized](/quick-start)
16+
- The [Sharp](https://sharp.pixelplumbing.com/install) library installed on your machine
17+
- An R2-compatible object storage service, such as [Cloudflare R2](https://developers.cloudflare.com/r2)
18+
19+
## Adding the build configuration
1420

1521
To use this example, you'll first need to add these build settings to your `trigger.config.ts` file:
1622

@@ -34,22 +40,21 @@ export default defineConfig({
3440

3541
## Key features
3642

37-
- Resizes and rotates an image
38-
- Adds a watermark to the image
39-
- Uploads the processed image to R2 storage
43+
- Resizes a JPEG image to 800x800 pixels
44+
- Adds a watermark to the image, positioned in the bottom-right corner, using a PNG image
45+
- Uploads the processed image to R2 storage
4046

4147
## Task code
4248

4349
```ts trigger/sharp-image-processing.ts
4450
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
4551
import { logger, task } from "@trigger.dev/sdk/v3";
4652
import fs from "fs/promises";
47-
import fetch from "node-fetch";
4853
import os from "os";
4954
import path from "path";
5055
import sharp from "sharp";
5156

52-
// Initialize R2 client
57+
// Initialize R2 client using your R2 account details
5358
const r2Client = new S3Client({
5459
region: "auto",
5560
endpoint: process.env.R2_ENDPOINT,
@@ -61,6 +66,10 @@ const r2Client = new S3Client({
6166

6267
export const sharpProcessImage = task({
6368
id: "sharp-process-image",
69+
// Only retry this task once if it fails
70+
retry: {
71+
maxAttempts: 1,
72+
},
6473
run: async (payload: { imageUrl: string; watermarkUrl: string }) => {
6574
const { imageUrl, watermarkUrl } = payload;
6675

@@ -76,10 +85,9 @@ export const sharpProcessImage = task({
7685
const imageBuffer = await imageResponse.arrayBuffer();
7786
const watermarkBuffer = await watermarkResponse.arrayBuffer();
7887

79-
// Optimize the image using Sharp
88+
// Process the image using Sharp
8089
await sharp(Buffer.from(imageBuffer))
81-
.rotate(90) // Rotate the image by 90 degrees
82-
.resize(800, 600) // Resize the image to 800x600
90+
.resize(800, 800) // Resize the image to 800x800
8391
.composite([
8492
{
8593
input: Buffer.from(watermarkBuffer),
@@ -89,34 +97,32 @@ export const sharpProcessImage = task({
8997
.toFormat("jpeg")
9098
.toFile(outputPath);
9199

92-
// Log the output file path
93-
logger.log(`Optimized image saved at: ${outputPath}`);
100+
logger.log(`Image processed.`);
94101

95-
// Read the optimized image file
96-
const optimizedImageBuffer = await fs.readFile(outputPath);
102+
// Read the processed image file
103+
const processedImageBuffer = await fs.readFile(outputPath);
97104

98-
// Upload the optimized image to R2, replacing slashes with underscores
105+
// Upload the image to R2, replacing slashes with underscores
99106
const r2Key = `processed-images/${path.basename(outputPath)}`;
100107

101108
const uploadParams = {
102109
Bucket: process.env.R2_BUCKET,
103110
Key: r2Key,
104-
Body: optimizedImageBuffer,
111+
Body: processedImageBuffer,
105112
};
106113

107114
// Upload the image to R2 and get the URL
108115
await r2Client.send(new PutObjectCommand(uploadParams));
109-
const r2Url = `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com/${process.env.R2_BUCKET}/${r2Key}`;
110-
logger.log("Optimized image uploaded to R2", { url: r2Url });
116+
logger.log("Processed image uploaded to R2", {
117+
path: `/${process.env.R2_BUCKET}/${r2Key}`,
118+
});
111119

112120
// Delete the temporary file
113121
await fs.unlink(outputPath);
122+
logger.log("Temporary file deleted", { outputPath: outputPath });
114123

115-
// Return the optimized image buffer, file path, and R2 URL
116124
return {
117-
optimizedImageBuffer,
118-
optimizedImagePath: outputPath,
119-
r2Url,
125+
message: `New image uploaded to /${process.env.R2_BUCKET}/${r2Key}`,
120126
};
121127
},
122128
});
@@ -133,4 +139,4 @@ To test this task in the dashboard, you can use the following payload:
133139
}
134140
```
135141

136-
<LocalDevelopment packages={"the Sharp image processing library"} />
142+
<LocalDevelopment packages={"the Sharp image processing library"} />

0 commit comments

Comments
 (0)