1
0

processing_test.go 25 KB

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