pipeline.go 2.8 KB

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