processing_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. package processing
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "testing"
  9. "github.com/sirupsen/logrus"
  10. "github.com/stretchr/testify/suite"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. "github.com/imgproxy/imgproxy/v3/imagedata"
  13. "github.com/imgproxy/imgproxy/v3/options"
  14. "github.com/imgproxy/imgproxy/v3/security"
  15. "github.com/imgproxy/imgproxy/v3/vips"
  16. )
  17. type ProcessingTestSuite struct {
  18. suite.Suite
  19. }
  20. func (s *ProcessingTestSuite) SetupSuite() {
  21. config.Reset()
  22. s.Require().NoError(imagedata.Init())
  23. s.Require().NoError(vips.Init())
  24. logrus.SetOutput(io.Discard)
  25. }
  26. func (s *ProcessingTestSuite) openFile(name string) *imagedata.ImageData {
  27. secopts := security.Options{
  28. MaxSrcResolution: 10 * 1024 * 1024,
  29. MaxSrcFileSize: 10 * 1024 * 1024,
  30. MaxAnimationFrames: 100,
  31. MaxAnimationFrameResolution: 10 * 1024 * 1024,
  32. }
  33. wd, err := os.Getwd()
  34. s.Require().NoError(err)
  35. path := filepath.Join(wd, "..", "testdata", name)
  36. imagedata, err := imagedata.FromFile(path, "test image", secopts)
  37. s.Require().NoError(err)
  38. return imagedata
  39. }
  40. func (s *ProcessingTestSuite) checkSize(imgdata *imagedata.ImageData, width, height int) {
  41. img := new(vips.Image)
  42. err := img.Load(imgdata, 1, 1, 1)
  43. s.Require().NoError(err)
  44. defer img.Clear()
  45. s.Require().Equal(width, img.Width(), "Width mismatch")
  46. s.Require().Equal(height, img.Height(), "Height mismatch")
  47. }
  48. func (s *ProcessingTestSuite) TestResizeToFit() {
  49. imgdata := s.openFile("test2.jpg")
  50. po := options.NewProcessingOptions()
  51. po.ResizingType = options.ResizeFit
  52. testCases := []struct {
  53. width int
  54. height int
  55. outWidth int
  56. outHeight int
  57. }{
  58. {width: 50, height: 50, outWidth: 50, outHeight: 25},
  59. {width: 50, height: 20, outWidth: 40, outHeight: 20},
  60. {width: 20, height: 50, outWidth: 20, outHeight: 10},
  61. {width: 300, height: 300, outWidth: 200, outHeight: 100},
  62. {width: 300, height: 50, outWidth: 100, outHeight: 50},
  63. {width: 100, height: 300, outWidth: 100, outHeight: 50},
  64. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  65. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  66. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  67. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  68. }
  69. for _, tc := range testCases {
  70. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  71. po.Width = tc.width
  72. po.Height = tc.height
  73. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  74. s.Require().NoError(err)
  75. s.Require().NotNil(outImgdata)
  76. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  77. })
  78. }
  79. }
  80. func (s *ProcessingTestSuite) TestResizeToFitEnlarge() {
  81. imgdata := s.openFile("test2.jpg")
  82. po := options.NewProcessingOptions()
  83. po.ResizingType = options.ResizeFit
  84. po.Enlarge = true
  85. testCases := []struct {
  86. width int
  87. height int
  88. outWidth int
  89. outHeight int
  90. }{
  91. {width: 50, height: 50, outWidth: 50, outHeight: 25},
  92. {width: 50, height: 20, outWidth: 40, outHeight: 20},
  93. {width: 20, height: 50, outWidth: 20, outHeight: 10},
  94. {width: 300, height: 300, outWidth: 300, outHeight: 150},
  95. {width: 300, height: 125, outWidth: 250, outHeight: 125},
  96. {width: 250, height: 300, outWidth: 250, outHeight: 125},
  97. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  98. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  99. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  100. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  101. }
  102. for _, tc := range testCases {
  103. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  104. po.Width = tc.width
  105. po.Height = tc.height
  106. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  107. s.Require().NoError(err)
  108. s.Require().NotNil(outImgdata)
  109. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  110. })
  111. }
  112. }
  113. func (s *ProcessingTestSuite) TestResizeToFitExtend() {
  114. imgdata := s.openFile("test2.jpg")
  115. po := options.NewProcessingOptions()
  116. po.ResizingType = options.ResizeFit
  117. po.Extend = options.ExtendOptions{
  118. Enabled: true,
  119. Gravity: options.GravityOptions{
  120. Type: options.GravityCenter,
  121. },
  122. }
  123. testCases := []struct {
  124. width int
  125. height int
  126. outWidth int
  127. outHeight int
  128. }{
  129. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  130. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  131. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  132. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  133. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  134. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  135. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  136. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  137. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  138. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  139. }
  140. for _, tc := range testCases {
  141. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  142. po.Width = tc.width
  143. po.Height = tc.height
  144. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  145. s.Require().NoError(err)
  146. s.Require().NotNil(outImgdata)
  147. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  148. })
  149. }
  150. }
  151. func (s *ProcessingTestSuite) TestResizeToFitExtendAR() {
  152. imgdata := s.openFile("test2.jpg")
  153. po := options.NewProcessingOptions()
  154. po.ResizingType = options.ResizeFit
  155. po.ExtendAspectRatio = options.ExtendOptions{
  156. Enabled: true,
  157. Gravity: options.GravityOptions{
  158. Type: options.GravityCenter,
  159. },
  160. }
  161. testCases := []struct {
  162. width int
  163. height int
  164. outWidth int
  165. outHeight int
  166. }{
  167. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  168. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  169. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  170. {width: 300, height: 300, outWidth: 200, outHeight: 200},
  171. {width: 300, height: 125, outWidth: 240, outHeight: 100},
  172. {width: 250, height: 500, outWidth: 200, outHeight: 400},
  173. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  174. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  175. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  176. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  177. }
  178. for _, tc := range testCases {
  179. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  180. po.Width = tc.width
  181. po.Height = tc.height
  182. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  183. s.Require().NoError(err)
  184. s.Require().NotNil(outImgdata)
  185. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  186. })
  187. }
  188. }
  189. func (s *ProcessingTestSuite) TestResizeToFill() {
  190. imgdata := s.openFile("test2.jpg")
  191. po := options.NewProcessingOptions()
  192. po.ResizingType = options.ResizeFill
  193. testCases := []struct {
  194. width int
  195. height int
  196. outWidth int
  197. outHeight int
  198. }{
  199. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  200. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  201. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  202. {width: 300, height: 300, outWidth: 200, outHeight: 100},
  203. {width: 300, height: 50, outWidth: 200, outHeight: 50},
  204. {width: 100, height: 300, outWidth: 100, outHeight: 100},
  205. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  206. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  207. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  208. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  209. }
  210. for _, tc := range testCases {
  211. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  212. po.Width = tc.width
  213. po.Height = tc.height
  214. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  215. s.Require().NoError(err)
  216. s.Require().NotNil(outImgdata)
  217. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  218. })
  219. }
  220. }
  221. func (s *ProcessingTestSuite) TestResizeToFillEnlarge() {
  222. imgdata := s.openFile("test2.jpg")
  223. po := options.NewProcessingOptions()
  224. po.ResizingType = options.ResizeFill
  225. po.Enlarge = true
  226. testCases := []struct {
  227. width int
  228. height int
  229. outWidth int
  230. outHeight int
  231. }{
  232. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  233. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  234. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  235. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  236. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  237. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  238. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  239. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  240. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  241. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  242. }
  243. for _, tc := range testCases {
  244. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  245. po.Width = tc.width
  246. po.Height = tc.height
  247. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  248. s.Require().NoError(err)
  249. s.Require().NotNil(outImgdata)
  250. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  251. })
  252. }
  253. }
  254. func (s *ProcessingTestSuite) TestResizeToFillExtend() {
  255. imgdata := s.openFile("test2.jpg")
  256. po := options.NewProcessingOptions()
  257. po.ResizingType = options.ResizeFill
  258. po.Extend = options.ExtendOptions{
  259. Enabled: true,
  260. Gravity: options.GravityOptions{
  261. Type: options.GravityCenter,
  262. },
  263. }
  264. testCases := []struct {
  265. width int
  266. height int
  267. outWidth int
  268. outHeight int
  269. }{
  270. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  271. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  272. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  273. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  274. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  275. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  276. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  277. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  278. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  279. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  280. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  281. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  282. }
  283. for _, tc := range testCases {
  284. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  285. po.Width = tc.width
  286. po.Height = tc.height
  287. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  288. s.Require().NoError(err)
  289. s.Require().NotNil(outImgdata)
  290. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  291. })
  292. }
  293. }
  294. func (s *ProcessingTestSuite) TestResizeToFillExtendAR() {
  295. imgdata := s.openFile("test2.jpg")
  296. po := options.NewProcessingOptions()
  297. po.ResizingType = options.ResizeFill
  298. po.ExtendAspectRatio = options.ExtendOptions{
  299. Enabled: true,
  300. Gravity: options.GravityOptions{
  301. Type: options.GravityCenter,
  302. },
  303. }
  304. testCases := []struct {
  305. width int
  306. height int
  307. outWidth int
  308. outHeight int
  309. }{
  310. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  311. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  312. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  313. {width: 300, height: 300, outWidth: 200, outHeight: 200},
  314. {width: 300, height: 125, outWidth: 240, outHeight: 100},
  315. {width: 250, height: 500, outWidth: 200, outHeight: 400},
  316. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  317. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  318. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  319. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  320. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  321. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  322. }
  323. for _, tc := range testCases {
  324. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  325. po.Width = tc.width
  326. po.Height = tc.height
  327. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  328. s.Require().NoError(err)
  329. s.Require().NotNil(outImgdata)
  330. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  331. })
  332. }
  333. }
  334. func (s *ProcessingTestSuite) TestResizeToFillDown() {
  335. imgdata := s.openFile("test2.jpg")
  336. po := options.NewProcessingOptions()
  337. po.ResizingType = options.ResizeFillDown
  338. testCases := []struct {
  339. width int
  340. height int
  341. outWidth int
  342. outHeight int
  343. }{
  344. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  345. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  346. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  347. {width: 300, height: 300, outWidth: 100, outHeight: 100},
  348. {width: 300, height: 125, outWidth: 200, outHeight: 83},
  349. {width: 250, height: 300, outWidth: 83, outHeight: 100},
  350. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  351. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  352. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  353. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  354. }
  355. for _, tc := range testCases {
  356. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  357. po.Width = tc.width
  358. po.Height = tc.height
  359. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  360. s.Require().NoError(err)
  361. s.Require().NotNil(outImgdata)
  362. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  363. })
  364. }
  365. }
  366. func (s *ProcessingTestSuite) TestResizeToFillDownEnlarge() {
  367. imgdata := s.openFile("test2.jpg")
  368. po := options.NewProcessingOptions()
  369. po.ResizingType = options.ResizeFillDown
  370. po.Enlarge = true
  371. testCases := []struct {
  372. width int
  373. height int
  374. outWidth int
  375. outHeight int
  376. }{
  377. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  378. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  379. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  380. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  381. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  382. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  383. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  384. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  385. {width: 0, height: 200, outWidth: 400, outHeight: 200},
  386. {width: 300, height: 0, outWidth: 300, outHeight: 150},
  387. }
  388. for _, tc := range testCases {
  389. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  390. po.Width = tc.width
  391. po.Height = tc.height
  392. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  393. s.Require().NoError(err)
  394. s.Require().NotNil(outImgdata)
  395. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  396. })
  397. }
  398. }
  399. func (s *ProcessingTestSuite) TestResizeToFillDownExtend() {
  400. imgdata := s.openFile("test2.jpg")
  401. po := options.NewProcessingOptions()
  402. po.ResizingType = options.ResizeFillDown
  403. po.Extend = options.ExtendOptions{
  404. Enabled: true,
  405. Gravity: options.GravityOptions{
  406. Type: options.GravityCenter,
  407. },
  408. }
  409. testCases := []struct {
  410. width int
  411. height int
  412. outWidth int
  413. outHeight int
  414. }{
  415. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  416. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  417. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  418. {width: 300, height: 300, outWidth: 300, outHeight: 300},
  419. {width: 300, height: 125, outWidth: 300, outHeight: 125},
  420. {width: 250, height: 300, outWidth: 250, outHeight: 300},
  421. {width: 300, height: 50, outWidth: 300, outHeight: 50},
  422. {width: 100, height: 300, outWidth: 100, outHeight: 300},
  423. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  424. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  425. {width: 0, height: 200, outWidth: 200, outHeight: 200},
  426. {width: 300, height: 0, outWidth: 300, outHeight: 100},
  427. }
  428. for _, tc := range testCases {
  429. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  430. po.Width = tc.width
  431. po.Height = tc.height
  432. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  433. s.Require().NoError(err)
  434. s.Require().NotNil(outImgdata)
  435. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  436. })
  437. }
  438. }
  439. func (s *ProcessingTestSuite) TestResizeToFillDownExtendAR() {
  440. imgdata := s.openFile("test2.jpg")
  441. po := options.NewProcessingOptions()
  442. po.ResizingType = options.ResizeFillDown
  443. po.ExtendAspectRatio = options.ExtendOptions{
  444. Enabled: true,
  445. Gravity: options.GravityOptions{
  446. Type: options.GravityCenter,
  447. },
  448. }
  449. testCases := []struct {
  450. width int
  451. height int
  452. outWidth int
  453. outHeight int
  454. }{
  455. {width: 50, height: 50, outWidth: 50, outHeight: 50},
  456. {width: 50, height: 20, outWidth: 50, outHeight: 20},
  457. {width: 20, height: 50, outWidth: 20, outHeight: 50},
  458. {width: 300, height: 300, outWidth: 100, outHeight: 100},
  459. {width: 300, height: 125, outWidth: 200, outHeight: 83},
  460. {width: 250, height: 300, outWidth: 83, outHeight: 100},
  461. {width: 0, height: 50, outWidth: 100, outHeight: 50},
  462. {width: 50, height: 0, outWidth: 50, outHeight: 25},
  463. {width: 0, height: 200, outWidth: 200, outHeight: 100},
  464. {width: 300, height: 0, outWidth: 200, outHeight: 100},
  465. }
  466. for _, tc := range testCases {
  467. s.Run(fmt.Sprintf("%dx%d", tc.width, tc.height), func() {
  468. po.Width = tc.width
  469. po.Height = tc.height
  470. outImgdata, err := ProcessImage(context.Background(), imgdata, po)
  471. s.Require().NoError(err)
  472. s.Require().NotNil(outImgdata)
  473. s.checkSize(outImgdata, tc.outWidth, tc.outHeight)
  474. })
  475. }
  476. }
  477. func TestProcessing(t *testing.T) {
  478. suite.Run(t, new(ProcessingTestSuite))
  479. }