scale_on_load.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 == imagetype.SVG {
  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(scale float64, imgtype imagetype.Type) int {
  28. shrink := int(1.0 / scale)
  29. switch {
  30. case shrink >= 8:
  31. return 8
  32. case shrink >= 4:
  33. return 4
  34. case shrink >= 2:
  35. return 2
  36. }
  37. return 1
  38. }
  39. func scaleOnLoad(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  40. prescale := math.Max(pctx.wscale, pctx.hscale)
  41. if !canScaleOnLoad(pctx, imgdata, prescale) {
  42. return nil
  43. }
  44. var newWidth, newHeight int
  45. if imgdata.Type.SupportsThumbnail() {
  46. thumbnail := new(vips.Image)
  47. defer thumbnail.Clear()
  48. if err := thumbnail.LoadThumbnail(imgdata); err != nil {
  49. log.Debugf("Can't load thumbnail: %s", err)
  50. return nil
  51. }
  52. angle, flip := 0, false
  53. newWidth, newHeight, angle, flip = extractMeta(thumbnail, po.Rotate, po.AutoRotate)
  54. if newWidth >= pctx.srcWidth || float64(newWidth)/float64(pctx.srcWidth) < prescale {
  55. return nil
  56. }
  57. img.Swap(thumbnail)
  58. pctx.angle = angle
  59. pctx.flip = flip
  60. } else {
  61. jpegShrink := calcJpegShink(prescale, pctx.imgtype)
  62. if pctx.imgtype == imagetype.JPEG && jpegShrink == 1 {
  63. return nil
  64. }
  65. if err := img.Load(imgdata, jpegShrink, prescale, 1); err != nil {
  66. return err
  67. }
  68. newWidth, newHeight, _, _ = extractMeta(img, po.Rotate, po.AutoRotate)
  69. }
  70. // Update scales after scale-on-load
  71. wpreshrink := float64(pctx.srcWidth) / float64(newWidth)
  72. hpreshrink := float64(pctx.srcHeight) / float64(newHeight)
  73. pctx.wscale = wpreshrink * pctx.wscale
  74. if newWidth == imath.Scale(newWidth, pctx.wscale) {
  75. pctx.wscale = 1.0
  76. }
  77. pctx.hscale = hpreshrink * pctx.hscale
  78. if newHeight == imath.Scale(newHeight, pctx.hscale) {
  79. pctx.hscale = 1.0
  80. }
  81. if pctx.cropWidth > 0 {
  82. pctx.cropWidth = imath.Max(1, imath.Shrink(pctx.cropWidth, wpreshrink))
  83. }
  84. if pctx.cropHeight > 0 {
  85. pctx.cropHeight = imath.Max(1, imath.Shrink(pctx.cropHeight, hpreshrink))
  86. }
  87. if pctx.cropGravity.Type != options.GravityFocusPoint {
  88. pctx.cropGravity.X /= wpreshrink
  89. pctx.cropGravity.Y /= hpreshrink
  90. }
  91. return nil
  92. }