processing_test.go 25 KB

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