ThumbTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace tests;
  12. use think\Image;
  13. class ThumbTest extends TestCase
  14. {
  15. public function testJpeg()
  16. {
  17. $pathname = TEST_PATH . 'tmp/thumb.jpg';
  18. //1
  19. $image = Image::open($this->getJpeg());
  20. $image->thumb(200, 200, Image::THUMB_CENTER)->save($pathname);
  21. $this->assertEquals(200, $image->width());
  22. $this->assertEquals(200, $image->height());
  23. $file = new \SplFileInfo($pathname);
  24. $this->assertTrue($file->isFile());
  25. @unlink($pathname);
  26. //2
  27. $image = Image::open($this->getJpeg());
  28. $image->thumb(200, 200, Image::THUMB_SCALING)->save($pathname);
  29. $this->assertEquals(200, $image->width());
  30. $this->assertEquals(150, $image->height());
  31. $file = new \SplFileInfo($pathname);
  32. $this->assertTrue($file->isFile());
  33. @unlink($pathname);
  34. //3
  35. $image = Image::open($this->getJpeg());
  36. $image->thumb(200, 200, Image::THUMB_FILLED)->save($pathname);
  37. $this->assertEquals(200, $image->width());
  38. $this->assertEquals(200, $image->height());
  39. $file = new \SplFileInfo($pathname);
  40. $this->assertTrue($file->isFile());
  41. @unlink($pathname);
  42. //4
  43. $image = Image::open($this->getJpeg());
  44. $image->thumb(200, 200, Image::THUMB_NORTHWEST)->save($pathname);
  45. $this->assertEquals(200, $image->width());
  46. $this->assertEquals(200, $image->height());
  47. $file = new \SplFileInfo($pathname);
  48. $this->assertTrue($file->isFile());
  49. @unlink($pathname);
  50. //5
  51. $image = Image::open($this->getJpeg());
  52. $image->thumb(200, 200, Image::THUMB_SOUTHEAST)->save($pathname);
  53. $this->assertEquals(200, $image->width());
  54. $this->assertEquals(200, $image->height());
  55. $file = new \SplFileInfo($pathname);
  56. $this->assertTrue($file->isFile());
  57. @unlink($pathname);
  58. //6
  59. $image = Image::open($this->getJpeg());
  60. $image->thumb(200, 200, Image::THUMB_FIXED)->save($pathname);
  61. $this->assertEquals(200, $image->width());
  62. $this->assertEquals(200, $image->height());
  63. $file = new \SplFileInfo($pathname);
  64. $this->assertTrue($file->isFile());
  65. @unlink($pathname);
  66. }
  67. public function testPng()
  68. {
  69. $pathname = TEST_PATH . 'tmp/thumb.png';
  70. //1
  71. $image = Image::open($this->getPng());
  72. $image->thumb(200, 200, Image::THUMB_CENTER)->save($pathname);
  73. $this->assertEquals(200, $image->width());
  74. $this->assertEquals(200, $image->height());
  75. $file = new \SplFileInfo($pathname);
  76. $this->assertTrue($file->isFile());
  77. @unlink($pathname);
  78. //2
  79. $image = Image::open($this->getPng());
  80. $image->thumb(200, 200, Image::THUMB_SCALING)->save($pathname);
  81. $this->assertEquals(200, $image->width());
  82. $this->assertEquals(150, $image->height());
  83. $file = new \SplFileInfo($pathname);
  84. $this->assertTrue($file->isFile());
  85. @unlink($pathname);
  86. //3
  87. $image = Image::open($this->getPng());
  88. $image->thumb(200, 200, Image::THUMB_FILLED)->save($pathname);
  89. $this->assertEquals(200, $image->width());
  90. $this->assertEquals(200, $image->height());
  91. $file = new \SplFileInfo($pathname);
  92. $this->assertTrue($file->isFile());
  93. @unlink($pathname);
  94. //4
  95. $image = Image::open($this->getPng());
  96. $image->thumb(200, 200, Image::THUMB_NORTHWEST)->save($pathname);
  97. $this->assertEquals(200, $image->width());
  98. $this->assertEquals(200, $image->height());
  99. $file = new \SplFileInfo($pathname);
  100. $this->assertTrue($file->isFile());
  101. @unlink($pathname);
  102. //5
  103. $image = Image::open($this->getPng());
  104. $image->thumb(200, 200, Image::THUMB_SOUTHEAST)->save($pathname);
  105. $this->assertEquals(200, $image->width());
  106. $this->assertEquals(200, $image->height());
  107. $file = new \SplFileInfo($pathname);
  108. $this->assertTrue($file->isFile());
  109. @unlink($pathname);
  110. //6
  111. $image = Image::open($this->getPng());
  112. $image->thumb(200, 200, Image::THUMB_FIXED)->save($pathname);
  113. $this->assertEquals(200, $image->width());
  114. $this->assertEquals(200, $image->height());
  115. $file = new \SplFileInfo($pathname);
  116. $this->assertTrue($file->isFile());
  117. @unlink($pathname);
  118. }
  119. public function testGif()
  120. {
  121. $pathname = TEST_PATH . 'tmp/thumb.gif';
  122. //1
  123. $image = Image::open($this->getGif());
  124. $image->thumb(200, 200, Image::THUMB_CENTER)->save($pathname);
  125. $this->assertEquals(200, $image->width());
  126. $this->assertEquals(200, $image->height());
  127. $file = new \SplFileInfo($pathname);
  128. $this->assertTrue($file->isFile());
  129. @unlink($pathname);
  130. //2
  131. $image = Image::open($this->getGif());
  132. $image->thumb(200, 200, Image::THUMB_SCALING)->save($pathname);
  133. $this->assertEquals(200, $image->width());
  134. $this->assertEquals(113, $image->height());
  135. $file = new \SplFileInfo($pathname);
  136. $this->assertTrue($file->isFile());
  137. @unlink($pathname);
  138. //3
  139. $image = Image::open($this->getGif());
  140. $image->thumb(200, 200, Image::THUMB_FILLED)->save($pathname);
  141. $this->assertEquals(200, $image->width());
  142. $this->assertEquals(200, $image->height());
  143. $file = new \SplFileInfo($pathname);
  144. $this->assertTrue($file->isFile());
  145. @unlink($pathname);
  146. //4
  147. $image = Image::open($this->getGif());
  148. $image->thumb(200, 200, Image::THUMB_NORTHWEST)->save($pathname);
  149. $this->assertEquals(200, $image->width());
  150. $this->assertEquals(200, $image->height());
  151. $file = new \SplFileInfo($pathname);
  152. $this->assertTrue($file->isFile());
  153. @unlink($pathname);
  154. //5
  155. $image = Image::open($this->getGif());
  156. $image->thumb(200, 200, Image::THUMB_SOUTHEAST)->save($pathname);
  157. $this->assertEquals(200, $image->width());
  158. $this->assertEquals(200, $image->height());
  159. $file = new \SplFileInfo($pathname);
  160. $this->assertTrue($file->isFile());
  161. @unlink($pathname);
  162. //6
  163. $image = Image::open($this->getGif());
  164. $image->thumb(200, 200, Image::THUMB_FIXED)->save($pathname);
  165. $this->assertEquals(200, $image->width());
  166. $this->assertEquals(200, $image->height());
  167. $file = new \SplFileInfo($pathname);
  168. $this->assertTrue($file->isFile());
  169. @unlink($pathname);
  170. }
  171. }