watermark.go 3.2 KB

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