pipeline.go 1.2 KB

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