processing_test.go 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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/fetcher"
  13. "github.com/imgproxy/imgproxy/v3/ierrors"
  14. "github.com/imgproxy/imgproxy/v3/imagedata"
  15. "github.com/imgproxy/imgproxy/v3/options"
  16. "github.com/imgproxy/imgproxy/v3/vips"
  17. )
  18. type ProcessingTestSuite struct {
  19. suite.Suite
  20. idf *imagedata.Factory
  21. }
  22. func (s *ProcessingTestSuite) SetupSuite() {
  23. config.Reset()
  24. config.MaxSrcResolution = 10 * 1024 * 1024
  25. config.MaxSrcFileSize = 10 * 1024 * 1024
  26. config.MaxAnimationFrames = 100
  27. config.MaxAnimationFrameResolution = 10 * 1024 * 1024
  28. s.Require().NoError(vips.Init())
  29. logrus.SetOutput(io.Discard)
  30. fc := fetcher.NewDefaultConfig()
  31. f, err := fetcher.New(&fc)
  32. s.Require().NoError(err)
  33. s.idf = imagedata.NewFactory(f)
  34. }
  35. func (s *ProcessingTestSuite) openFile(name string) imagedata.ImageData {
  36. wd, err := os.Getwd()
  37. s.Require().NoError(err)
  38. path := filepath.Join(wd, "..", "testdata", name)
  39. imagedata, err := s.idf.NewFromPath(path)
  40. s.Require().NoError(err)
  41. return imagedata
  42. }
  43. func (s *ProcessingTestSuite) checkSize(r *Result, width, height int) {
  44. s.Require().NotNil(r)
  45. s.Require().Equal(width, r.ResultWidth, "Width mismatch")
  46. s.Require().Equal(height, r.ResultHeight, "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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  74. s.Require().NoError(err)
  75. s.Require().NotNil(result)
  76. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  107. s.Require().NoError(err)
  108. s.Require().NotNil(result)
  109. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  145. s.Require().NoError(err)
  146. s.Require().NotNil(result)
  147. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  183. s.Require().NoError(err)
  184. s.Require().NotNil(result)
  185. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  215. s.Require().NoError(err)
  216. s.Require().NotNil(result)
  217. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  248. s.Require().NoError(err)
  249. s.Require().NotNil(result)
  250. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  288. s.Require().NoError(err)
  289. s.Require().NotNil(result)
  290. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  328. s.Require().NoError(err)
  329. s.Require().NotNil(result)
  330. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  360. s.Require().NoError(err)
  361. s.Require().NotNil(result)
  362. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  393. s.Require().NoError(err)
  394. s.Require().NotNil(result)
  395. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  433. s.Require().NoError(err)
  434. s.Require().NotNil(result)
  435. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  471. s.Require().NoError(err)
  472. s.Require().NotNil(result)
  473. s.checkSize(result, 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. result, err := ProcessImage(context.Background(), imgdata, po, nil)
  893. s.Require().NoError(err)
  894. s.Require().NotNil(result)
  895. s.checkSize(result, tc.outWidth, tc.outHeight)
  896. })
  897. }
  898. }
  899. func (s *ProcessingTestSuite) TestImageResolutionTooLarge() {
  900. po := options.NewProcessingOptions()
  901. po.SecurityOptions.MaxSrcResolution = 1
  902. imgdata := s.openFile("test2.jpg")
  903. _, err := ProcessImage(context.Background(), imgdata, po, nil)
  904. s.Require().Error(err)
  905. s.Require().Equal(422, ierrors.Wrap(err, 0).StatusCode())
  906. }
  907. func TestProcessing(t *testing.T) {
  908. suite.Run(t, new(ProcessingTestSuite))
  909. }