watermark.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package processing
  2. import (
  3. "context"
  4. "math"
  5. "github.com/imgproxy/imgproxy/v3/config"
  6. "github.com/imgproxy/imgproxy/v3/imagedata"
  7. "github.com/imgproxy/imgproxy/v3/imath"
  8. "github.com/imgproxy/imgproxy/v3/options"
  9. "github.com/imgproxy/imgproxy/v3/vips"
  10. )
  11. var watermarkPipeline = pipeline{
  12. prepare,
  13. scaleOnLoad,
  14. importColorProfile,
  15. scale,
  16. rotateAndFlip,
  17. padding,
  18. }
  19. func prepareWatermark(wm *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, imgWidth, imgHeight int, offsetScale float64, framesCount int) error {
  20. if err := wm.Load(wmData, 1, 1.0, 1); err != nil {
  21. return err
  22. }
  23. po := options.NewProcessingOptions()
  24. po.ResizingType = options.ResizeFit
  25. po.Dpr = 1
  26. po.Enlarge = true
  27. po.Format = wmData.Type
  28. if opts.Scale > 0 {
  29. po.Width = imath.Max(imath.ScaleToEven(imgWidth, opts.Scale), 1)
  30. po.Height = imath.Max(imath.ScaleToEven(imgHeight, opts.Scale), 1)
  31. }
  32. if opts.ShouldReplicate() {
  33. var offX, offY int
  34. if math.Abs(opts.Gravity.X) >= 1.0 {
  35. offX = imath.RoundToEven(opts.Gravity.X * offsetScale)
  36. } else {
  37. offX = imath.ScaleToEven(imgWidth, opts.Gravity.X)
  38. }
  39. if math.Abs(opts.Gravity.Y) >= 1.0 {
  40. offY = imath.RoundToEven(opts.Gravity.Y * offsetScale)
  41. } else {
  42. offY = imath.ScaleToEven(imgHeight, opts.Gravity.Y)
  43. }
  44. po.Padding.Enabled = true
  45. po.Padding.Left = offX / 2
  46. po.Padding.Right = offX - po.Padding.Left
  47. po.Padding.Top = offY / 2
  48. po.Padding.Bottom = offY - po.Padding.Top
  49. }
  50. if err := watermarkPipeline.Run(context.Background(), wm, po, wmData); err != nil {
  51. return err
  52. }
  53. if opts.ShouldReplicate() || framesCount > 1 {
  54. // We need to copy image if we're going to replicate.
  55. // Replication requires image to be read several times, and this requires
  56. // random access to pixels
  57. if err := wm.CopyMemory(); err != nil {
  58. return err
  59. }
  60. }
  61. if opts.ShouldReplicate() {
  62. if err := wm.Replicate(imgWidth, imgHeight); err != nil {
  63. return err
  64. }
  65. }
  66. // We don't want any headers to be copied from the watermark to the image
  67. if err := wm.StripAll(); err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, offsetScale float64, framesCount int) error {
  73. wm := new(vips.Image)
  74. defer wm.Clear()
  75. width := img.Width()
  76. height := img.Height()
  77. frameHeight := height / framesCount
  78. if err := prepareWatermark(wm, wmData, opts, width, frameHeight, offsetScale, framesCount); err != nil {
  79. return err
  80. }
  81. if !img.ColourProfileImported() {
  82. if err := img.ImportColourProfile(); err != nil {
  83. return err
  84. }
  85. }
  86. if err := img.RgbColourspace(); err != nil {
  87. return err
  88. }
  89. opacity := opts.Opacity * config.WatermarkOpacity
  90. // If we replicated the watermark and need to apply it to an animated image,
  91. // it is faster to replicate the watermark to all the image and apply it single-pass
  92. if opts.ShouldReplicate() && framesCount > 1 {
  93. if err := wm.Replicate(width, height); err != nil {
  94. return err
  95. }
  96. return img.ApplyWatermark(wm, 0, 0, opacity)
  97. }
  98. left, top := 0, 0
  99. wmWidth := wm.Width()
  100. wmHeight := wm.Height()
  101. if !opts.ShouldReplicate() {
  102. left, top = calcPosition(width, frameHeight, wmWidth, wmHeight, &opts.Gravity, offsetScale, true)
  103. }
  104. if left >= width || top >= height || -left >= wmWidth || -top >= wmHeight {
  105. // Watermark is completely outside the image
  106. return nil
  107. }
  108. // if watermark is partially outside the image, it may partially be visible
  109. // on the next frame. We need to crop it vertically.
  110. // We don't care about horizontal overlap, as frames are stacked vertically
  111. if framesCount > 1 {
  112. cropTop := 0
  113. cropHeight := wmHeight
  114. if top < 0 {
  115. cropTop = -top
  116. cropHeight -= cropTop
  117. top = 0
  118. }
  119. if top+cropHeight > frameHeight {
  120. cropHeight = frameHeight - top
  121. }
  122. if cropTop > 0 || cropHeight < wmHeight {
  123. if err := wm.Crop(0, cropTop, wmWidth, cropHeight); err != nil {
  124. return err
  125. }
  126. }
  127. }
  128. for i := 0; i < framesCount; i++ {
  129. if err := img.ApplyWatermark(wm, left, top, opacity); err != nil {
  130. return err
  131. }
  132. top += frameHeight
  133. }
  134. return nil
  135. }
  136. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  137. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  138. return nil
  139. }
  140. return applyWatermark(img, imagedata.Watermark, &po.Watermark, pctx.dprScale, 1)
  141. }