pipeline.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package processing
  2. import (
  3. "context"
  4. "github.com/imgproxy/imgproxy/v3/auximageprovider"
  5. "github.com/imgproxy/imgproxy/v3/imagedata"
  6. "github.com/imgproxy/imgproxy/v3/imagetype"
  7. "github.com/imgproxy/imgproxy/v3/options"
  8. "github.com/imgproxy/imgproxy/v3/server"
  9. "github.com/imgproxy/imgproxy/v3/vips"
  10. )
  11. type pipelineContext struct {
  12. ctx context.Context
  13. config *Config
  14. imgtype imagetype.Type
  15. // The watermark image provider, if any watermarking is to be done.
  16. watermarkProvider auximageprovider.Provider
  17. trimmed bool
  18. srcWidth int
  19. srcHeight int
  20. angle int
  21. flip bool
  22. cropWidth int
  23. cropHeight int
  24. cropGravity options.GravityOptions
  25. wscale float64
  26. hscale float64
  27. dprScale float64
  28. // The base scale factor for vector images.
  29. // It is used to downscale the input vector image to the maximum allowed resolution
  30. vectorBaseScale float64
  31. // The width we aim to get.
  32. // Based on the requested width scaled according to processing options.
  33. // Can be 0 if width is not specified in the processing options.
  34. targetWidth int
  35. // The height we aim to get.
  36. // Based on the requested height scaled according to processing options.
  37. // Can be 0 if height is not specified in the processing options.
  38. targetHeight int
  39. // The width of the image after cropping, scaling and rotating
  40. scaledWidth int
  41. // The height of the image after cropping, scaling and rotating
  42. scaledHeight int
  43. // The width of the result crop according to the resizing type
  44. resultCropWidth int
  45. // The height of the result crop according to the resizing type
  46. resultCropHeight int
  47. // The width of the image extended to the requested aspect ratio.
  48. // Can be 0 if any of the dimensions is not specified in the processing options
  49. // or if the image already has the requested aspect ratio.
  50. extendAspectRatioWidth int
  51. // The width of the image extended to the requested aspect ratio.
  52. // Can be 0 if any of the dimensions is not specified in the processing options
  53. // or if the image already has the requested aspect ratio.
  54. extendAspectRatioHeight int
  55. }
  56. type pipelineStep func(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata imagedata.ImageData) error
  57. type pipeline []pipelineStep
  58. func (p pipeline) Run(
  59. ctx context.Context,
  60. img *vips.Image,
  61. po *options.ProcessingOptions,
  62. imgdata imagedata.ImageData,
  63. watermark auximageprovider.Provider,
  64. config *Config,
  65. ) error {
  66. pctx := pipelineContext{
  67. ctx: ctx,
  68. config: config,
  69. wscale: 1.0,
  70. hscale: 1.0,
  71. dprScale: 1.0,
  72. vectorBaseScale: 1.0,
  73. cropGravity: po.Crop.Gravity,
  74. watermarkProvider: watermark,
  75. }
  76. if pctx.cropGravity.Type == options.GravityUnknown {
  77. pctx.cropGravity = po.Gravity
  78. }
  79. for _, step := range p {
  80. if err := step(&pctx, img, po, imgdata); err != nil {
  81. return err
  82. }
  83. if err := server.CheckTimeout(ctx); err != nil {
  84. return err
  85. }
  86. }
  87. img.SetDouble("imgproxy-dpr-scale", pctx.dprScale)
  88. return nil
  89. }