Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resize_mode option to watermark filter #484

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading