1
0

watermark.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package processing
  2. import (
  3. "context"
  4. "github.com/imgproxy/imgproxy/v3/config"
  5. "github.com/imgproxy/imgproxy/v3/imagedata"
  6. "github.com/imgproxy/imgproxy/v3/imath"
  7. "github.com/imgproxy/imgproxy/v3/options"
  8. "github.com/imgproxy/imgproxy/v3/vips"
  9. )
  10. var watermarkPipeline = pipeline{
  11. prepare,
  12. scaleOnLoad,
  13. importColorProfile,
  14. scale,
  15. rotateAndFlip,
  16. padding,
  17. }
  18. func prepareWatermark(wm *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, imgWidth, imgHeight, framesCount int) error {
  19. if err := wm.Load(wmData, 1, 1.0, 1); err != nil {
  20. return err
  21. }
  22. po := options.NewProcessingOptions()
  23. po.ResizingType = options.ResizeFit
  24. po.Dpr = 1
  25. po.Enlarge = true
  26. po.Format = wmData.Type
  27. if opts.Scale > 0 {
  28. po.Width = imath.Max(imath.Scale(imgWidth, opts.Scale), 1)
  29. po.Height = imath.Max(imath.Scale(imgHeight, opts.Scale), 1)
  30. }
  31. if opts.Replicate {
  32. po.Padding.Enabled = true
  33. po.Padding.Left = int(opts.Gravity.X / 2)
  34. po.Padding.Right = int(opts.Gravity.X) - po.Padding.Left
  35. po.Padding.Top = int(opts.Gravity.Y / 2)
  36. po.Padding.Bottom = int(opts.Gravity.Y) - po.Padding.Top
  37. }
  38. if err := watermarkPipeline.Run(context.Background(), wm, po, wmData); err != nil {
  39. return err
  40. }
  41. if opts.Replicate || framesCount > 1 {
  42. // We need to copy image if we're going to replicate.
  43. // Replication requires image to be read several times, and this requires
  44. // random access to pixels
  45. if err := wm.CopyMemory(); err != nil {
  46. return err
  47. }
  48. }
  49. if opts.Replicate {
  50. if err := wm.Replicate(imgWidth, imgHeight); err != nil {
  51. return err
  52. }
  53. } else {
  54. left, top := calcPosition(imgWidth, imgHeight, wm.Width(), wm.Height(), &opts.Gravity, true)
  55. if err := wm.Embed(imgWidth, imgHeight, left, top); err != nil {
  56. return err
  57. }
  58. }
  59. if framesCount > 1 {
  60. if err := wm.Replicate(imgWidth, imgWidth*framesCount); err != nil {
  61. return err
  62. }
  63. }
  64. return nil
  65. }
  66. func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.WatermarkOptions, framesCount int) error {
  67. if err := img.RgbColourspace(); err != nil {
  68. return err
  69. }
  70. wm := new(vips.Image)
  71. defer wm.Clear()
  72. width := img.Width()
  73. height := img.Height()
  74. if err := prepareWatermark(wm, wmData, opts, width, height/framesCount, framesCount); err != nil {
  75. return err
  76. }
  77. opacity := opts.Opacity * config.WatermarkOpacity
  78. return img.ApplyWatermark(wm, opacity)
  79. }
  80. func watermark(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  81. if !po.Watermark.Enabled || imagedata.Watermark == nil {
  82. return nil
  83. }
  84. return applyWatermark(img, imagedata.Watermark, &po.Watermark, 1)
  85. }