pipeline.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/router"
  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 width we aim to get.
  25. // Based on the requested width scaled according to processing options.
  26. // Can be 0 if width is not specified in the processing options.
  27. targetWidth int
  28. // The height we aim to get.
  29. // Based on the requested height scaled according to processing options.
  30. // Can be 0 if height is not specified in the processing options.
  31. targetHeight int
  32. // The width of the image after cropping, scaling and rotating
  33. scaledWidth int
  34. // The height of the image after cropping, scaling and rotating
  35. scaledHeight int
  36. // The width of the result crop according to the resizing type
  37. resultCropWidth int
  38. // The height of the result crop according to the resizing type
  39. resultCropHeight int
  40. // The width of the image extended to the requested aspect ratio.
  41. // Can be 0 if any of the dimensions is not specified in the processing options
  42. // or if the image already has the requested aspect ratio.
  43. extendAspectRatioWidth int
  44. // The width of the image extended to the requested aspect ratio.
  45. // Can be 0 if any of the dimensions is not specified in the processing options
  46. // or if the image already has the requested aspect ratio.
  47. extendAspectRatioHeight int
  48. }
  49. type pipelineStep func(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error
  50. type pipeline []pipelineStep
  51. func (p pipeline) Run(ctx context.Context, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  52. pctx := pipelineContext{
  53. ctx: ctx,
  54. wscale: 1.0,
  55. hscale: 1.0,
  56. cropGravity: po.Crop.Gravity,
  57. }
  58. if pctx.cropGravity.Type == options.GravityUnknown {
  59. pctx.cropGravity = po.Gravity
  60. }
  61. for _, step := range p {
  62. if err := step(&pctx, img, po, imgdata); err != nil {
  63. return err
  64. }
  65. if err := router.CheckTimeout(ctx); err != nil {
  66. return err
  67. }
  68. }
  69. img.SetDouble("imgproxy-dpr-scale", pctx.dprScale)
  70. return nil
  71. }