scale_on_load.go 3.3 KB

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