1
0

processing_test.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. package processing
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/sirupsen/logrus"
  10. "github.com/stretchr/testify/suite"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. "github.com/imgproxy/imgproxy/v3/fetcher"
  13. "github.com/imgproxy/imgproxy/v3/ierrors"
  14. "github.com/imgproxy/imgproxy/v3/imagedata"
  15. "github.com/imgproxy/imgproxy/v3/options"
  16. "github.com/imgproxy/imgproxy/v3/transport"
  17. "github.com/imgproxy/imgproxy/v3/vips"
  18. )
  19. type ProcessingTestSuite struct {
  20. suite.Suite
  21. idf *imagedata.Factory
  22. }
  23. func (s *ProcessingTestSuite) SetupSuite() {
  24. config.Reset()
  25. config.MaxSrcResolution = 10 * 1024 * 1024
  26. config.MaxSrcFileSize = 10 * 1024 * 1024
  27. config.MaxAnimationFrames = 100
  28. config.MaxAnimationFrameResolution = 10 * 1024 * 1024
  29. s.Require().NoError(vips.Init())
  30. logrus.SetOutput(io.Discard)
  31. trc := transport.NewDefaultConfig()
  32. tr, err := transport.New(trc)
  33. s.Require().NoError(err)
  34. fc := fetcher.NewDefaultConfig()
  35. f, err := fetcher.New(tr, fc)
  36. s.Require().NoError(err)
  37. s.idf = imagedata.NewFactory(f)
  38. }
  39. func (s *ProcessingTestSuite) openFile(name string) imagedata.ImageData {
  40. wd, err := os.Getwd()
  41. s.Require().NoError(err)
  42. path := filepath.Join(wd, "..", "testdata", name)
  43. imagedata, err := s.idf.NewFromPath(path)
  44. s.Require().NoError(err)
  45. return imagedata
  46. }
  47. func (s *ProcessingTestSuite) checkSize(r *Result, width, height int) {
  48. s.Require().NotNil(r)
  49. s.Require().Equal(width, r.ResultWidth, "Width mismatch")
  50. s.Require().Equal(height, r.ResultHeight, "Height mismatch")
  51. }
  52. func (s *ProcessingTestSuite) TestResizeToFit() {
  53. imgdata := s.openFile("test2.jpg")
  54. po := options.NewProcessingOptions()
  55. po.ResizingType = options.ResizeFit
  56. testCases := []struct {
  57. width int
  58. height int
  59. outWidth int
  60. outHeight int
  61. }{
  62. {width: 50, height: 50, outWidth: 50, outHeight: 25},
  63. {width: 50, height: 20, outWidth: 40, outHeight: 20},
  64. {width: 20, height: 50, outWidth: 20, outHeight: 10},
  65. {width: 300, height: 300, outWidth: 200, outHeight: 100},
  66. {width: 300, height: 50, outWidth: 100, outHeight: 50},
  67. {width: 100, height: 300, outWidth: 100, outHeight: 50},
  68. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  69. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  70. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  71. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  72. }
  73. for _, tc := range testCases {
  74. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  75. po.Width = tc.width
  76. po.Height = tc.height
  77. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  78. s.Require().NoError(err)
  79. s.Require().NotNil(result)
  80. s.checkSize(result, tc.outWidth, tc.outHeight)
  81. })
  82. }
  83. }
  84. func (s *ProcessingTestSuite) TestResizeToFitEnlarge() {
  85. imgdata := s.openFile("test2.jpg")
  86. po := options.NewProcessingOptions()
  87. po.ResizingType = options.ResizeFit
  88. po.Enlarge = true
  89. testCases := []struct {
  90. width int
  91. height int
  92. outWidth int
  93. outHeight int
  94. }{
  95. {width: 50, height: 50, outWidth: 50, outHeight: 25},
  96. {width: 50, height: 20, outWidth: 40, outHeight: 20},
  97. {width: 20, height: 50, outWidth: 20, outHeight: 10},
  98. {width: 300, height: 300, outWidth: 300, outHeight: 150},
  99. {width: 300, height: 125, outWidth: 250, outHeight: 125},
  100. {width: 250, height: 300, outWidth: 250, outHeight: 125},
  101. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  102. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  103. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  104. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  105. }
  106. for _, tc := range testCases {
  107. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  108. po.Width = tc.width
  109. po.Height = tc.height
  110. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  111. s.Require().NoError(err)
  112. s.Require().NotNil(result)
  113. s.checkSize(result, tc.outWidth, tc.outHeight)
  114. })
  115. }
  116. }
  117. func (s *ProcessingTestSuite) TestResizeToFitExtend() {
  118. imgdata := s.openFile("test2.jpg")
  119. po := options.NewProcessingOptions()
  120. po.ResizingType = options.ResizeFit
  121. po.Extend = options.ExtendOptions{
  122. Enabled: true,
  123. Gravity: options.GravityOptions{
  124. Type: options.GravityCenter,
  125. },
  126. }
  127. testCases := []struct {
  128. width int
  129. height int
  130. outWidth int
  131. outHeight int
  132. }{
  133. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  134. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  135. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  136. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  137. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  138. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  139. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  140. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  141. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  142. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  143. }
  144. for _, tc := range testCases {
  145. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  146. po.Width = tc.width
  147. po.Height = tc.height
  148. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  149. s.Require().NoError(err)
  150. s.Require().NotNil(result)
  151. s.checkSize(result, tc.outWidth, tc.outHeight)
  152. })
  153. }
  154. }
  155. func (s *ProcessingTestSuite) TestResizeToFitExtendAR() {
  156. imgdata := s.openFile("test2.jpg")
  157. po := options.NewProcessingOptions()
  158. po.ResizingType = options.ResizeFit
  159. po.ExtendAspectRatio = options.ExtendOptions{
  160. Enabled: true,
  161. Gravity: options.GravityOptions{
  162. Type: options.GravityCenter,
  163. },
  164. }
  165. testCases := []struct {
  166. width int
  167. height int
  168. outWidth int
  169. outHeight int
  170. }{
  171. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  172. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  173. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  174. {width: 300, height: 300, outWidth: 200, outHeight: 200},
  175. {width: 300, height: 125, outWidth: 240, outHeight: 100},
  176. {width: 250, height: 500, outWidth: 200, outHeight: 400},
  177. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  178. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  179. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  180. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  181. }
  182. for _, tc := range testCases {
  183. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  184. po.Width = tc.width
  185. po.Height = tc.height
  186. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  187. s.Require().NoError(err)
  188. s.Require().NotNil(result)
  189. s.checkSize(result, tc.outWidth, tc.outHeight)
  190. })
  191. }
  192. }
  193. func (s *ProcessingTestSuite) TestResizeToFill() {
  194. imgdata := s.openFile("test2.jpg")
  195. po := options.NewProcessingOptions()
  196. po.ResizingType = options.ResizeFill
  197. testCases := []struct {
  198. width int
  199. height int
  200. outWidth int
  201. outHeight int
  202. }{
  203. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  204. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  205. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  206. {width: 300, height: 300, outWidth: 200, outHeight: 100},
  207. {width: 300, height: 50, outWidth: 200, outHeight: 50},
  208. {width: 100, height: 300, outWidth: 100, outHeight: 100},
  209. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  210. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  211. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  212. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  213. }
  214. for _, tc := range testCases {
  215. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  216. po.Width = tc.width
  217. po.Height = tc.height
  218. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  219. s.Require().NoError(err)
  220. s.Require().NotNil(result)
  221. s.checkSize(result, tc.outWidth, tc.outHeight)
  222. })
  223. }
  224. }
  225. func (s *ProcessingTestSuite) TestResizeToFillEnlarge() {
  226. imgdata := s.openFile("test2.jpg")
  227. po := options.NewProcessingOptions()
  228. po.ResizingType = options.ResizeFill
  229. po.Enlarge = true
  230. testCases := []struct {
  231. width int
  232. height int
  233. outWidth int
  234. outHeight int
  235. }{
  236. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  237. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  238. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  239. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  240. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  241. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  242. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  243. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  244. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  245. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  246. }
  247. for _, tc := range testCases {
  248. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  249. po.Width = tc.width
  250. po.Height = tc.height
  251. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  252. s.Require().NoError(err)
  253. s.Require().NotNil(result)
  254. s.checkSize(result, tc.outWidth, tc.outHeight)
  255. })
  256. }
  257. }
  258. func (s *ProcessingTestSuite) TestResizeToFillExtend() {
  259. imgdata := s.openFile("test2.jpg")
  260. po := options.NewProcessingOptions()
  261. po.ResizingType = options.ResizeFill
  262. po.Extend = options.ExtendOptions{
  263. Enabled: true,
  264. Gravity: options.GravityOptions{
  265. Type: options.GravityCenter,
  266. },
  267. }
  268. testCases := []struct {
  269. width int
  270. height int
  271. outWidth int
  272. outHeight int
  273. }{
  274. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  275. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  276. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  277. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  278. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  279. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  280. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  281. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  282. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  283. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  284. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  285. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  286. }
  287. for _, tc := range testCases {
  288. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  289. po.Width = tc.width
  290. po.Height = tc.height
  291. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  292. s.Require().NoError(err)
  293. s.Require().NotNil(result)
  294. s.checkSize(result, tc.outWidth, tc.outHeight)
  295. })
  296. }
  297. }
  298. func (s *ProcessingTestSuite) TestResizeToFillExtendAR() {
  299. imgdata := s.openFile("test2.jpg")
  300. po := options.NewProcessingOptions()
  301. po.ResizingType = options.ResizeFill
  302. po.ExtendAspectRatio = options.ExtendOptions{
  303. Enabled: true,
  304. Gravity: options.GravityOptions{
  305. Type: options.GravityCenter,
  306. },
  307. }
  308. testCases := []struct {
  309. width int
  310. height int
  311. outWidth int
  312. outHeight int
  313. }{
  314. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  315. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  316. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  317. {width: 300, height: 300, outWidth: 200, outHeight: 200},
  318. {width: 300, height: 125, outWidth: 240, outHeight: 100},
  319. {width: 250, height: 500, outWidth: 200, outHeight: 400},
  320. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  321. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  322. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  323. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  324. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  325. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  326. }
  327. for _, tc := range testCases {
  328. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  329. po.Width = tc.width
  330. po.Height = tc.height
  331. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  332. s.Require().NoError(err)
  333. s.Require().NotNil(result)
  334. s.checkSize(result, tc.outWidth, tc.outHeight)
  335. })
  336. }
  337. }
  338. func (s *ProcessingTestSuite) TestResizeToFillDown() {
  339. imgdata := s.openFile("test2.jpg")
  340. po := options.NewProcessingOptions()
  341. po.ResizingType = options.ResizeFillDown
  342. testCases := []struct {
  343. width int
  344. height int
  345. outWidth int
  346. outHeight int
  347. }{
  348. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  349. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  350. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  351. {width: 300, height: 300, outWidth: 100, outHeight: 100},
  352. {width: 300, height: 125, outWidth: 200, outHeight: 83},
  353. {width: 250, height: 300, outWidth: 83, outHeight: 100},
  354. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  355. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  356. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  357. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  358. }
  359. for _, tc := range testCases {
  360. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  361. po.Width = tc.width
  362. po.Height = tc.height
  363. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  364. s.Require().NoError(err)
  365. s.Require().NotNil(result)
  366. s.checkSize(result, tc.outWidth, tc.outHeight)
  367. })
  368. }
  369. }
  370. func (s *ProcessingTestSuite) TestResizeToFillDownEnlarge() {
  371. imgdata := s.openFile("test2.jpg")
  372. po := options.NewProcessingOptions()
  373. po.ResizingType = options.ResizeFillDown
  374. po.Enlarge = true
  375. testCases := []struct {
  376. width int
  377. height int
  378. outWidth int
  379. outHeight int
  380. }{
  381. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  382. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  383. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  384. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  385. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  386. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  387. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  388. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  389. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  390. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  391. }
  392. for _, tc := range testCases {
  393. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  394. po.Width = tc.width
  395. po.Height = tc.height
  396. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  397. s.Require().NoError(err)
  398. s.Require().NotNil(result)
  399. s.checkSize(result, tc.outWidth, tc.outHeight)
  400. })
  401. }
  402. }
  403. func (s *ProcessingTestSuite) TestResizeToFillDownExtend() {
  404. imgdata := s.openFile("test2.jpg")
  405. po := options.NewProcessingOptions()
  406. po.ResizingType = options.ResizeFillDown
  407. po.Extend = options.ExtendOptions{
  408. Enabled: true,
  409. Gravity: options.GravityOptions{
  410. Type: options.GravityCenter,
  411. },
  412. }
  413. testCases := []struct {
  414. width int
  415. height int
  416. outWidth int
  417. outHeight int
  418. }{
  419. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  420. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  421. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  422. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  423. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  424. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  425. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  426. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  427. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  428. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  429. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  430. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  431. }
  432. for _, tc := range testCases {
  433. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  434. po.Width = tc.width
  435. po.Height = tc.height
  436. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  437. s.Require().NoError(err)
  438. s.Require().NotNil(result)
  439. s.checkSize(result, tc.outWidth, tc.outHeight)
  440. })
  441. }
  442. }
  443. func (s *ProcessingTestSuite) TestResizeToFillDownExtendAR() {
  444. imgdata := s.openFile("test2.jpg")
  445. po := options.NewProcessingOptions()
  446. po.ResizingType = options.ResizeFillDown
  447. po.ExtendAspectRatio = options.ExtendOptions{
  448. Enabled: true,
  449. Gravity: options.GravityOptions{
  450. Type: options.GravityCenter,
  451. },
  452. }
  453. testCases := []struct {
  454. width int
  455. height int
  456. outWidth int
  457. outHeight int
  458. }{
  459. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  460. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  461. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  462. {width: 300, height: 300, outWidth: 100, outHeight: 100},
  463. {width: 300, height: 125, outWidth: 200, outHeight: 83},
  464. {width: 250, height: 300, outWidth: 83, outHeight: 100},
  465. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  466. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  467. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  468. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  469. }
  470. for _, tc := range testCases {
  471. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  472. po.Width = tc.width
  473. po.Height = tc.height
  474. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  475. s.Require().NoError(err)
  476. s.Require().NotNil(result)
  477. s.checkSize(result, tc.outWidth, tc.outHeight)
  478. })
  479. }
  480. }
  481. func (s *ProcessingTestSuite) TestResultSizeLimit() {
  482. imgdata := s.openFile("test2.jpg")
  483. po := options.NewProcessingOptions()
  484. testCases := []struct {
  485. limit int
  486. width int
  487. height int
  488. resizingType options.ResizeType
  489. enlarge bool
  490. extend bool
  491. extendAR bool
  492. padding options.PaddingOptions
  493. rotate int
  494. outWidth int
  495. outHeight int
  496. }{
  497. {
  498. limit: 1000,
  499. width: 100,
  500. height: 100,
  501. resizingType: options.ResizeFit,
  502. outWidth: 100,
  503. outHeight: 50,
  504. },
  505. {
  506. limit: 50,
  507. width: 100,
  508. height: 100,
  509. resizingType: options.ResizeFit,
  510. outWidth: 50,
  511. outHeight: 25,
  512. },
  513. {
  514. limit: 50,
  515. width: 0,
  516. height: 0,
  517. resizingType: options.ResizeFit,
  518. outWidth: 50,
  519. outHeight: 25,
  520. },
  521. {
  522. limit: 100,
  523. width: 0,
  524. height: 100,
  525. resizingType: options.ResizeFit,
  526. outWidth: 100,
  527. outHeight: 50,
  528. },
  529. {
  530. limit: 50,
  531. width: 150,
  532. height: 0,
  533. resizingType: options.ResizeFit,
  534. outWidth: 50,
  535. outHeight: 25,
  536. },
  537. {
  538. limit: 100,
  539. width: 1000,
  540. height: 1000,
  541. resizingType: options.ResizeFit,
  542. outWidth: 100,
  543. outHeight: 50,
  544. },
  545. {
  546. limit: 100,
  547. width: 1000,
  548. height: 1000,
  549. resizingType: options.ResizeFit,
  550. enlarge: true,
  551. outWidth: 100,
  552. outHeight: 50,
  553. },
  554. {
  555. limit: 100,
  556. width: 1000,
  557. height: 2000,
  558. resizingType: options.ResizeFit,
  559. extend: true,
  560. outWidth: 50,
  561. outHeight: 100,
  562. },
  563. {
  564. limit: 100,
  565. width: 1000,
  566. height: 2000,
  567. resizingType: options.ResizeFit,
  568. extendAR: true,
  569. outWidth: 50,
  570. outHeight: 100,
  571. },
  572. {
  573. limit: 100,
  574. width: 100,
  575. height: 150,
  576. resizingType: options.ResizeFit,
  577. rotate: 90,
  578. outWidth: 50,
  579. outHeight: 100,
  580. },
  581. {
  582. limit: 100,
  583. width: 0,
  584. height: 0,
  585. resizingType: options.ResizeFit,
  586. rotate: 90,
  587. outWidth: 50,
  588. outHeight: 100,
  589. },
  590. {
  591. limit: 200,
  592. width: 100,
  593. height: 100,
  594. resizingType: options.ResizeFit,
  595. padding: options.PaddingOptions{
  596. Enabled: true,
  597. Top: 100,
  598. Right: 200,
  599. Bottom: 300,
  600. Left: 400,
  601. },
  602. outWidth: 200,
  603. outHeight: 129,
  604. },
  605. {
  606. limit: 1000,
  607. width: 100,
  608. height: 100,
  609. resizingType: options.ResizeFill,
  610. outWidth: 100,
  611. outHeight: 100,
  612. },
  613. {
  614. limit: 50,
  615. width: 100,
  616. height: 100,
  617. resizingType: options.ResizeFill,
  618. outWidth: 50,
  619. outHeight: 50,
  620. },
  621. {
  622. limit: 50,
  623. width: 1000,
  624. height: 50,
  625. resizingType: options.ResizeFill,
  626. outWidth: 50,
  627. outHeight: 13,
  628. },
  629. {
  630. limit: 50,
  631. width: 100,
  632. height: 1000,
  633. resizingType: options.ResizeFill,
  634. outWidth: 50,
  635. outHeight: 50,
  636. },
  637. {
  638. limit: 50,
  639. width: 0,
  640. height: 0,
  641. resizingType: options.ResizeFill,
  642. outWidth: 50,
  643. outHeight: 25,
  644. },
  645. {
  646. limit: 100,
  647. width: 0,
  648. height: 100,
  649. resizingType: options.ResizeFill,
  650. outWidth: 100,
  651. outHeight: 50,
  652. },
  653. {
  654. limit: 50,
  655. width: 150,
  656. height: 0,
  657. resizingType: options.ResizeFill,
  658. outWidth: 50,
  659. outHeight: 25,
  660. },
  661. {
  662. limit: 100,
  663. width: 1000,
  664. height: 1000,
  665. resizingType: options.ResizeFill,
  666. outWidth: 100,
  667. outHeight: 50,
  668. },
  669. {
  670. limit: 100,
  671. width: 1000,
  672. height: 1000,
  673. resizingType: options.ResizeFill,
  674. enlarge: true,
  675. outWidth: 100,
  676. outHeight: 100,
  677. },
  678. {
  679. limit: 100,
  680. width: 1000,
  681. height: 2000,
  682. resizingType: options.ResizeFill,
  683. extend: true,
  684. outWidth: 50,
  685. outHeight: 100,
  686. },
  687. {
  688. limit: 100,
  689. width: 1000,
  690. height: 2000,
  691. resizingType: options.ResizeFill,
  692. extendAR: true,
  693. outWidth: 50,
  694. outHeight: 100,
  695. },
  696. {
  697. limit: 100,
  698. width: 100,
  699. height: 150,
  700. resizingType: options.ResizeFill,
  701. rotate: 90,
  702. outWidth: 67,
  703. outHeight: 100,
  704. },
  705. {
  706. limit: 100,
  707. width: 0,
  708. height: 0,
  709. resizingType: options.ResizeFill,
  710. rotate: 90,
  711. outWidth: 50,
  712. outHeight: 100,
  713. },
  714. {
  715. limit: 200,
  716. width: 100,
  717. height: 100,
  718. resizingType: options.ResizeFill,
  719. padding: options.PaddingOptions{
  720. Enabled: true,
  721. Top: 100,
  722. Right: 200,
  723. Bottom: 300,
  724. Left: 400,
  725. },
  726. outWidth: 200,
  727. outHeight: 144,
  728. },
  729. {
  730. limit: 1000,
  731. width: 100,
  732. height: 100,
  733. resizingType: options.ResizeFillDown,
  734. outWidth: 100,
  735. outHeight: 100,
  736. },
  737. {
  738. limit: 50,
  739. width: 100,
  740. height: 100,
  741. resizingType: options.ResizeFillDown,
  742. outWidth: 50,
  743. outHeight: 50,
  744. },
  745. {
  746. limit: 50,
  747. width: 1000,
  748. height: 50,
  749. resizingType: options.ResizeFillDown,
  750. outWidth: 50,
  751. outHeight: 3,
  752. },
  753. {
  754. limit: 50,
  755. width: 100,
  756. height: 1000,
  757. resizingType: options.ResizeFillDown,
  758. outWidth: 5,
  759. outHeight: 50,
  760. },
  761. {
  762. limit: 50,
  763. width: 0,
  764. height: 0,
  765. resizingType: options.ResizeFillDown,
  766. outWidth: 50,
  767. outHeight: 25,
  768. },
  769. {
  770. limit: 100,
  771. width: 0,
  772. height: 100,
  773. resizingType: options.ResizeFillDown,
  774. outWidth: 100,
  775. outHeight: 50,
  776. },
  777. {
  778. limit: 50,
  779. width: 150,
  780. height: 0,
  781. resizingType: options.ResizeFillDown,
  782. outWidth: 50,
  783. outHeight: 25,
  784. },
  785. {
  786. limit: 100,
  787. width: 1000,
  788. height: 1000,
  789. resizingType: options.ResizeFillDown,
  790. outWidth: 100,
  791. outHeight: 100,
  792. },
  793. {
  794. limit: 100,
  795. width: 1000,
  796. height: 1000,
  797. resizingType: options.ResizeFillDown,
  798. enlarge: true,
  799. outWidth: 100,
  800. outHeight: 100,
  801. },
  802. {
  803. limit: 100,
  804. width: 1000,
  805. height: 2000,
  806. resizingType: options.ResizeFillDown,
  807. extend: true,
  808. outWidth: 50,
  809. outHeight: 100,
  810. },
  811. {
  812. limit: 100,
  813. width: 1000,
  814. height: 2000,
  815. resizingType: options.ResizeFillDown,
  816. extendAR: true,
  817. outWidth: 50,
  818. outHeight: 100,
  819. },
  820. {
  821. limit: 100,
  822. width: 1000,
  823. height: 1500,
  824. resizingType: options.ResizeFillDown,
  825. rotate: 90,
  826. outWidth: 67,
  827. outHeight: 100,
  828. },
  829. {
  830. limit: 100,
  831. width: 0,
  832. height: 0,
  833. resizingType: options.ResizeFillDown,
  834. rotate: 90,
  835. outWidth: 50,
  836. outHeight: 100,
  837. },
  838. {
  839. limit: 200,
  840. width: 100,
  841. height: 100,
  842. resizingType: options.ResizeFillDown,
  843. padding: options.PaddingOptions{
  844. Enabled: true,
  845. Top: 100,
  846. Right: 200,
  847. Bottom: 300,
  848. Left: 400,
  849. },
  850. outWidth: 200,
  851. outHeight: 144,
  852. },
  853. {
  854. limit: 200,
  855. width: 1000,
  856. height: 1000,
  857. resizingType: options.ResizeFillDown,
  858. padding: options.PaddingOptions{
  859. Enabled: true,
  860. Top: 100,
  861. Right: 200,
  862. Bottom: 300,
  863. Left: 400,
  864. },
  865. outWidth: 200,
  866. outHeight: 144,
  867. },
  868. }
  869. for _, tc := range testCases {
  870. name := fmt.Sprintf("%s_%dx%d_limit_%d", tc.resizingType, tc.width, tc.height, tc.limit)
  871. if tc.enlarge {
  872. name += "_enlarge"
  873. }
  874. if tc.extend {
  875. name += "_extend"
  876. }
  877. if tc.extendAR {
  878. name += "_extendAR"
  879. }
  880. if tc.rotate != 0 {
  881. name += fmt.Sprintf("_rot_%d", tc.rotate)
  882. }
  883. if tc.padding.Enabled {
  884. name += fmt.Sprintf("_padding_%dx%dx%dx%d", tc.padding.Top, tc.padding.Right, tc.padding.Bottom, tc.padding.Left)
  885. }
  886. s.Run(name, func() {
  887. po.SecurityOptions.MaxResultDimension = tc.limit
  888. po.Width = tc.width
  889. po.Height = tc.height
  890. po.ResizingType = tc.resizingType
  891. po.Enlarge = tc.enlarge
  892. po.Extend.Enabled = tc.extend
  893. po.ExtendAspectRatio.Enabled = tc.extendAR
  894. po.Rotate = tc.rotate
  895. po.Padding = tc.padding
  896. result, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  897. s.Require().NoError(err)
  898. s.Require().NotNil(result)
  899. s.checkSize(result, tc.outWidth, tc.outHeight)
  900. })
  901. }
  902. }
  903. func (s *ProcessingTestSuite) TestImageResolutionTooLarge() {
  904. po := options.NewProcessingOptions()
  905. po.SecurityOptions.MaxSrcResolution = 1
  906. imgdata := s.openFile("test2.jpg")
  907. _, err := ProcessImage(context.Background(), imgdata, po, nil, s.idf)
  908. s.Require().Error(err)
  909. s.Require().Equal(422, ierrors.Wrap(err, 0).StatusCode())
  910. }
  911. func TestProcessing(t *testing.T) {
  912. suite.Run(t, new(ProcessingTestSuite))
  913. }