watermark.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Replicate {
  33. offX := int(math.RoundToEven(opts.Gravity.X * offsetScale))
  34. offY := int(math.RoundToEven(opts.Gravity.Y * offsetScale))
  35. po.Padding.Enabled = true
  36. po.Padding.Left = offX / 2
  37. po.Padding.Right = offX - po.Padding.Left
  38. po.Padding.Top = offY / 2
  39. po.Padding.Bottom = offY - po.Padding.Top
  40. }
  41. if err := watermarkPipeline.Run(context.Background(), wm, po, wmData); err != nil {
  42. return err
  43. }
  44. if opts.Replicate || framesCount > 1 {
  45. // We need to copy image if we're going to replicate.
  46. // Replication requires image to be read several times, and this requires
  47. // random access to pixels
  48. if err := wm.CopyMemory(); err != nil {
  49. return err
  50. }
  51. }
  52. if opts.Replicate {
  53. if err := wm.Replicate(imgWidth, imgHeight); err != nil {
  54. return err
  55. }
  56. } else {
  57. left, top := calcPosition(imgWidth, imgHeight, wm.Width(), wm.Height(), &opts.Gravity, offsetScale, true)
  58. if err := wm.Embed(imgWidth, imgHeight, left, top); err != nil {
  59. return err
  60. }
  61. }
  62. if framesCount > 1 {
  63. if err := wm.Replicate(imgWidth, imgWidth*framesCount); err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. }
  69. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, offsetScale float64, framesCount int) error {
  70. if err := img.RgbColourspace(); err != nil {
  71. return err
  72. }
  73. wm := new(vips.Image)
  74. defer wm.Clear()
  75. width := img.Width()
  76. height := img.Height()
  77. if err := prepareWatermark(wm, wmData, opts, width, height/framesCount, offsetScale, framesCount); err != nil {
  78. return err
  79. }
  80. opacity := opts.Opacity * config.WatermarkOpacity
  81. return img.ApplyWatermark(wm, opacity)
  82. }
  83. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  84. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  85. return nil
  86. }
  87. return applyWatermark(img, imagedata.Watermark, &po.Watermark, pctx.dprScale, 1)
  88. }