extend.go 1.1 KB

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