prepare.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 (c *Context) 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 *options.ProcessingOptions) {
  53. wshrink, hshrink := 1.0, 1.0
  54. srcW, srcH := float64(width), float64(height)
  55. dstW := imath.NonZero(float64(po.Width), srcW)
  56. dstH := imath.NonZero(float64(po.Height), srcH)
  57. if dstW != srcW {
  58. wshrink = srcW / dstW
  59. }
  60. if dstH != srcH {
  61. hshrink = srcH / dstH
  62. }
  63. if wshrink != 1 || hshrink != 1 {
  64. rt := po.ResizingType
  65. if rt == options.ResizeAuto {
  66. srcD := srcW - srcH
  67. dstD := dstW - dstH
  68. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  69. rt = options.ResizeFill
  70. } else {
  71. rt = options.ResizeFit
  72. }
  73. }
  74. switch {
  75. case po.Width == 0 && rt != options.ResizeForce:
  76. wshrink = hshrink
  77. case po.Height == 0 && rt != options.ResizeForce:
  78. hshrink = wshrink
  79. case rt == options.ResizeFit:
  80. wshrink = math.Max(wshrink, hshrink)
  81. hshrink = wshrink
  82. case rt == options.ResizeFill || rt == options.ResizeFillDown:
  83. wshrink = math.Min(wshrink, hshrink)
  84. hshrink = wshrink
  85. }
  86. }
  87. wshrink /= po.ZoomWidth
  88. hshrink /= po.ZoomHeight
  89. c.DprScale = po.Dpr
  90. isVector := c.ImgData != nil && c.ImgData.Format().IsVector()
  91. if !po.Enlarge && !isVector {
  92. minShrink := math.Min(wshrink, hshrink)
  93. if minShrink < 1 {
  94. wshrink /= minShrink
  95. hshrink /= minShrink
  96. // If we reached this point, this means that we can't reach the target size
  97. // because the image is smaller than it, and the enlargement is disabled.
  98. // If the DprScale is less than 1, the image will be downscaled, moving
  99. // even further from the target size, so we need to compensate it.
  100. // The compensation may increase the DprScale too much, but this is okay,
  101. // because we'll handle this further in the code.
  102. //
  103. // If the Extend option is enabled, we want to keep the resulting image
  104. // composition the same regardless of the DPR, so we don't apply this compensation
  105. // in this case.
  106. if !po.Extend.Enabled {
  107. c.DprScale /= minShrink
  108. }
  109. }
  110. // The minimum of wshrink and hshrink is the maximum dprScale value
  111. // that can be used without enlarging the image.
  112. c.DprScale = math.Min(c.DprScale, math.Min(wshrink, hshrink))
  113. }
  114. if po.MinWidth > 0 {
  115. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  116. hshrink /= wshrink / minShrink
  117. wshrink = minShrink
  118. }
  119. }
  120. if po.MinHeight > 0 {
  121. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  122. wshrink /= hshrink / minShrink
  123. hshrink = minShrink
  124. }
  125. }
  126. wshrink /= c.DprScale
  127. hshrink /= c.DprScale
  128. if wshrink > srcW {
  129. wshrink = srcW
  130. }
  131. if hshrink > srcH {
  132. hshrink = srcH
  133. }
  134. c.WScale = 1.0 / wshrink
  135. c.HScale = 1.0 / hshrink
  136. }
  137. func (c *Context) calcSizes(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  138. c.TargetWidth = imath.Scale(po.Width, c.DprScale*po.ZoomWidth)
  139. c.TargetHeight = imath.Scale(po.Height, c.DprScale*po.ZoomHeight)
  140. c.ScaledWidth = imath.Scale(widthToScale, c.WScale)
  141. c.ScaledHeight = imath.Scale(heightToScale, c.HScale)
  142. if po.ResizingType == options.ResizeFillDown && !po.Enlarge {
  143. diffW := float64(c.TargetWidth) / float64(c.ScaledWidth)
  144. diffH := float64(c.TargetHeight) / float64(c.ScaledHeight)
  145. switch {
  146. case diffW > diffH && diffW > 1.0:
  147. c.ResultCropHeight = imath.Scale(c.ScaledWidth, float64(c.TargetHeight)/float64(c.TargetWidth))
  148. c.ResultCropWidth = c.ScaledWidth
  149. case diffH > diffW && diffH > 1.0:
  150. c.ResultCropWidth = imath.Scale(c.ScaledHeight, float64(c.TargetWidth)/float64(c.TargetHeight))
  151. c.ResultCropHeight = c.ScaledHeight
  152. default:
  153. c.ResultCropWidth = c.TargetWidth
  154. c.ResultCropHeight = c.TargetHeight
  155. }
  156. } else {
  157. c.ResultCropWidth = c.TargetWidth
  158. c.ResultCropHeight = c.TargetHeight
  159. }
  160. if po.ExtendAspectRatio.Enabled && c.TargetWidth > 0 && c.TargetHeight > 0 {
  161. outWidth := imath.MinNonZero(c.ScaledWidth, c.ResultCropWidth)
  162. outHeight := imath.MinNonZero(c.ScaledHeight, c.ResultCropHeight)
  163. diffW := float64(c.TargetWidth) / float64(outWidth)
  164. diffH := float64(c.TargetHeight) / float64(outHeight)
  165. switch {
  166. case diffH > diffW:
  167. c.ExtendAspectRatioHeight = imath.Scale(outWidth, float64(c.TargetHeight)/float64(c.TargetWidth))
  168. c.ExtendAspectRatioWidth = outWidth
  169. case diffW > diffH:
  170. c.ExtendAspectRatioWidth = imath.Scale(outHeight, float64(c.TargetWidth)/float64(c.TargetHeight))
  171. c.ExtendAspectRatioHeight = outHeight
  172. }
  173. }
  174. }
  175. func (c *Context) limitScale(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  176. maxresultDim := po.SecurityOptions.MaxResultDimension
  177. if maxresultDim <= 0 {
  178. return
  179. }
  180. outWidth := imath.MinNonZero(c.ScaledWidth, c.ResultCropWidth)
  181. outHeight := imath.MinNonZero(c.ScaledHeight, c.ResultCropHeight)
  182. if po.Extend.Enabled {
  183. outWidth = max(outWidth, c.TargetWidth)
  184. outHeight = max(outHeight, c.TargetHeight)
  185. } else if po.ExtendAspectRatio.Enabled {
  186. outWidth = max(outWidth, c.ExtendAspectRatioWidth)
  187. outHeight = max(outHeight, c.ExtendAspectRatioHeight)
  188. }
  189. if po.Padding.Enabled {
  190. outWidth += imath.ScaleToEven(po.Padding.Left, c.DprScale) + imath.ScaleToEven(po.Padding.Right, c.DprScale)
  191. outHeight += imath.ScaleToEven(po.Padding.Top, c.DprScale) + imath.ScaleToEven(po.Padding.Bottom, c.DprScale)
  192. }
  193. if maxresultDim > 0 && (outWidth > maxresultDim || outHeight > maxresultDim) {
  194. downScale := float64(maxresultDim) / float64(max(outWidth, outHeight))
  195. c.WScale *= downScale
  196. c.HScale *= downScale
  197. // Prevent scaling below 1px
  198. if minWScale := 1.0 / float64(widthToScale); c.WScale < minWScale {
  199. c.WScale = minWScale
  200. }
  201. if minHScale := 1.0 / float64(heightToScale); c.HScale < minHScale {
  202. c.HScale = minHScale
  203. }
  204. c.DprScale *= downScale
  205. // Recalculate the sizes after changing the scales
  206. c.calcSizes(widthToScale, heightToScale, po)
  207. }
  208. }
  209. // Prepare calculates context image parameters based on the current image size.
  210. // Some steps (like trim) must call this function when finished.
  211. func (c *Context) CalcParams() {
  212. c.SrcWidth, c.SrcHeight, c.Angle, c.Flip = ExtractGeometry(c.Img, c.PO.Rotate, c.PO.AutoRotate)
  213. c.CropWidth = c.CalcCropSize(c.SrcWidth, c.PO.Crop.Width)
  214. c.CropHeight = c.CalcCropSize(c.SrcHeight, c.PO.Crop.Height)
  215. widthToScale := imath.MinNonZero(c.CropWidth, c.SrcWidth)
  216. heightToScale := imath.MinNonZero(c.CropHeight, c.SrcHeight)
  217. c.calcScale(widthToScale, heightToScale, c.PO)
  218. c.calcSizes(widthToScale, heightToScale, c.PO)
  219. c.limitScale(widthToScale, heightToScale, c.PO)
  220. }