prepare.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. if !po.Enlarge && pctx.ImgData != nil && !pctx.ImgData.Format().IsVector() {
  91. minShrink := math.Min(wshrink, hshrink)
  92. if minShrink < 1 {
  93. wshrink /= minShrink
  94. hshrink /= minShrink
  95. if !po.Extend.Enabled {
  96. pctx.DprScale /= minShrink
  97. }
  98. }
  99. // The minimum of wshrink and hshrink is the maximum dprScale value
  100. // that can be used without enlarging the image.
  101. pctx.DprScale = math.Min(pctx.DprScale, math.Min(wshrink, hshrink))
  102. }
  103. if po.MinWidth > 0 {
  104. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  105. hshrink /= wshrink / minShrink
  106. wshrink = minShrink
  107. }
  108. }
  109. if po.MinHeight > 0 {
  110. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  111. wshrink /= hshrink / minShrink
  112. hshrink = minShrink
  113. }
  114. }
  115. wshrink /= pctx.DprScale
  116. hshrink /= pctx.DprScale
  117. if wshrink > srcW {
  118. wshrink = srcW
  119. }
  120. if hshrink > srcH {
  121. hshrink = srcH
  122. }
  123. pctx.WScale = 1.0 / wshrink
  124. pctx.HScale = 1.0 / hshrink
  125. }
  126. func (pctx *Context) calcSizes(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  127. pctx.TargetWidth = imath.Scale(po.Width, pctx.DprScale*po.ZoomWidth)
  128. pctx.TargetHeight = imath.Scale(po.Height, pctx.DprScale*po.ZoomHeight)
  129. pctx.ScaledWidth = imath.Scale(widthToScale, pctx.WScale)
  130. pctx.ScaledHeight = imath.Scale(heightToScale, pctx.HScale)
  131. if po.ResizingType == options.ResizeFillDown && !po.Enlarge {
  132. diffW := float64(pctx.TargetWidth) / float64(pctx.ScaledWidth)
  133. diffH := float64(pctx.TargetHeight) / float64(pctx.ScaledHeight)
  134. switch {
  135. case diffW > diffH && diffW > 1.0:
  136. pctx.ResultCropHeight = imath.Scale(pctx.ScaledWidth, float64(pctx.TargetHeight)/float64(pctx.TargetWidth))
  137. pctx.ResultCropWidth = pctx.ScaledWidth
  138. case diffH > diffW && diffH > 1.0:
  139. pctx.ResultCropWidth = imath.Scale(pctx.ScaledHeight, float64(pctx.TargetWidth)/float64(pctx.TargetHeight))
  140. pctx.ResultCropHeight = pctx.ScaledHeight
  141. default:
  142. pctx.ResultCropWidth = pctx.TargetWidth
  143. pctx.ResultCropHeight = pctx.TargetHeight
  144. }
  145. } else {
  146. pctx.ResultCropWidth = pctx.TargetWidth
  147. pctx.ResultCropHeight = pctx.TargetHeight
  148. }
  149. if po.ExtendAspectRatio.Enabled && pctx.TargetWidth > 0 && pctx.TargetHeight > 0 {
  150. outWidth := imath.MinNonZero(pctx.ScaledWidth, pctx.ResultCropWidth)
  151. outHeight := imath.MinNonZero(pctx.ScaledHeight, pctx.ResultCropHeight)
  152. diffW := float64(pctx.TargetWidth) / float64(outWidth)
  153. diffH := float64(pctx.TargetHeight) / float64(outHeight)
  154. switch {
  155. case diffH > diffW:
  156. pctx.ExtendAspectRatioHeight = imath.Scale(outWidth, float64(pctx.TargetHeight)/float64(pctx.TargetWidth))
  157. pctx.ExtendAspectRatioWidth = outWidth
  158. case diffW > diffH:
  159. pctx.ExtendAspectRatioWidth = imath.Scale(outHeight, float64(pctx.TargetWidth)/float64(pctx.TargetHeight))
  160. pctx.ExtendAspectRatioHeight = outHeight
  161. }
  162. }
  163. }
  164. func (pctx *Context) limitScale(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  165. maxresultDim := po.SecurityOptions.MaxResultDimension
  166. if maxresultDim <= 0 {
  167. return
  168. }
  169. outWidth := imath.MinNonZero(pctx.ScaledWidth, pctx.ResultCropWidth)
  170. outHeight := imath.MinNonZero(pctx.ScaledHeight, pctx.ResultCropHeight)
  171. if po.Extend.Enabled {
  172. outWidth = max(outWidth, pctx.TargetWidth)
  173. outHeight = max(outHeight, pctx.TargetHeight)
  174. } else if po.ExtendAspectRatio.Enabled {
  175. outWidth = max(outWidth, pctx.ExtendAspectRatioWidth)
  176. outHeight = max(outHeight, pctx.ExtendAspectRatioHeight)
  177. }
  178. if po.Padding.Enabled {
  179. outWidth += imath.ScaleToEven(po.Padding.Left, pctx.DprScale) + imath.ScaleToEven(po.Padding.Right, pctx.DprScale)
  180. outHeight += imath.ScaleToEven(po.Padding.Top, pctx.DprScale) + imath.ScaleToEven(po.Padding.Bottom, pctx.DprScale)
  181. }
  182. if maxresultDim > 0 && (outWidth > maxresultDim || outHeight > maxresultDim) {
  183. downScale := float64(maxresultDim) / float64(max(outWidth, outHeight))
  184. pctx.WScale *= downScale
  185. pctx.HScale *= downScale
  186. // Prevent scaling below 1px
  187. if minWScale := 1.0 / float64(widthToScale); pctx.WScale < minWScale {
  188. pctx.WScale = minWScale
  189. }
  190. if minHScale := 1.0 / float64(heightToScale); pctx.HScale < minHScale {
  191. pctx.HScale = minHScale
  192. }
  193. pctx.DprScale *= downScale
  194. // Recalculate the sizes after changing the scales
  195. pctx.calcSizes(widthToScale, heightToScale, po)
  196. }
  197. }
  198. // prepare extracts image metadata and calculates scaling factors and target sizes.
  199. // This can't be done in advance because some steps like trimming and rasterization could
  200. // happen before this step.
  201. func prepare(c *Context) error {
  202. c.SrcWidth, c.SrcHeight, c.Angle, c.Flip = extractMeta(c.Img, c.PO.Rotate, c.PO.AutoRotate)
  203. c.CropWidth = calcCropSize(c.SrcWidth, c.PO.Crop.Width)
  204. c.CropHeight = calcCropSize(c.SrcHeight, c.PO.Crop.Height)
  205. widthToScale := imath.MinNonZero(c.CropWidth, c.SrcWidth)
  206. heightToScale := imath.MinNonZero(c.CropHeight, c.SrcHeight)
  207. c.calcScale(widthToScale, heightToScale, c.PO)
  208. c.calcSizes(widthToScale, heightToScale, c.PO)
  209. c.limitScale(widthToScale, heightToScale, c.PO)
  210. return nil
  211. }