processing_test.go 24 KB

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