/
watermark.go
69 lines (64 loc) · 1.99 KB
/
watermark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2022 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package imagepipeline
import (
"context"
"image"
"image/color"
"github.com/disintegration/imaging"
)
func getWatermarkPosition(position string, w, h, watermarkWidth, watermarkHeight int) (int, int) {
x := 0
y := 0
// PositionTopLeft 为0,0 不需要处理
switch position {
case PositionTop:
x = (w - watermarkWidth) / 2
case PositionTopRight:
x = w - watermarkWidth
case PositionLeft:
y = (h - watermarkHeight) / 2
case PositionCenter:
x = (w - watermarkWidth) / 2
y = (h - watermarkHeight) / 2
case PositionRight:
y = (h - watermarkHeight) / 2
x = w - watermarkWidth
case PositionBottomLeft:
y = h - watermarkHeight
case PositionBottom:
x = (w - watermarkWidth) / 2
y = h - watermarkHeight
case PositionBottomRight:
x = w - watermarkWidth
y = h - watermarkHeight
}
return x, y
}
// NewWatermark creates an image job, which will add watermark to image
func NewWatermark(watermarkImg image.Image, position string, angle float64) Job {
return func(_ context.Context, img *Image) (*Image, error) {
if angle != 0 {
watermarkImg = imaging.Rotate(watermarkImg, angle, color.Transparent)
}
w := img.Width()
h := img.Height()
watermarkWidth := watermarkImg.Bounds().Dx()
watermarkHeight := watermarkImg.Bounds().Dy()
x, y := getWatermarkPosition(position, w, h, watermarkWidth, watermarkHeight)
grid := imaging.Paste(img.grid, watermarkImg, image.Pt(x, y))
img.Set(grid)
return img, nil
}
}