MCPcopy Create free account
hub / github.com/imgproxy/imgproxy / prepareWatermark

Method prepareWatermark

processing/watermark.go:31–104  ·  view source on GitHub ↗
(
	ctx context.Context,
	wm *vips.Image,
	wmData imagedata.ImageData,
	po ProcessingOptions,
	imgWidth, imgHeight int,
	offsetScale float64,
	framesCount int, //nolint:unparam
)

Source from the content-addressed store, hash-verified

29}
30
31func (p *Processor) prepareWatermark(
32 ctx context.Context,
33 wm *vips.Image,
34 wmData imagedata.ImageData,
35 po ProcessingOptions,
36 imgWidth, imgHeight int,
37 offsetScale float64,
38 framesCount int, //nolint:unparam
39) error {
40 if err := wm.Load(wmData, 1.0, 0, 1); err != nil {
41 return err
42 }
43
44 _, _, err := p.checkImageSize(wm, wmData.Format(), po)
45 if err != nil {
46 return err
47 }
48
49 wmPo := p.NewProcessingOptions(options.New())
50 wmPo.Set(keys.ResizingType, ResizeFit)
51 wmPo.Set(keys.Dpr, 1)
52 wmPo.Set(keys.Enlarge, true)
53 wmPo.Set(keys.Format, wmData.Format())
54
55 if scale := po.WatermarkScale(); scale > 0 {
56 wmPo.Set(keys.Width, max(imath.ScaleToEven(imgWidth, scale), 1))
57 wmPo.Set(keys.Height, max(imath.ScaleToEven(imgHeight, scale), 1))
58 }
59
60 shouldReplicate := shouldReplicateWatermark(po.WatermarkPosition())
61
62 if shouldReplicate {
63 offsetX := po.WatermarkXOffset()
64 offsetY := po.WatermarkYOffset()
65
66 var padX, padY int
67
68 if math.Abs(offsetX) >= 1.0 {
69 padX = imath.RoundToEven(offsetX * offsetScale)
70 } else {
71 padX = imath.ScaleToEven(imgWidth, offsetX)
72 }
73
74 if math.Abs(offsetY) >= 1.0 {
75 padY = imath.RoundToEven(offsetY * offsetScale)
76 } else {
77 padY = imath.ScaleToEven(imgHeight, offsetY)
78 }
79
80 wmPo.Set(keys.PaddingLeft, padX/2)
81 wmPo.Set(keys.PaddingRight, padX-padX/2)
82 wmPo.Set(keys.PaddingTop, padY/2)
83 wmPo.Set(keys.PaddingBottom, padY-padY/2)
84 }
85
86 if err := p.watermarkPipeline().Run(ctx, wm, wmPo, wmData); err != nil {
87 return err
88 }

Callers 1

applyWatermarkMethod · 0.95

Calls 15

checkImageSizeMethod · 0.95
NewProcessingOptionsMethod · 0.95
watermarkPipelineMethod · 0.95
NewFunction · 0.92
ScaleToEvenFunction · 0.92
RoundToEvenFunction · 0.92
shouldReplicateWatermarkFunction · 0.85
LoadMethod · 0.80
WatermarkScaleMethod · 0.80
WatermarkPositionMethod · 0.80
WatermarkXOffsetMethod · 0.80
WatermarkYOffsetMethod · 0.80

Tested by

no test coverage detected