extend.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/options"
  4. "github.com/imgproxy/imgproxy/v3/vips"
  5. )
  6. func extendImage(img *vips.Image, width, height int, gravity *options.GravityOptions, offsetScale float64) error {
  7. imgWidth := img.Width()
  8. imgHeight := img.Height()
  9. if width <= imgWidth && height <= imgHeight {
  10. return nil
  11. }
  12. if width <= 0 {
  13. width = imgWidth
  14. }
  15. if height <= 0 {
  16. height = imgHeight
  17. }
  18. offX, offY := calcPosition(width, height, imgWidth, imgHeight, gravity, offsetScale, false)
  19. return img.Embed(width, height, offX, offY)
  20. }
  21. func (p *Processor) extend(c *Context) error {
  22. if !c.PO.Extend.Enabled {
  23. return nil
  24. }
  25. width, height := c.TargetWidth, c.TargetHeight
  26. return extendImage(c.Img, width, height, &c.PO.Extend.Gravity, c.DprScale)
  27. }
  28. func (p *Processor) extendAspectRatio(c *Context) error {
  29. if !c.PO.ExtendAspectRatio.Enabled {
  30. return nil
  31. }
  32. width, height := c.ExtendAspectRatioWidth, c.ExtendAspectRatioHeight
  33. if width == 0 || height == 0 {
  34. return nil
  35. }
  36. return extendImage(c.Img, width, height, &c.PO.ExtendAspectRatio.Gravity, c.DprScale)
  37. }