processing_test.go 25 KB

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