processing_test.go 25 KB

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