watermark.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. } else {
  60. left, top := calcPosition(imgWidth, imgHeight, wm.Width(), wm.Height(), &opts.Gravity, offsetScale, true)
  61. if err := wm.Embed(imgWidth, imgHeight, left, top); err != nil {
  62. return err
  63. }
  64. }
  65. if framesCount > 1 {
  66. if err := wm.Replicate(imgWidth, imgWidth*framesCount); err != nil {
  67. return err
  68. }
  69. }
  70. wm.RemoveHeader("palette-bit-depth")
  71. return nil
  72. }
  73. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, offsetScale float64, framesCount int) error {
  74. if err := img.RgbColourspace(); err != nil {
  75. return err
  76. }
  77. wm := new(vips.Image)
  78. defer wm.Clear()
  79. width := img.Width()
  80. height := img.Height()
  81. if err := prepareWatermark(wm, wmData, opts, width, height/framesCount, offsetScale, framesCount); err != nil {
  82. return err
  83. }
  84. opacity := opts.Opacity * config.WatermarkOpacity
  85. return img.ApplyWatermark(wm, opacity)
  86. }
  87. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  88. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  89. return nil
  90. }
  91. return applyWatermark(img, imagedata.Watermark, &po.Watermark, pctx.dprScale, 1)
  92. }