vector_guard_scale.go 807 B

12345678910111213141516171819202122232425262728
  1. package processing
  2. import (
  3. "math"
  4. "github.com/imgproxy/imgproxy/v3/imagedata"
  5. "github.com/imgproxy/imgproxy/v3/options"
  6. "github.com/imgproxy/imgproxy/v3/vips"
  7. )
  8. // vectorGuardScale checks if the image is a vector format and downscales it
  9. // to the maximum allowed resolution if necessary
  10. func vectorGuardScale(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata imagedata.ImageData) error {
  11. if imgdata == nil || !imgdata.Format().IsVector() {
  12. return nil
  13. }
  14. if resolution := img.Width() * img.Height(); resolution > po.SecurityOptions.MaxSrcResolution {
  15. scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
  16. pctx.vectorBaseScale = scale
  17. if err := img.Load(imgdata, 1, scale, 1); err != nil {
  18. return err
  19. }
  20. }
  21. return nil
  22. }