Skip to content

Commit 4c6f27e

Browse files
authored
Updated sharp docs (#1403)
* Added prerequisites to ffmpeg and puppeteer * Improved the sharp docs * Updated sharp task code
1 parent 367d7bd commit 4c6f27e

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

docs/guides/examples/ffmpeg-video-processing.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ description: "These examples show you how to process videos in various ways usin
66

77
import LocalDevelopment from "/snippets/local-development-extensions.mdx";
88

9-
## Adding the FFmpeg build extension
9+
## Prerequisites
10+
11+
- A project with [Trigger.dev initialized](/quick-start)
12+
- [FFmpeg](https://www.ffmpeg.org/download.html) installed on your machine
13+
14+
### Adding the FFmpeg build extension
1015

1116
To use these example tasks, you'll first need to add our FFmpeg extension to your project configuration like this:
1217

docs/guides/examples/puppeteer.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ description: "These examples demonstrate how to use Puppeteer with Trigger.dev."
77
import LocalDevelopment from "/snippets/local-development-extensions.mdx";
88
import ScrapingWarning from "/snippets/web-scraping-warning.mdx";
99

10+
## Prerequisites
11+
12+
- A project with [Trigger.dev initialized](/quick-start)
13+
- [Puppeteer](https://pptr.dev/guides/installation) installed on your machine
14+
1015
## Overview
1116

1217
There are 3 example tasks to follow on this page:
@@ -15,7 +20,7 @@ There are 3 example tasks to follow on this page:
1520
2. [Generate a PDF from a web page](/guides/examples/puppeteer#generate-a-pdf-from-a-web-page)
1621
3. [Scrape content from a web page](/guides/examples/puppeteer#scrape-content-from-a-web-page)
1722

18-
<ScrapingWarning/>
23+
<ScrapingWarning />
1924

2025
## Build configurations
2126

@@ -133,7 +138,6 @@ export const puppeteerWebpageToPDF = task({
133138
return { pdfUrl: s3Url };
134139
},
135140
});
136-
137141
```
138142

139143
### Testing your task
@@ -146,9 +150,10 @@ There's no payload required for this task so you can just click "Run test" from
146150

147151
In this example we use [Puppeteer](https://pptr.dev/) with a [BrowserBase](https://www.browserbase.com/) proxy to scrape the GitHub stars count from the [Trigger.dev](https://trigger.dev) landing page and log it out. See [this list](/guides/examples/puppeteer#proxying) for more proxying services we recommend.
148152

149-
<Note>
150-
When web scraping, you MUST use the technique below which uses a proxy with Puppeteer. Direct scraping without using `browserWSEndpoint` is prohibited and will result in account suspension.
151-
</Note>
153+
<Warning>
154+
When web scraping, you MUST use the technique below which uses a proxy with Puppeteer. Direct
155+
scraping without using `browserWSEndpoint` is prohibited and will result in account suspension.
156+
</Warning>
152157

153158
### Task code
154159

@@ -209,4 +214,4 @@ Here are a list of proxy services we recommend:
209214
- [Browserless](https://browserless.io/)
210215
- [Oxylabs](https://oxylabs.io/)
211216
- [ScrapingBee](https://scrapingbee.com/)
212-
- [Smartproxy](https://smartproxy.com/)
217+
- [Smartproxy](https://smartproxy.com/)

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

Lines changed: 43 additions & 49 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,22 @@ 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
44-
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
50+
import { S3Client } from "@aws-sdk/client-s3";
51+
import { Upload } from "@aws-sdk/lib-storage";
4552
import { logger, task } from "@trigger.dev/sdk/v3";
4653
import fs from "fs/promises";
47-
import fetch from "node-fetch";
4854
import os from "os";
4955
import path from "path";
5056
import sharp from "sharp";
5157

52-
// Initialize R2 client
58+
// Initialize R2 client using your R2 account details
5359
const r2Client = new S3Client({
5460
region: "auto",
5561
endpoint: process.env.R2_ENDPOINT,
@@ -61,63 +67,51 @@ const r2Client = new S3Client({
6167

6268
export const sharpProcessImage = task({
6369
id: "sharp-process-image",
70+
retry: { maxAttempts: 1 },
6471
run: async (payload: { imageUrl: string; watermarkUrl: string }) => {
6572
const { imageUrl, watermarkUrl } = payload;
73+
const outputPath = path.join(os.tmpdir(), `output_${Date.now()}.jpg`);
6674

67-
// Generate temporary and output file names
68-
const tempDirectory = os.tmpdir();
69-
const outputPath = path.join(tempDirectory, `output_${Date.now()}.jpg`);
70-
71-
// Fetch the image and watermark
7275
const [imageResponse, watermarkResponse] = await Promise.all([
7376
fetch(imageUrl),
7477
fetch(watermarkUrl),
7578
]);
7679
const imageBuffer = await imageResponse.arrayBuffer();
7780
const watermarkBuffer = await watermarkResponse.arrayBuffer();
7881

79-
// Optimize the image using Sharp
8082
await sharp(Buffer.from(imageBuffer))
81-
.rotate(90) // Rotate the image by 90 degrees
82-
.resize(800, 600) // Resize the image to 800x600
83+
.resize(800, 800) // Resize the image to 800x800px
8384
.composite([
8485
{
8586
input: Buffer.from(watermarkBuffer),
8687
gravity: "southeast", // Position the watermark in the bottom-right corner
8788
},
8889
])
89-
.toFormat("jpeg")
90-
.toFile(outputPath);
91-
92-
// Log the output file path
93-
logger.log(`Optimized image saved at: ${outputPath}`);
94-
95-
// Read the optimized image file
96-
const optimizedImageBuffer = await fs.readFile(outputPath);
97-
98-
// Upload the optimized image to R2, replacing slashes with underscores
99-
const r2Key = `processed-images/${path.basename(outputPath)}`;
100-
101-
const uploadParams = {
102-
Bucket: process.env.R2_BUCKET,
103-
Key: r2Key,
104-
Body: optimizedImageBuffer,
105-
};
106-
107-
// Upload the image to R2 and get the URL
108-
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 });
111-
112-
// Delete the temporary file
113-
await fs.unlink(outputPath);
114-
115-
// Return the optimized image buffer, file path, and R2 URL
116-
return {
117-
optimizedImageBuffer,
118-
optimizedImagePath: outputPath,
119-
r2Url,
120-
};
90+
.jpeg() // Convert to jpeg
91+
.toBuffer() // Convert to buffer
92+
.then(async (outputBuffer) => {
93+
await fs.writeFile(outputPath, outputBuffer); // Write the buffer to file
94+
95+
const r2Key = `processed-images/${path.basename(outputPath)}`;
96+
const uploadParams = {
97+
Bucket: process.env.R2_BUCKET,
98+
Key: r2Key,
99+
Body: await fs.readFile(outputPath),
100+
};
101+
102+
const upload = new Upload({
103+
client: r2Client,
104+
params: uploadParams,
105+
});
106+
107+
await upload.done();
108+
logger.log("Image uploaded to R2 storage.", {
109+
path: `/${process.env.R2_BUCKET}/${r2Key}`,
110+
});
111+
112+
await fs.unlink(outputPath); // Clean up the temporary file
113+
return { r2Key };
114+
});
121115
},
122116
});
123117
```
@@ -133,4 +127,4 @@ To test this task in the dashboard, you can use the following payload:
133127
}
134128
```
135129

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

0 commit comments

Comments
 (0)