prepare.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package processing
  2. import (
  3. "math"
  4. "github.com/imgproxy/imgproxy/v2/imagedata"
  5. "github.com/imgproxy/imgproxy/v2/imagetype"
  6. "github.com/imgproxy/imgproxy/v2/imath"
  7. "github.com/imgproxy/imgproxy/v2/options"
  8. "github.com/imgproxy/imgproxy/v2/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 calcScale(width, height int, po *options.ProcessingOptions, imgtype imagetype.Type) (float64, float64) {
  36. var wshrink, hshrink float64
  37. srcW, srcH := float64(width), float64(height)
  38. dstW, dstH := float64(po.Width), float64(po.Height)
  39. if po.Width == 0 {
  40. dstW = srcW
  41. }
  42. if dstW == srcW {
  43. wshrink = 1
  44. } else {
  45. wshrink = srcW / dstW
  46. }
  47. if po.Height == 0 {
  48. dstH = srcH
  49. }
  50. if dstH == srcH {
  51. hshrink = 1
  52. } else {
  53. hshrink = srcH / dstH
  54. }
  55. if wshrink != 1 || hshrink != 1 {
  56. rt := po.ResizingType
  57. if rt == options.ResizeAuto {
  58. srcD := srcW - srcH
  59. dstD := dstW - dstH
  60. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  61. rt = options.ResizeFill
  62. } else {
  63. rt = options.ResizeFit
  64. }
  65. }
  66. switch {
  67. case po.Width == 0 && rt != options.ResizeForce:
  68. wshrink = hshrink
  69. case po.Height == 0 && rt != options.ResizeForce:
  70. hshrink = wshrink
  71. case rt == options.ResizeFit:
  72. wshrink = math.Max(wshrink, hshrink)
  73. hshrink = wshrink
  74. case rt == options.ResizeFill || rt == options.ResizeFillDown:
  75. wshrink = math.Min(wshrink, hshrink)
  76. hshrink = wshrink
  77. }
  78. }
  79. if !po.Enlarge && imgtype != imagetype.SVG {
  80. if wshrink < 1 {
  81. hshrink /= wshrink
  82. wshrink = 1
  83. }
  84. if hshrink < 1 {
  85. wshrink /= hshrink
  86. hshrink = 1
  87. }
  88. }
  89. if po.MinWidth > 0 {
  90. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  91. hshrink /= wshrink / minShrink
  92. wshrink = minShrink
  93. }
  94. }
  95. if po.MinHeight > 0 {
  96. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  97. wshrink /= hshrink / minShrink
  98. hshrink = minShrink
  99. }
  100. }
  101. wshrink /= po.Dpr
  102. hshrink /= po.Dpr
  103. if wshrink > srcW {
  104. wshrink = srcW
  105. }
  106. if hshrink > srcH {
  107. hshrink = srcH
  108. }
  109. return 1.0 / wshrink, 1.0 / hshrink
  110. }
  111. func calcCropSize(orig int, crop float64) int {
  112. switch {
  113. case crop == 0.0:
  114. return 0
  115. case crop >= 1.0:
  116. return int(crop)
  117. default:
  118. return imath.Max(1, imath.Scale(orig, crop))
  119. }
  120. }
  121. func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  122. pctx.imgtype = imagetype.Unknown
  123. if imgdata != nil {
  124. pctx.imgtype = imgdata.Type
  125. }
  126. pctx.srcWidth, pctx.srcHeight, pctx.angle, pctx.flip = extractMeta(img, po.Rotate, po.AutoRotate)
  127. pctx.cropWidth = calcCropSize(pctx.srcWidth, po.Crop.Width)
  128. pctx.cropHeight = calcCropSize(pctx.srcHeight, po.Crop.Height)
  129. widthToScale := imath.MinNonZero(pctx.cropWidth, pctx.srcWidth)
  130. heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
  131. pctx.wscale, pctx.hscale = calcScale(widthToScale, heightToScale, po, pctx.imgtype)
  132. if pctx.cropWidth > 0 {
  133. pctx.cropWidth = imath.Max(1, imath.Scale(pctx.cropWidth, pctx.wscale))
  134. }
  135. if pctx.cropHeight > 0 {
  136. pctx.cropHeight = imath.Max(1, imath.Scale(pctx.cropHeight, pctx.hscale))
  137. }
  138. if pctx.cropGravity.Type != options.GravityFocusPoint {
  139. pctx.cropGravity.X *= pctx.wscale
  140. pctx.cropGravity.Y *= pctx.hscale
  141. }
  142. return nil
  143. }