extend.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package processing
  2. import (
  3. "github.com/imgproxy/imgproxy/v3/imagedata"
  4. "github.com/imgproxy/imgproxy/v3/options"
  5. "github.com/imgproxy/imgproxy/v3/vips"
  6. )
  7. func extendImage(img *vips.Image, width, height int, gravity *options.GravityOptions, offsetScale float64) error {
  8. imgWidth := img.Width()
  9. imgHeight := img.Height()
  10. if width <= imgWidth && height <= imgHeight {
  11. return nil
  12. }
  13. if width <= 0 {
  14. width = imgWidth
  15. }
  16. if height <= 0 {
  17. height = imgHeight
  18. }
  19. offX, offY := calcPosition(width, height, imgWidth, imgHeight, gravity, offsetScale, false)
  20. return img.Embed(width, height, offX, offY)
  21. }
  22. func extend(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  23. if !po.Extend.Enabled {
  24. return nil
  25. }
  26. width, height := pctx.targetWidth, pctx.targetHeight
  27. return extendImage(img, width, height, &po.Extend.Gravity, pctx.dprScale)
  28. }
  29. func extendAspectRatio(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  30. if !po.ExtendAspectRatio.Enabled {
  31. return nil
  32. }
  33. width, height := pctx.extendAspectRatioWidth, pctx.extendAspectRatioHeight
  34. if width == 0 || height == 0 {
  35. return nil
  36. }
  37. return extendImage(img, width, height, &po.ExtendAspectRatio.Gravity, pctx.dprScale)
  38. }