Skip to content

Commit

Permalink
first version 👍
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Feb 23, 2024
1 parent 714579d commit 4811a51
Show file tree
Hide file tree
Showing 20 changed files with 2,097 additions and 19 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Docker Image CI

on:
push:
branches:
- 'main'
tags:
- 'v*'

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/[email protected]
with:
images: codeinchq/watermarker

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
target: watermarker-prod
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
/node_modules
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/watermarker.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
####################################################################################################
# WATERMARKER
####################################################################################################
FROM --platform=$TARGETPLATFORM node:21-alpine AS watermarker

ENV PORT=3000
ENV NODE_ENV=production
WORKDIR /app


####################################################################################################
# WATERMARKER dev
####################################################################################################
FROM watermarker AS watermarker-dev

ENV NODE_ENV=development
RUN npm install --global nodemon

EXPOSE $PORT
ENTRYPOINT ["nodemon", "main.mjs"]


####################################################################################################
# WATERMARKER prod
####################################################################################################
FROM watermarker AS watermarker-prod

COPY main.mjs /app/
COPY package.json /app/
COPY package-lock.json /app/
RUN mkdir -p /app/temp
RUN npm install --omit=dev

EXPOSE $PORT
ENTRYPOINT ["node", "main.mjs"]
22 changes: 4 additions & 18 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
MIT License
Copyright 2024 Code Inc. / Joan Fabrégat <[email protected]>

Copyright (c) 2024 Joan Fabrégat
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,61 @@
# watermarker
A containerized REST API for applying watermarks to images and PDF documents

[![Docker Image CI](https://github.com/codeinchq/watermarker/actions/workflows/docker-image.yml/badge.svg)](https://github.com/codeinchq/watermarker/actions/workflows/docker-image.yml)

This repository contains a simple containerized REST API to apply a watermark to an image. The server is written in Javascript and uses the [Jimp](https://www.npmjs.com/package/jimp) library to process the images.

The image is available on [Docker Hub](https://hub.docker.com/r/codeinchq/watermarker) under the name `codeinchq/watermarker`.

## Configuration

By default, the container listens on port 3000. The port is configurable using the `PORT` environment variable.

## Usage

All requests must by send in POST to the `/apply` endpoint with a `multipart/form-data` content type. The request must
contain two files:
* `image`: The image to apply the watermark to.
* `watermark`: The watermark to apply to the image.

Additional parameters can be sent to customize the conversion process:
* `ratio`: The ratio of the watermark to the image. The value must be a float between 0 and 1. The default value is 0.5.
* `position`: The position of the watermark. The value must be one of `center`, `top-left`, `top`, `top-right`, `left`, `right`, `bottom-left`, `bottom`, `bottom-right`. The default value is `center`.
* `padding`: The padding between the watermark and the edge of the image. The value must be an integer. The default value is 10. This is ignored if the position is `center`.
* `format`: The format of the output image. The value must be one of `jpg`, `png`, `gif`. The default value is `png`.
* `quality`: The quality of the output image. The value must be an integer between 0 and 100. The default value is 100. This is ignored if the format is `gif` or `png`.
* `blur`: The blur radius of background behind the watermark. The value must be an integer. The default value is 0.
* `opacity`: The opacity of the watermark. The value must be an integer between 0 and 100. The default value is 100.

The server returns `200` if the conversion was successful and the images are available in the response body. In case of
error, the server returns a `400` status code with a JSON object containing the error message (
format: `{error: string}`).

### Example

#### Step 1: run the container using Docker

```bash
docker run -p "3000:3000" codeinchq/watermarker
```

#### Step 2: apply the watermark to an image

A simple scenario to apply a watermark to an image using `curl`:
```bash
curl -X POST -F "image=@/path/po/image.png" -F "watermark=@/path/to/watermark.png" http://localhost:3000/apply -o watermarked.png
```

A more complex scenario with additional parameters:
```bash
curl -X POST -F "image=@/path/po/image.png" -F "watermark=@/path/to/watermark.png" http://localhost:3000/apply -F "blur=3" -F "ratio=1" -F "format=jpg" -F "quality=100" -o test-watermarked.jpg
```

## Client

A PHP 8 client is available at on [GitHub](https://github.com/codeinchq/watermarker-php-client)
and [Packagist](https://packagist.org/packages/codeinc/watermarker-client).

## License

This project is licensed under the MIT License - see
the [LICENSE](https://github.com/codeinchq/watermarker?tab=MIT-1-ov-file) file for details.
14 changes: 14 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
watermarker-dev:
image: watermarker-dev
build:
context: .
target: watermarker-dev
platforms:
#- linux/amd64
- linux/arm64
volumes:
- ./:/app/
ports:
- "3000:3000"
tty: true
Loading

0 comments on commit 4811a51

Please sign in to comment.