prepare.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package processing
  2. import (
  3. "math"
  4. "github.com/imgproxy/imgproxy/v3/imagedata"
  5. "github.com/imgproxy/imgproxy/v3/imagetype"
  6. "github.com/imgproxy/imgproxy/v3/imath"
  7. "github.com/imgproxy/imgproxy/v3/options"
  8. "github.com/imgproxy/imgproxy/v3/vips"
  9. )
  10. func extractMeta(img *vips.Image, baseAngle int, useOrientation bool) (int, int, int, bool) {
  11. width := img.Width()
  12. height := img.Height()
  13. angle := 0
  14. flip := false
  15. if useOrientation {
  16. orientation := img.Orientation()
  17. if orientation == 3 || orientation == 4 {
  18. angle = 180
  19. }
  20. if orientation == 5 || orientation == 6 {
  21. angle = 90
  22. }
  23. if orientation == 7 || orientation == 8 {
  24. angle = 270
  25. }
  26. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  27. flip = true
  28. }
  29. }
  30. if (angle+baseAngle)%180 != 0 {
  31. width, height = height, width
  32. }
  33. return width, height, angle, flip
  34. }
  35. func calcScale(width, height int, po *options.ProcessingOptions, imgtype imagetype.Type) (float64, float64, float64) {
  36. var wshrink, hshrink float64
  37. srcW, srcH := float64(width), float64(height)
  38. dstW, dstH := float64(po.Width), float64(po.Height)
  39. if po.Width == 0 {
  40. dstW = srcW
  41. }
  42. if dstW == srcW {
  43. wshrink = 1
  44. } else {
  45. wshrink = srcW / dstW
  46. }
  47. if po.Height == 0 {
  48. dstH = srcH
  49. }
  50. if dstH == srcH {
  51. hshrink = 1
  52. } else {
  53. hshrink = srcH / dstH
  54. }
  55. if wshrink != 1 || hshrink != 1 {
  56. rt := po.ResizingType
  57. if rt == options.ResizeAuto {
  58. srcD := srcW - srcH
  59. dstD := dstW - dstH
  60. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  61. rt = options.ResizeFill
  62. } else {
  63. rt = options.ResizeFit
  64. }
  65. }
  66. switch {
  67. case po.Width == 0 && rt != options.ResizeForce:
  68. wshrink = hshrink
  69. case po.Height == 0 && rt != options.ResizeForce:
  70. hshrink = wshrink
  71. case rt == options.ResizeFit:
  72. wshrink = math.Max(wshrink, hshrink)
  73. hshrink = wshrink
  74. case rt == options.ResizeFill || rt == options.ResizeFillDown:
  75. wshrink = math.Min(wshrink, hshrink)
  76. hshrink = wshrink
  77. }
  78. }
  79. wshrink /= po.ZoomWidth
  80. hshrink /= po.ZoomHeight
  81. dprScale := po.Dpr
  82. if !po.Enlarge && imgtype != imagetype.SVG {
  83. minShrink := math.Min(wshrink, hshrink)
  84. if minShrink < 1 {
  85. wshrink /= minShrink
  86. hshrink /= minShrink
  87. if !po.Extend.Enabled {
  88. dprScale /= minShrink
  89. }
  90. }
  91. dprScale = math.Min(dprScale, math.Min(wshrink, hshrink))
  92. }
  93. if po.MinWidth > 0 {
  94. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  95. hshrink /= wshrink / minShrink
  96. wshrink = minShrink
  97. }
  98. }
  99. if po.MinHeight > 0 {
  100. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  101. wshrink /= hshrink / minShrink
  102. hshrink = minShrink
  103. }
  104. }
  105. wshrink /= dprScale
  106. hshrink /= dprScale
  107. if wshrink > srcW {
  108. wshrink = srcW
  109. }
  110. if hshrink > srcH {
  111. hshrink = srcH
  112. }
  113. return 1.0 / wshrink, 1.0 / hshrink, dprScale
  114. }
  115. func calcCropSize(orig int, crop float64) int {
  116. switch {
  117. case crop == 0.0:
  118. return 0
  119. case crop >= 1.0:
  120. return int(crop)
  121. default:
  122. return imath.Max(1, imath.Scale(orig, crop))
  123. }
  124. }
  125. func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  126. pctx.imgtype = imagetype.Unknown
  127. if imgdata != nil {
  128. pctx.imgtype = imgdata.Type
  129. }
  130. pctx.srcWidth, pctx.srcHeight, pctx.angle, pctx.flip = extractMeta(img, po.Rotate, po.AutoRotate)
  131. pctx.cropWidth = calcCropSize(pctx.srcWidth, po.Crop.Width)
  132. pctx.cropHeight = calcCropSize(pctx.srcHeight, po.Crop.Height)
  133. widthToScale := imath.MinNonZero(pctx.cropWidth, pctx.srcWidth)
  134. heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
  135. pctx.wscale, pctx.hscale, pctx.dprScale = calcScale(widthToScale, heightToScale, po, pctx.imgtype)
  136. // The size of a vector image is not checked during download, yet it can be very large.
  137. // So we should scale it down to the maximum allowed resolution
  138. if !pctx.trimmed && imgdata != nil && imgdata.Type.IsVector() && !po.Enlarge {
  139. resolution := imath.Round((float64(img.Width()*img.Height()) * pctx.wscale * pctx.hscale))
  140. if resolution > po.SecurityOptions.MaxSrcResolution {
  141. scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
  142. pctx.wscale *= scale
  143. pctx.hscale *= scale
  144. }
  145. }
  146. return nil
  147. }