Skip to content

Commit

Permalink
Add resize_mode option to watermark filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymarsden committed Sep 26, 2024
1 parent 657c771 commit d613ce5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ imagor supports the following filters:
- `strip_icc()` removes ICC profile information from the resulting image
- `strip_metadata()` removes all metadata from the resulting image
- `upscale()` upscale the image if `fit-in` is used
- `watermark(image, x, y, alpha [, w_ratio [, h_ratio]])` adds a watermark to the image. It can be positioned inside the image with the alpha channel specified and optionally resized based on the image size by specifying the ratio
- `watermark(image, x, y, alpha [, w_ratio [, h_ratio [, resize_mode]]])` adds a watermark to the image. It can be positioned inside the image with the alpha channel specified and optionally resized based on the image size by specifying the ratio
- `image` watermark image URI, using the same image loader configured for imagor
- `x` horizontal position that the watermark will be in:
- Positive number indicate position from the left, negative number from the right.
Expand All @@ -149,6 +149,7 @@ imagor supports the following filters:
- `alpha` watermark image transparency, a number between 0 (fully opaque) and 100 (fully transparent).
- `w_ratio` percentage of the width of the image the watermark should fit-in
- `h_ratio` percentage of the height of the image the watermark should fit-in
- `resize_mode` resize mode of the watermark. If 'force', the watermark will be resized to w_ratio and h_ratio regardless of the watermark's aspect ratio. Otherwise, the watermark will be resized to fit within the w_ratio and h_ratio, respecting its aspect ratio.

#### Utility Filters

Expand Down
8 changes: 7 additions & 1 deletion vips/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ func (v *Processor) watermark(ctx context.Context, img *Image, load imagor.LoadF
var down = 1
var overlay *Image
var n = 1
resizeMode := SizeBoth
if isAnimated(img) {
n = -1
}
// resize_mode
if ln >= 7 && args[6] == "force" {
resizeMode = SizeForce
}
// w_ratio h_ratio
if ln >= 6 {
w = img.Width()
Expand All @@ -47,8 +52,9 @@ func (v *Processor) watermark(ctx context.Context, img *Image, load imagor.LoadF
h, _ = strconv.Atoi(args[5])
h = img.PageHeight() * h / 100
}

if overlay, err = v.NewThumbnail(
ctx, blob, w, h, InterestingNone, SizeBoth, n, 1, 0,
ctx, blob, w, h, InterestingNone, resizeMode, n, 1, 0,
); err != nil {
return
}
Expand Down

0 comments on commit d613ce5

Please sign in to comment.