prepare.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 calcCropSize(orig int, crop float64) int {
  36. switch {
  37. case crop == 0.0:
  38. return 0
  39. case crop >= 1.0:
  40. return int(crop)
  41. default:
  42. return imath.Max(1, imath.Scale(orig, crop))
  43. }
  44. }
  45. func (pctx *pipelineContext) calcScale(width, height int, po *options.ProcessingOptions) {
  46. var wshrink, hshrink float64
  47. srcW, srcH := float64(width), float64(height)
  48. dstW, dstH := float64(po.Width), float64(po.Height)
  49. if po.Width == 0 {
  50. dstW = srcW
  51. }
  52. if dstW == srcW {
  53. wshrink = 1
  54. } else {
  55. wshrink = srcW / dstW
  56. }
  57. if po.Height == 0 {
  58. dstH = srcH
  59. }
  60. if dstH == srcH {
  61. hshrink = 1
  62. } else {
  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 po.Width == 0 && rt != options.ResizeForce:
  78. wshrink = hshrink
  79. case po.Height == 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. pctx.dprScale = po.Dpr
  92. if !po.Enlarge && !pctx.imgtype.IsVector() {
  93. minShrink := math.Min(wshrink, hshrink)
  94. if minShrink < 1 {
  95. wshrink /= minShrink
  96. hshrink /= minShrink
  97. if !po.Extend.Enabled {
  98. pctx.dprScale /= minShrink
  99. }
  100. }
  101. // The minimum of wshrink and hshrink is the maximum dprScale value
  102. // that can be used without enlarging the image.
  103. pctx.dprScale = math.Min(pctx.dprScale, math.Min(wshrink, hshrink))
  104. }
  105. if po.MinWidth > 0 {
  106. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  107. hshrink /= wshrink / minShrink
  108. wshrink = minShrink
  109. }
  110. }
  111. if po.MinHeight > 0 {
  112. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  113. wshrink /= hshrink / minShrink
  114. hshrink = minShrink
  115. }
  116. }
  117. wshrink /= pctx.dprScale
  118. hshrink /= pctx.dprScale
  119. if wshrink > srcW {
  120. wshrink = srcW
  121. }
  122. if hshrink > srcH {
  123. hshrink = srcH
  124. }
  125. pctx.wscale = 1.0 / wshrink
  126. pctx.hscale = 1.0 / hshrink
  127. }
  128. func (pctx *pipelineContext) calcSizes(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  129. pctx.targetWidth = imath.Scale(po.Width, pctx.dprScale*po.ZoomWidth)
  130. pctx.targetHeight = imath.Scale(po.Height, pctx.dprScale*po.ZoomHeight)
  131. pctx.scaledWidth = imath.Scale(widthToScale, pctx.wscale)
  132. pctx.scaledHeight = imath.Scale(heightToScale, pctx.hscale)
  133. if po.ResizingType == options.ResizeFillDown && !po.Enlarge {
  134. diffW := float64(pctx.targetWidth) / float64(pctx.scaledWidth)
  135. diffH := float64(pctx.targetHeight) / float64(pctx.scaledHeight)
  136. switch {
  137. case diffW > diffH && diffW > 1.0:
  138. pctx.resultCropHeight = imath.Scale(pctx.scaledWidth, float64(pctx.targetHeight)/float64(pctx.targetWidth))
  139. pctx.resultCropWidth = pctx.scaledWidth
  140. case diffH > diffW && diffH > 1.0:
  141. pctx.resultCropWidth = imath.Scale(pctx.scaledHeight, float64(pctx.targetWidth)/float64(pctx.targetHeight))
  142. pctx.resultCropHeight = pctx.scaledHeight
  143. default:
  144. pctx.resultCropWidth = pctx.targetWidth
  145. pctx.resultCropHeight = pctx.targetHeight
  146. }
  147. } else {
  148. pctx.resultCropWidth = pctx.targetWidth
  149. pctx.resultCropHeight = pctx.targetHeight
  150. }
  151. if po.ExtendAspectRatio.Enabled && pctx.targetWidth > 0 && pctx.targetHeight > 0 {
  152. outWidth := imath.MinNonZero(pctx.scaledWidth, pctx.resultCropWidth)
  153. outHeight := imath.MinNonZero(pctx.scaledHeight, pctx.resultCropHeight)
  154. diffW := float64(pctx.targetWidth) / float64(outWidth)
  155. diffH := float64(pctx.targetHeight) / float64(outHeight)
  156. switch {
  157. case diffH > diffW:
  158. pctx.extendAspectRatioHeight = imath.Scale(outWidth, float64(pctx.targetHeight)/float64(pctx.targetWidth))
  159. pctx.extendAspectRatioWidth = outWidth
  160. case diffW > diffH:
  161. pctx.extendAspectRatioWidth = imath.Scale(outHeight, float64(pctx.targetWidth)/float64(pctx.targetHeight))
  162. pctx.extendAspectRatioHeight = outHeight
  163. }
  164. }
  165. }
  166. func (pctx *pipelineContext) limitScale(widthToScale, heightToScale int, po *options.ProcessingOptions) {
  167. maxresultDim := po.SecurityOptions.MaxResultDimension
  168. if maxresultDim <= 0 {
  169. return
  170. }
  171. outWidth := imath.MinNonZero(pctx.scaledWidth, pctx.resultCropWidth)
  172. outHeight := imath.MinNonZero(pctx.scaledHeight, pctx.resultCropHeight)
  173. if po.Extend.Enabled {
  174. outWidth = imath.Max(outWidth, pctx.targetWidth)
  175. outHeight = imath.Max(outHeight, pctx.targetHeight)
  176. } else if po.ExtendAspectRatio.Enabled {
  177. outWidth = imath.Max(outWidth, pctx.extendAspectRatioWidth)
  178. outHeight = imath.Max(outHeight, pctx.extendAspectRatioHeight)
  179. }
  180. if po.Padding.Enabled {
  181. outWidth += imath.ScaleToEven(po.Padding.Left, pctx.dprScale) + imath.ScaleToEven(po.Padding.Right, pctx.dprScale)
  182. outHeight += imath.ScaleToEven(po.Padding.Top, pctx.dprScale) + imath.ScaleToEven(po.Padding.Bottom, pctx.dprScale)
  183. }
  184. if maxresultDim > 0 && (outWidth > maxresultDim || outHeight > maxresultDim) {
  185. downScale := float64(maxresultDim) / float64(imath.Max(outWidth, outHeight))
  186. pctx.wscale *= downScale
  187. pctx.hscale *= downScale
  188. // Prevent scaling below 1px
  189. if minWScale := 1.0 / float64(widthToScale); pctx.wscale < minWScale {
  190. pctx.wscale = minWScale
  191. }
  192. if minHScale := 1.0 / float64(heightToScale); pctx.hscale < minHScale {
  193. pctx.hscale = minHScale
  194. }
  195. pctx.dprScale *= downScale
  196. // Recalculate the sizes after changing the scales
  197. pctx.calcSizes(widthToScale, heightToScale, po)
  198. }
  199. }
  200. func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  201. pctx.imgtype = imagetype.Unknown
  202. if imgdata != nil {
  203. pctx.imgtype = imgdata.Type
  204. }
  205. pctx.srcWidth, pctx.srcHeight, pctx.angle, pctx.flip = extractMeta(img, po.Rotate, po.AutoRotate)
  206. pctx.cropWidth = calcCropSize(pctx.srcWidth, po.Crop.Width)
  207. pctx.cropHeight = calcCropSize(pctx.srcHeight, po.Crop.Height)
  208. widthToScale := imath.MinNonZero(pctx.cropWidth, pctx.srcWidth)
  209. heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
  210. pctx.calcScale(widthToScale, heightToScale, po)
  211. // The size of a vector image is not checked during download, yet it can be very large.
  212. // So we should scale it down to the maximum allowed resolution
  213. if !pctx.trimmed && imgdata != nil && imgdata.Type.IsVector() && !po.Enlarge {
  214. resolution := imath.Round((float64(img.Width()*img.Height()) * pctx.wscale * pctx.hscale))
  215. if resolution > po.SecurityOptions.MaxSrcResolution {
  216. scale := math.Sqrt(float64(po.SecurityOptions.MaxSrcResolution) / float64(resolution))
  217. pctx.wscale *= scale
  218. pctx.hscale *= scale
  219. }
  220. }
  221. pctx.calcSizes(widthToScale, heightToScale, po)
  222. pctx.limitScale(widthToScale, heightToScale, po)
  223. return nil
  224. }