This project demonstrates how to create a custom API endpoint in a Next.js application to add watermarks to images using the Sharp image processing library. The project features an API route that processes an image by adding text-based watermarks and a React component for fetching and displaying the watermarked image.
- Custom API Endpoint: Built using Next.js API routes to handle image processing.
- Watermarking with Sharp: Applies text-based watermarks with high performance and quality.
- Client-Side Integration: A React component fetches and displays the watermarked image.
- Path:
/api/watermark
- Method:
GET
- Parameters:
imageUrl
: Path to the image file (relative to thepublic
directory).watermarkText
: Text to be used as the watermark.
Functionality:
- Retrieve Parameters: Parses the
imageUrl
andwatermarkText
from the request query parameters. - Load Image: Reads the image file from the
public
directory. - Apply Watermark: Uses Sharp to overlay the watermark text on the image.
- Respond with Image: Returns the watermarked image as a JPEG file.
- File:
page.js
- Function: Fetches and displays the watermarked image.
Functionality:
- Fetch Watermarked Image: Uses the
fetch
API to call the/api/watermark
endpoint with the sample image and watermark text. - Display Image: Updates the component state with the URL of the fetched image and renders it using the Next.js
Image
component.
-
Clone the Repository:
git clone https://github.com/your-username/your-repo-name.git
Navigate to the Project Directory, Install Dependencies, Start the Development Server, Open your browser and navigate to http://localhost:3000. You should see the watermarked image displayed.