watermark.go 4.3 KB

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