processing_test.go 26 KB

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