scale_on_load.go 3.4 KB

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