1
0

pipeline.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package processing
  2. import (
  3. "context"
  4. "github.com/imgproxy/imgproxy/v3/imagedata"
  5. "github.com/imgproxy/imgproxy/v3/security"
  6. "github.com/imgproxy/imgproxy/v3/server"
  7. "github.com/imgproxy/imgproxy/v3/vips"
  8. )
  9. type Context struct {
  10. // The context to check for timeouts and cancellations
  11. Ctx context.Context
  12. // Current image being processed
  13. Img *vips.Image
  14. // Processing options this pipeline runs with
  15. PO ProcessingOptions
  16. // Security options this pipeline runs with
  17. SecOps security.Options
  18. // Original image data
  19. ImgData imagedata.ImageData
  20. SrcWidth int
  21. SrcHeight int
  22. Angle int
  23. Flip bool
  24. CropWidth int
  25. CropHeight int
  26. CropGravity GravityOptions
  27. WScale float64
  28. HScale float64
  29. DprScale float64
  30. // The base shrink factor for vector images.
  31. // It is used to downscale the input vector image to the maximum allowed resolution
  32. VectorBaseShrink float64
  33. // The width we aim to get.
  34. // Based on the requested width scaled according to processing options.
  35. // Can be 0 if width is not specified in the processing options.
  36. TargetWidth int
  37. // The height we aim to get.
  38. // Based on the requested height scaled according to processing options.
  39. // Can be 0 if height is not specified in the processing options.
  40. TargetHeight int
  41. // The width of the image after cropping, scaling and rotating
  42. ScaledWidth int
  43. // The height of the image after cropping, scaling and rotating
  44. ScaledHeight int
  45. // The width of the result crop according to the resizing type
  46. ResultCropWidth int
  47. // The height of the result crop according to the resizing type
  48. ResultCropHeight int
  49. // The width of the image extended to the requested aspect ratio.
  50. // Can be 0 if any of the dimensions is not specified in the processing options
  51. // or if the image already has the requested aspect ratio.
  52. ExtendAspectRatioWidth int
  53. // The width of the image extended to the requested aspect ratio.
  54. // Can be 0 if any of the dimensions is not specified in the processing options
  55. // or if the image already has the requested aspect ratio.
  56. ExtendAspectRatioHeight int
  57. }
  58. type Step func(c *Context) error
  59. type Pipeline []Step
  60. // Run runs the given pipeline with the given parameters
  61. func (p Pipeline) Run(
  62. ctx context.Context,
  63. img *vips.Image,
  64. po ProcessingOptions,
  65. secops security.Options,
  66. imgdata imagedata.ImageData,
  67. ) error {
  68. pctx := p.newContext(ctx, img, po, secops, imgdata)
  69. pctx.CalcParams()
  70. for _, step := range p {
  71. if err := step(&pctx); err != nil {
  72. return err
  73. }
  74. if err := server.CheckTimeout(ctx); err != nil {
  75. return err
  76. }
  77. }
  78. img.SetDouble("imgproxy-dpr-scale", pctx.DprScale)
  79. return nil
  80. }
  81. func (p Pipeline) newContext(
  82. ctx context.Context,
  83. img *vips.Image,
  84. po ProcessingOptions,
  85. secops security.Options,
  86. imgdata imagedata.ImageData,
  87. ) Context {
  88. pctx := Context{
  89. Ctx: ctx,
  90. Img: img,
  91. PO: po,
  92. SecOps: secops,
  93. ImgData: imgdata,
  94. WScale: 1.0,
  95. HScale: 1.0,
  96. DprScale: 1.0,
  97. VectorBaseShrink: 1.0,
  98. CropGravity: po.CropGravity(),
  99. }
  100. // If crop gravity is not set, use the general gravity option
  101. if pctx.CropGravity.Type == GravityUnknown {
  102. pctx.CropGravity = po.Gravity()
  103. }
  104. return pctx
  105. }