scale_on_load.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package processing
  2. import (
  3. "log/slog"
  4. "math"
  5. "github.com/imgproxy/imgproxy/v3/imagedata"
  6. "github.com/imgproxy/imgproxy/v3/imagetype"
  7. "github.com/imgproxy/imgproxy/v3/imath"
  8. "github.com/imgproxy/imgproxy/v3/vips"
  9. )
  10. func (p *Processor) canScaleOnLoad(c *Context, imgdata imagedata.ImageData, scale float64) bool {
  11. if imgdata == nil || scale == 1 {
  12. return false
  13. }
  14. if imgdata.Format().IsVector() {
  15. return true
  16. }
  17. if p.config.DisableShrinkOnLoad || scale >= 1 {
  18. return false
  19. }
  20. return imgdata.Format() == imagetype.JPEG ||
  21. imgdata.Format() == imagetype.WEBP ||
  22. imgdata.Format() == imagetype.HEIC ||
  23. imgdata.Format() == imagetype.AVIF
  24. }
  25. func calcJpegShink(shrink float64) int {
  26. switch {
  27. case shrink >= 8:
  28. return 8
  29. case shrink >= 4:
  30. return 4
  31. case shrink >= 2:
  32. return 2
  33. }
  34. return 1
  35. }
  36. func (p *Processor) scaleOnLoad(c *Context) error {
  37. wshrink := float64(c.SrcWidth) / float64(imath.Scale(c.SrcWidth, c.WScale))
  38. hshrink := float64(c.SrcHeight) / float64(imath.Scale(c.SrcHeight, c.HScale))
  39. preshrink := math.Min(wshrink, hshrink)
  40. prescale := 1.0 / preshrink
  41. if c.ImgData != nil && c.ImgData.Format().IsVector() {
  42. // For vector images, apply the vector base scale
  43. prescale *= c.VectorBaseScale
  44. }
  45. if !p.canScaleOnLoad(c, c.ImgData, prescale) {
  46. return nil
  47. }
  48. var newWidth, newHeight int
  49. rotateAngle := c.PO.Rotate()
  50. autoRotate := c.PO.AutoRotate()
  51. if c.ImgData.Format().SupportsThumbnail() {
  52. thumbnail := new(vips.Image)
  53. defer thumbnail.Clear()
  54. if err := thumbnail.LoadThumbnail(c.ImgData); err != nil {
  55. slog.Debug("Can't load thumbnail: %s", "error", err)
  56. return nil
  57. }
  58. angle, flip := 0, false
  59. newWidth, newHeight, angle, flip = ExtractGeometry(thumbnail, rotateAngle, autoRotate)
  60. if newWidth >= c.SrcWidth || float64(newWidth)/float64(c.SrcWidth) < prescale {
  61. return nil
  62. }
  63. c.Img.Swap(thumbnail)
  64. c.Angle = angle
  65. c.Flip = flip
  66. } else {
  67. jpegShrink := calcJpegShink(preshrink)
  68. if c.ImgData.Format() == imagetype.JPEG && jpegShrink == 1 {
  69. return nil
  70. }
  71. if err := c.Img.Load(c.ImgData, jpegShrink, prescale, 1); err != nil {
  72. return err
  73. }
  74. newWidth, newHeight, _, _ = ExtractGeometry(c.Img, rotateAngle, autoRotate)
  75. }
  76. // Update scales after scale-on-load
  77. wpreshrink := float64(c.SrcWidth) / float64(newWidth)
  78. hpreshrink := float64(c.SrcHeight) / float64(newHeight)
  79. c.WScale = wpreshrink * c.WScale
  80. if newWidth == imath.Scale(newWidth, c.WScale) {
  81. c.WScale = 1.0
  82. }
  83. c.HScale = hpreshrink * c.HScale
  84. if newHeight == imath.Scale(newHeight, c.HScale) {
  85. c.HScale = 1.0
  86. }
  87. // We should crop before scaling, but we scaled the image on load,
  88. // so we need to adjust crop options
  89. if c.CropWidth > 0 {
  90. c.CropWidth = max(1, imath.Shrink(c.CropWidth, wpreshrink))
  91. }
  92. if c.CropHeight > 0 {
  93. c.CropHeight = max(1, imath.Shrink(c.CropHeight, hpreshrink))
  94. }
  95. if c.CropGravity.Type != GravityFocusPoint {
  96. // Adjust only when crop gravity offsets are absolute
  97. if math.Abs(c.CropGravity.X) >= 1.0 {
  98. // Round offsets to prevent turning absolute offsets to relative (ex: 1.0 => 0.5)
  99. c.CropGravity.X = math.RoundToEven(c.CropGravity.X / wpreshrink)
  100. }
  101. if math.Abs(c.CropGravity.Y) >= 1.0 {
  102. c.CropGravity.Y = math.RoundToEven(c.CropGravity.Y / hpreshrink)
  103. }
  104. }
  105. return nil
  106. }