watermark.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. stripMetadata,
  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.Type
  29. po.StripMetadata = true
  30. po.KeepCopyright = false
  31. if opts.Scale > 0 {
  32. po.Width = imath.Max(imath.ScaleToEven(imgWidth, opts.Scale), 1)
  33. po.Height = imath.Max(imath.ScaleToEven(imgHeight, opts.Scale), 1)
  34. }
  35. if opts.Replicate {
  36. var offX, offY int
  37. if math.Abs(opts.Gravity.X) >= 1.0 {
  38. offX = imath.RoundToEven(opts.Gravity.X * offsetScale)
  39. } else {
  40. offX = imath.ScaleToEven(imgWidth, opts.Gravity.X)
  41. }
  42. if math.Abs(opts.Gravity.Y) >= 1.0 {
  43. offY = imath.RoundToEven(opts.Gravity.Y * offsetScale)
  44. } else {
  45. offY = imath.ScaleToEven(imgHeight, opts.Gravity.Y)
  46. }
  47. po.Padding.Enabled = true
  48. po.Padding.Left = offX / 2
  49. po.Padding.Right = offX - po.Padding.Left
  50. po.Padding.Top = offY / 2
  51. po.Padding.Bottom = offY - po.Padding.Top
  52. }
  53. if err := watermarkPipeline.Run(context.Background(), wm, po, wmData); err != nil {
  54. return err
  55. }
  56. if opts.Replicate || framesCount > 1 {
  57. // We need to copy image if we're going to replicate.
  58. // Replication requires image to be read several times, and this requires
  59. // random access to pixels
  60. if err := wm.CopyMemory(); err != nil {
  61. return err
  62. }
  63. }
  64. if opts.Replicate {
  65. if err := wm.Replicate(imgWidth, imgHeight); err != nil {
  66. return err
  67. }
  68. }
  69. wm.RemovePaletteBitDepth()
  70. return nil
  71. }
  72. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, offsetScale float64, framesCount int) error {
  73. if err := img.RgbColourspace(); err != nil {
  74. return err
  75. }
  76. wm := new(vips.Image)
  77. defer wm.Clear()
  78. width := img.Width()
  79. height := img.Height()
  80. frameHeight := height / framesCount
  81. if err := prepareWatermark(wm, wmData, opts, width, frameHeight, offsetScale, framesCount); err != nil {
  82. return err
  83. }
  84. opacity := opts.Opacity * config.WatermarkOpacity
  85. // If we replicated the watermark and need to apply it to an animated image,
  86. // it is faster to replicate the watermark to all the image and apply it single-pass
  87. if opts.Replicate && framesCount > 1 {
  88. if err := wm.Replicate(width, height); err != nil {
  89. return err
  90. }
  91. return img.ApplyWatermark(wm, 0, 0, opacity)
  92. }
  93. left, top := 0, 0
  94. if !opts.Replicate {
  95. left, top = calcPosition(width, frameHeight, wm.Width(), wm.Height(), &opts.Gravity, offsetScale, true)
  96. }
  97. for i := 0; i < framesCount; i++ {
  98. if err := img.ApplyWatermark(wm, left, top, opacity); err != nil {
  99. return err
  100. }
  101. top += frameHeight
  102. }
  103. return nil
  104. }
  105. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  106. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  107. return nil
  108. }
  109. return applyWatermark(img, imagedata.Watermark, &po.Watermark, pctx.dprScale, 1)
  110. }