pipeline.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. iccImported bool
  25. }
  26. type pipelineStep func(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error
  27. type pipeline []pipelineStep
  28. func (p pipeline) Run(ctx context.Context, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  29. pctx := pipelineContext{
  30. ctx: ctx,
  31. wscale: 1.0,
  32. hscale: 1.0,
  33. cropGravity: po.Crop.Gravity,
  34. }
  35. if pctx.cropGravity.Type == options.GravityUnknown {
  36. pctx.cropGravity = po.Gravity
  37. }
  38. for _, step := range p {
  39. if err := step(&pctx, img, po, imgdata); err != nil {
  40. return err
  41. }
  42. if err := router.CheckTimeout(ctx); err != nil {
  43. return err
  44. }
  45. }
  46. img.SetDouble("imgproxy-dpr-scale", pctx.dprScale)
  47. return nil
  48. }