1
0

processing_test.go 24 KB

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