watermark.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.Position.X) >= 1.0 {
  35. offX = imath.RoundToEven(opts.Position.X * offsetScale)
  36. } else {
  37. offX = imath.ScaleToEven(imgWidth, opts.Position.X)
  38. }
  39. if math.Abs(opts.Position.Y) >= 1.0 {
  40. offY = imath.RoundToEven(opts.Position.Y * offsetScale)
  41. } else {
  42. offY = imath.ScaleToEven(imgHeight, opts.Position.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, true); err != nil {
  63. return err
  64. }
  65. }
  66. // We don't want any headers to be copied from the watermark to the image
  67. return wm.StripAll()
  68. }
  69. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, offsetScale float64, framesCount int) error {
  70. wm := new(vips.Image)
  71. defer wm.Clear()
  72. width := img.Width()
  73. height := img.Height()
  74. frameHeight := height / framesCount
  75. if err := prepareWatermark(wm, wmData, opts, width, frameHeight, offsetScale, framesCount); err != nil {
  76. return err
  77. }
  78. if !img.ColourProfileImported() {
  79. if err := img.ImportColourProfile(); err != nil {
  80. return err
  81. }
  82. }
  83. if err := img.RgbColourspace(); err != nil {
  84. return err
  85. }
  86. opacity := opts.Opacity * config.WatermarkOpacity
  87. // If we replicated the watermark and need to apply it to an animated image,
  88. // it is faster to replicate the watermark to all the image and apply it single-pass
  89. if opts.ShouldReplicate() && framesCount > 1 {
  90. if err := wm.Replicate(width, height, false); err != nil {
  91. return err
  92. }
  93. return img.ApplyWatermark(wm, 0, 0, opacity)
  94. }
  95. left, top := 0, 0
  96. wmWidth := wm.Width()
  97. wmHeight := wm.Height()
  98. if !opts.ShouldReplicate() {
  99. left, top = calcPosition(width, frameHeight, wmWidth, wmHeight, &opts.Position, offsetScale, true)
  100. }
  101. if left >= width || top >= height || -left >= wmWidth || -top >= wmHeight {
  102. // Watermark is completely outside the image
  103. return nil
  104. }
  105. // if watermark is partially outside the image, it may partially be visible
  106. // on the next frame. We need to crop it vertically.
  107. // We don't care about horizontal overlap, as frames are stacked vertically
  108. if framesCount > 1 {
  109. cropTop := 0
  110. cropHeight := wmHeight
  111. if top < 0 {
  112. cropTop = -top
  113. cropHeight -= cropTop
  114. top = 0
  115. }
  116. if top+cropHeight > frameHeight {
  117. cropHeight = frameHeight - top
  118. }
  119. if cropTop > 0 || cropHeight < wmHeight {
  120. if err := wm.Crop(0, cropTop, wmWidth, cropHeight); err != nil {
  121. return err
  122. }
  123. }
  124. }
  125. for i := 0; i < framesCount; i++ {
  126. if err := img.ApplyWatermark(wm, left, top, opacity); err != nil {
  127. return err
  128. }
  129. top += frameHeight
  130. }
  131. return nil
  132. }
  133. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  134. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  135. return nil
  136. }
  137. return applyWatermark(img, imagedata.Watermark, &po.Watermark, pctx.dprScale, 1)
  138. }