prepare.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. func extractMeta(img *vips.Image, baseAngle int, useOrientation bool) (int, int, int, bool) {
  9. width := img.Width()
  10. height := img.Height()
  11. angle := 0
  12. flip := false
  13. if useOrientation {
  14. orientation := img.Orientation()
  15. if orientation == 3 || orientation == 4 {
  16. angle = 180
  17. }
  18. if orientation == 5 || orientation == 6 {
  19. angle = 90
  20. }
  21. if orientation == 7 || orientation == 8 {
  22. angle = 270
  23. }
  24. if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
  25. flip = true
  26. }
  27. }
  28. if (angle+baseAngle)%180 != 0 {
  29. width, height = height, width
  30. }
  31. return width, height, angle, flip
  32. }
  33. func calcCropSize(orig int, crop float64) int {
  34. switch {
  35. case crop == 0.0:
  36. return 0
  37. case crop >= 1.0:
  38. return int(crop)
  39. default:
  40. return max(1, imath.Scale(orig, crop))
  41. }
  42. }
  43. func (pctx *Context) calcScale(width, height int, po *options.ProcessingOptions) {
  44. var wshrink, hshrink float64
  45. srcW, srcH := float64(width), float64(height)
  46. dstW, dstH := float64(po.Width), float64(po.Height)
  47. if po.Width == 0 {
  48. dstW = srcW
  49. }
  50. if dstW == srcW {
  51. wshrink = 1
  52. } else {
  53. wshrink = srcW / dstW
  54. }
  55. if po.Height == 0 {
  56. dstH = srcH
  57. }
  58. if dstH == srcH {
  59. hshrink = 1
  60. } else {
  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. pctx.DprScale = po.Dpr
  90. isVector := pctx.ImgData != nil && pctx.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. pctx.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. pctx.DprScale = math.Min(pctx.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 /= pctx.DprScale
  127. hshrink /= pctx.DprScale
  128. if wshrink > srcW {
  129. wshrink = srcW
  130. }
  131. if hshrink > srcH {
  132. hshrink = srcH
  133. }
  134. pctx.WScale = 1.0 / wshrink
  135. pctx.HScale = 1.0 / hshrink
  136. }
  137. func (pctx *Context) calcSizes(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  138. pctx.TargetWidth = imath.Scale(po.Width, pctx.DprScale*po.ZoomWidth)
  139. pctx.TargetHeight = imath.Scale(po.Height, pctx.DprScale*po.ZoomHeight)
  140. pctx.ScaledWidth = imath.Scale(widthToScale, pctx.WScale)
  141. pctx.ScaledHeight = imath.Scale(heightToScale, pctx.HScale)
  142. if po.ResizingType == options.ResizeFillDown && !po.Enlarge {
  143. diffW := float64(pctx.TargetWidth) / float64(pctx.ScaledWidth)
  144. diffH := float64(pctx.TargetHeight) / float64(pctx.ScaledHeight)
  145. switch {
  146. case diffW > diffH && diffW > 1.0:
  147. pctx.ResultCropHeight = imath.Scale(pctx.ScaledWidth, float64(pctx.TargetHeight)/float64(pctx.TargetWidth))
  148. pctx.ResultCropWidth = pctx.ScaledWidth
  149. case diffH > diffW && diffH > 1.0:
  150. pctx.ResultCropWidth = imath.Scale(pctx.ScaledHeight, float64(pctx.TargetWidth)/float64(pctx.TargetHeight))
  151. pctx.ResultCropHeight = pctx.ScaledHeight
  152. default:
  153. pctx.ResultCropWidth = pctx.TargetWidth
  154. pctx.ResultCropHeight = pctx.TargetHeight
  155. }
  156. } else {
  157. pctx.ResultCropWidth = pctx.TargetWidth
  158. pctx.ResultCropHeight = pctx.TargetHeight
  159. }
  160. if po.ExtendAspectRatio.Enabled && pctx.TargetWidth > 0 && pctx.TargetHeight > 0 {
  161. outWidth := imath.MinNonZero(pctx.ScaledWidth, pctx.ResultCropWidth)
  162. outHeight := imath.MinNonZero(pctx.ScaledHeight, pctx.ResultCropHeight)
  163. diffW := float64(pctx.TargetWidth) / float64(outWidth)
  164. diffH := float64(pctx.TargetHeight) / float64(outHeight)
  165. switch {
  166. case diffH > diffW:
  167. pctx.ExtendAspectRatioHeight = imath.Scale(outWidth, float64(pctx.TargetHeight)/float64(pctx.TargetWidth))
  168. pctx.ExtendAspectRatioWidth = outWidth
  169. case diffW > diffH:
  170. pctx.ExtendAspectRatioWidth = imath.Scale(outHeight, float64(pctx.TargetWidth)/float64(pctx.TargetHeight))
  171. pctx.ExtendAspectRatioHeight = outHeight
  172. }
  173. }
  174. }
  175. func (pctx *Context) limitScale(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  176. maxresultDim := po.SecurityOptions.MaxResultDimension
  177. if maxresultDim <= 0 {
  178. return
  179. }
  180. outWidth := imath.MinNonZero(pctx.ScaledWidth, pctx.ResultCropWidth)
  181. outHeight := imath.MinNonZero(pctx.ScaledHeight, pctx.ResultCropHeight)
  182. if po.Extend.Enabled {
  183. outWidth = max(outWidth, pctx.TargetWidth)
  184. outHeight = max(outHeight, pctx.TargetHeight)
  185. } else if po.ExtendAspectRatio.Enabled {
  186. outWidth = max(outWidth, pctx.ExtendAspectRatioWidth)
  187. outHeight = max(outHeight, pctx.ExtendAspectRatioHeight)
  188. }
  189. if po.Padding.Enabled {
  190. outWidth += imath.ScaleToEven(po.Padding.Left, pctx.DprScale) + imath.ScaleToEven(po.Padding.Right, pctx.DprScale)
  191. outHeight += imath.ScaleToEven(po.Padding.Top, pctx.DprScale) + imath.ScaleToEven(po.Padding.Bottom, pctx.DprScale)
  192. }
  193. if maxresultDim > 0 && (outWidth > maxresultDim || outHeight > maxresultDim) {
  194. downScale := float64(maxresultDim) / float64(max(outWidth, outHeight))
  195. pctx.WScale *= downScale
  196. pctx.HScale *= downScale
  197. // Prevent scaling below 1px
  198. if minWScale := 1.0 / float64(widthToScale); pctx.WScale < minWScale {
  199. pctx.WScale = minWScale
  200. }
  201. if minHScale := 1.0 / float64(heightToScale); pctx.HScale < minHScale {
  202. pctx.HScale = minHScale
  203. }
  204. pctx.DprScale *= downScale
  205. // Recalculate the sizes after changing the scales
  206. pctx.calcSizes(widthToScale, heightToScale, po)
  207. }
  208. }
  209. // prepare extracts image metadata and calculates scaling factors and target sizes.
  210. // This can't be done in advance because some steps like trimming and rasterization could
  211. // happen before this step.
  212. func prepare(c *Context) error {
  213. c.SrcWidth, c.SrcHeight, c.Angle, c.Flip = extractMeta(c.Img, c.PO.Rotate, c.PO.AutoRotate)
  214. c.CropWidth = calcCropSize(c.SrcWidth, c.PO.Crop.Width)
  215. c.CropHeight = calcCropSize(c.SrcHeight, c.PO.Crop.Height)
  216. widthToScale := imath.MinNonZero(c.CropWidth, c.SrcWidth)
  217. heightToScale := imath.MinNonZero(c.CropHeight, c.SrcHeight)
  218. c.calcScale(widthToScale, heightToScale, c.PO)
  219. c.calcSizes(widthToScale, heightToScale, c.PO)
  220. c.limitScale(widthToScale, heightToScale, c.PO)
  221. return nil
  222. }