prepare.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package processing
  2. import (
  3. "math"
  4. "github.com/imgproxy/imgproxy/v3/imath"
  5. "github.com/imgproxy/imgproxy/v3/options"
  6. "github.com/imgproxy/imgproxy/v3/vips"
  7. )
  8. // ExtractGeometry extracts image width, height, orientation angle and flip flag from the image metadata.
  9. func ExtractGeometry(img *vips.Image, baseAngle int, autoRotate bool) (int, int, int, bool) {
  10. width := img.Width()
  11. height := img.Height()
  12. angle, flip := angleFlip(img, autoRotate)
  13. if (angle+baseAngle)%180 != 0 {
  14. width, height = height, width
  15. }
  16. return width, height, angle, flip
  17. }
  18. // angleFlip returns the orientation angle and flip flag based on the image metadata
  19. // and po.AutoRotate flag.
  20. func angleFlip(img *vips.Image, autoRotate bool) (int, bool) {
  21. if !autoRotate {
  22. return 0, false
  23. }
  24. angle := 0
  25. flip := false
  26. orientation := img.Orientation()
  27. if orientation == 3 || orientation == 4 {
  28. angle = 180
  29. }
  30. if orientation == 5 || orientation == 6 {
  31. angle = 90
  32. }
  33. if orientation == 7 || orientation == 8 {
  34. angle = 270
  35. }
  36. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  37. flip = true
  38. }
  39. return angle, flip
  40. }
  41. // CalcCropSize calculates the crop size based on the original size and crop scale.
  42. func CalcCropSize(orig int, crop float64) int {
  43. switch {
  44. case crop == 0.0:
  45. return 0
  46. case crop >= 1.0:
  47. return int(crop)
  48. default:
  49. return max(1, imath.Scale(orig, crop))
  50. }
  51. }
  52. func (c *Context) calcScale(width, height int, po ProcessingOptions) {
  53. wshrink, hshrink := 1.0, 1.0
  54. srcW, srcH := float64(width), float64(height)
  55. poWidth := po.Width()
  56. poHeight := po.Height()
  57. dstW := imath.NonZero(float64(poWidth), srcW)
  58. dstH := imath.NonZero(float64(poHeight), srcH)
  59. if dstW != srcW {
  60. wshrink = srcW / dstW
  61. }
  62. if dstH != srcH {
  63. hshrink = srcH / dstH
  64. }
  65. if wshrink != 1 || hshrink != 1 {
  66. rt := po.ResizingType()
  67. if rt == options.ResizeAuto {
  68. srcD := srcW - srcH
  69. dstD := dstW - dstH
  70. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  71. rt = options.ResizeFill
  72. } else {
  73. rt = options.ResizeFit
  74. }
  75. }
  76. switch {
  77. case poWidth == 0 && rt != options.ResizeForce:
  78. wshrink = hshrink
  79. case poHeight == 0 && rt != options.ResizeForce:
  80. hshrink = wshrink
  81. case rt == options.ResizeFit:
  82. wshrink = math.Max(wshrink, hshrink)
  83. hshrink = wshrink
  84. case rt == options.ResizeFill || rt == options.ResizeFillDown:
  85. wshrink = math.Min(wshrink, hshrink)
  86. hshrink = wshrink
  87. }
  88. }
  89. wshrink /= po.ZoomWidth()
  90. hshrink /= po.ZoomHeight()
  91. c.DprScale = po.DPR()
  92. isVector := c.ImgData != nil && c.ImgData.Format().IsVector()
  93. if !po.Enlarge() && !isVector {
  94. minShrink := math.Min(wshrink, hshrink)
  95. if minShrink < 1 {
  96. wshrink /= minShrink
  97. hshrink /= minShrink
  98. // If we reached this point, this means that we can't reach the target size
  99. // because the image is smaller than it, and the enlargement is disabled.
  100. // If the DprScale is less than 1, the image will be downscaled, moving
  101. // even further from the target size, so we need to compensate it.
  102. // The compensation may increase the DprScale too much, but this is okay,
  103. // because we'll handle this further in the code.
  104. //
  105. // If the Extend option is enabled, we want to keep the resulting image
  106. // composition the same regardless of the DPR, so we don't apply this compensation
  107. // in this case.
  108. if !po.ExtendEnabled() {
  109. c.DprScale /= minShrink
  110. }
  111. }
  112. // The minimum of wshrink and hshrink is the maximum dprScale value
  113. // that can be used without enlarging the image.
  114. c.DprScale = math.Min(c.DprScale, math.Min(wshrink, hshrink))
  115. }
  116. if minWidth := po.MinWidth(); minWidth > 0 {
  117. if minShrink := srcW / float64(minWidth); minShrink < wshrink {
  118. hshrink /= wshrink / minShrink
  119. wshrink = minShrink
  120. }
  121. }
  122. if minHeight := po.MinHeight(); minHeight > 0 {
  123. if minShrink := srcH / float64(minHeight); minShrink < hshrink {
  124. wshrink /= hshrink / minShrink
  125. hshrink = minShrink
  126. }
  127. }
  128. wshrink /= c.DprScale
  129. hshrink /= c.DprScale
  130. if wshrink > srcW {
  131. wshrink = srcW
  132. }
  133. if hshrink > srcH {
  134. hshrink = srcH
  135. }
  136. c.WScale = 1.0 / wshrink
  137. c.HScale = 1.0 / hshrink
  138. }
  139. func (c *Context) calcSizes(widthToScale, heightToScale int, po ProcessingOptions) {
  140. c.TargetWidth = imath.Scale(po.Width(), c.DprScale*po.ZoomWidth())
  141. c.TargetHeight = imath.Scale(po.Height(), c.DprScale*po.ZoomHeight())
  142. c.ScaledWidth = imath.Scale(widthToScale, c.WScale)
  143. c.ScaledHeight = imath.Scale(heightToScale, c.HScale)
  144. if po.ResizingType() == options.ResizeFillDown && !po.Enlarge() {
  145. diffW := float64(c.TargetWidth) / float64(c.ScaledWidth)
  146. diffH := float64(c.TargetHeight) / float64(c.ScaledHeight)
  147. switch {
  148. case diffW > diffH && diffW > 1.0:
  149. c.ResultCropHeight = imath.Scale(c.ScaledWidth, float64(c.TargetHeight)/float64(c.TargetWidth))
  150. c.ResultCropWidth = c.ScaledWidth
  151. case diffH > diffW && diffH > 1.0:
  152. c.ResultCropWidth = imath.Scale(c.ScaledHeight, float64(c.TargetWidth)/float64(c.TargetHeight))
  153. c.ResultCropHeight = c.ScaledHeight
  154. default:
  155. c.ResultCropWidth = c.TargetWidth
  156. c.ResultCropHeight = c.TargetHeight
  157. }
  158. } else {
  159. c.ResultCropWidth = c.TargetWidth
  160. c.ResultCropHeight = c.TargetHeight
  161. }
  162. if po.ExtendAspectRatioEnabled() && c.TargetWidth > 0 && c.TargetHeight > 0 {
  163. outWidth := imath.MinNonZero(c.ScaledWidth, c.ResultCropWidth)
  164. outHeight := imath.MinNonZero(c.ScaledHeight, c.ResultCropHeight)
  165. diffW := float64(c.TargetWidth) / float64(outWidth)
  166. diffH := float64(c.TargetHeight) / float64(outHeight)
  167. switch {
  168. case diffH > diffW:
  169. c.ExtendAspectRatioHeight = imath.Scale(outWidth, float64(c.TargetHeight)/float64(c.TargetWidth))
  170. c.ExtendAspectRatioWidth = outWidth
  171. case diffW > diffH:
  172. c.ExtendAspectRatioWidth = imath.Scale(outHeight, float64(c.TargetWidth)/float64(c.TargetHeight))
  173. c.ExtendAspectRatioHeight = outHeight
  174. }
  175. }
  176. }
  177. func (c *Context) limitScale(widthToScale, heightToScale int, po ProcessingOptions) {
  178. maxresultDim := c.SecOps.MaxResultDimension
  179. if maxresultDim <= 0 {
  180. return
  181. }
  182. outWidth := imath.MinNonZero(c.ScaledWidth, c.ResultCropWidth)
  183. outHeight := imath.MinNonZero(c.ScaledHeight, c.ResultCropHeight)
  184. if po.ExtendEnabled() {
  185. outWidth = max(outWidth, c.TargetWidth)
  186. outHeight = max(outHeight, c.TargetHeight)
  187. } else if po.ExtendAspectRatioEnabled() {
  188. outWidth = max(outWidth, c.ExtendAspectRatioWidth)
  189. outHeight = max(outHeight, c.ExtendAspectRatioHeight)
  190. }
  191. outWidth += imath.ScaleToEven(po.PaddingLeft(), c.DprScale)
  192. outWidth += imath.ScaleToEven(po.PaddingRight(), c.DprScale)
  193. outHeight += imath.ScaleToEven(po.PaddingTop(), c.DprScale)
  194. outHeight += imath.ScaleToEven(po.PaddingBottom(), c.DprScale)
  195. if maxresultDim > 0 && (outWidth > maxresultDim || outHeight > maxresultDim) {
  196. downScale := float64(maxresultDim) / float64(max(outWidth, outHeight))
  197. c.WScale *= downScale
  198. c.HScale *= downScale
  199. // Prevent scaling below 1px
  200. if minWScale := 1.0 / float64(widthToScale); c.WScale < minWScale {
  201. c.WScale = minWScale
  202. }
  203. if minHScale := 1.0 / float64(heightToScale); c.HScale < minHScale {
  204. c.HScale = minHScale
  205. }
  206. c.DprScale *= downScale
  207. // Recalculate the sizes after changing the scales
  208. c.calcSizes(widthToScale, heightToScale, po)
  209. }
  210. }
  211. // Prepare calculates context image parameters based on the current image size.
  212. // Some steps (like trim) must call this function when finished.
  213. func (c *Context) CalcParams() {
  214. c.SrcWidth, c.SrcHeight, c.Angle, c.Flip = ExtractGeometry(c.Img, c.PO.Rotate(), c.PO.AutoRotate())
  215. c.CropWidth = CalcCropSize(c.SrcWidth, c.PO.CropWidth())
  216. c.CropHeight = CalcCropSize(c.SrcHeight, c.PO.CropHeight())
  217. widthToScale := imath.MinNonZero(c.CropWidth, c.SrcWidth)
  218. heightToScale := imath.MinNonZero(c.CropHeight, c.SrcHeight)
  219. c.calcScale(widthToScale, heightToScale, c.PO)
  220. c.calcSizes(widthToScale, heightToScale, c.PO)
  221. c.limitScale(widthToScale, heightToScale, c.PO)
  222. }