1
0

pipeline.go 2.6 KB

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