-
Notifications
You must be signed in to change notification settings - Fork 5
/
overlay.go
76 lines (62 loc) · 1.58 KB
/
overlay.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
70
71
72
73
74
75
76
package effect
import (
"context"
"image"
"github.com/disintegration/imaging"
"github.com/talento90/imgart/imgart"
)
type overlay struct {
imgRepository imgart.ImageRepository
effect
}
// NewOverlay creates an Effect that overlay an image with other image
func NewOverlay(imgRepository imgart.ImageRepository) imgart.Effect {
return &overlay{
imgRepository: imgRepository,
effect: effect{
id: "overlay",
description: "Overlay - Overlay image",
parameters: imgart.Parameters{
"position": imgart.Parameter{
Description: "Position for the overlay image",
Required: true,
Example: "[1,2]",
Type: "array[int]",
},
"url": imgart.Parameter{
Description: "Url for overlay image",
Required: true,
Example: "http://image.png",
Type: "string",
},
"opacity": imgart.Parameter{
Description: "Opacity for overlay image",
Required: false,
Example: 90,
Type: "float",
Default: 100,
},
},
},
}
}
func (o *overlay) Transform(ctx context.Context, img image.Image, params map[string]interface{}) (image.Image, error) {
position, err := pointBinder("position", params)
if err != nil {
return nil, err
}
url, err := urlBinder("url", params)
if err != nil {
return nil, err
}
opacity, err := floatBinder("opacity", params)
if err != nil {
opacity = 100
}
overlayImg, _, err := o.imgRepository.Get(ctx, url.String())
if err != nil {
return nil, err
}
img = imaging.Overlay(img, overlayImg, position, opacity)
return img, ctx.Err()
}