prepare.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 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. wshrink /= po.Dpr
  56. hshrink /= po.Dpr
  57. if wshrink != 1 || hshrink != 1 {
  58. rt := po.ResizingType
  59. if rt == options.ResizeAuto {
  60. srcD := srcW - srcH
  61. dstD := dstW - dstH
  62. if (srcD >= 0 && dstD >= 0) || (srcD < 0 && dstD < 0) {
  63. rt = options.ResizeFill
  64. } else {
  65. rt = options.ResizeFit
  66. }
  67. }
  68. switch {
  69. case po.Width == 0 && rt != options.ResizeForce:
  70. wshrink = hshrink
  71. case po.Height == 0 && rt != options.ResizeForce:
  72. hshrink = wshrink
  73. case rt == options.ResizeFit:
  74. wshrink = math.Max(wshrink, hshrink)
  75. hshrink = wshrink
  76. case rt == options.ResizeFill || rt == options.ResizeFillDown:
  77. wshrink = math.Min(wshrink, hshrink)
  78. hshrink = wshrink
  79. }
  80. }
  81. wshrink /= po.ZoomWidth
  82. hshrink /= po.ZoomHeight
  83. if !po.Enlarge && imgtype != imagetype.SVG {
  84. if wshrink < 1 {
  85. hshrink /= wshrink
  86. wshrink = 1
  87. }
  88. if hshrink < 1 {
  89. wshrink /= hshrink
  90. hshrink = 1
  91. }
  92. }
  93. if po.MinWidth > 0 {
  94. if minShrink := srcW / float64(po.MinWidth); minShrink < wshrink {
  95. hshrink /= wshrink / minShrink
  96. wshrink = minShrink
  97. }
  98. }
  99. if po.MinHeight > 0 {
  100. if minShrink := srcH / float64(po.MinHeight); minShrink < hshrink {
  101. wshrink /= hshrink / minShrink
  102. hshrink = minShrink
  103. }
  104. }
  105. if wshrink > srcW {
  106. wshrink = srcW
  107. }
  108. if hshrink > srcH {
  109. hshrink = srcH
  110. }
  111. return 1.0 / wshrink, 1.0 / hshrink
  112. }
  113. func calcCropSize(orig int, crop float64) int {
  114. switch {
  115. case crop == 0.0:
  116. return 0
  117. case crop >= 1.0:
  118. return int(crop)
  119. default:
  120. return imath.Max(1, imath.Scale(orig, crop))
  121. }
  122. }
  123. func prepare(pctx *pipelineContext, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
  124. pctx.imgtype = imagetype.Unknown
  125. if imgdata != nil {
  126. pctx.imgtype = imgdata.Type
  127. }
  128. pctx.srcWidth, pctx.srcHeight, pctx.angle, pctx.flip = extractMeta(img, po.Rotate, po.AutoRotate)
  129. pctx.cropWidth = calcCropSize(pctx.srcWidth, po.Crop.Width)
  130. pctx.cropHeight = calcCropSize(pctx.srcHeight, po.Crop.Height)
  131. widthToScale := imath.MinNonZero(pctx.cropWidth, pctx.srcWidth)
  132. heightToScale := imath.MinNonZero(pctx.cropHeight, pctx.srcHeight)
  133. pctx.wscale, pctx.hscale = calcScale(widthToScale, heightToScale, po, pctx.imgtype)
  134. return nil
  135. }