image.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_Image extends PHPUnit_Framework_TestCase {
  9. public static function tearDownAfterClass() {
  10. unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
  11. unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  12. }
  13. public function testGetMimeTypeForFile() {
  14. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  15. $this->assertEquals('image/png', $mimetype);
  16. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  17. $this->assertEquals('image/jpeg', $mimetype);
  18. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
  19. $this->assertEquals('image/gif', $mimetype);
  20. $mimetype = \OC_Image::getMimeTypeForFile(null);
  21. $this->assertEquals('', $mimetype);
  22. }
  23. public function testConstructDestruct() {
  24. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  25. $this->assertInstanceOf('\OC_Image', $img);
  26. unset($img);
  27. $imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  28. $img = new \OC_Image($imgcreate);
  29. $this->assertInstanceOf('\OC_Image', $img);
  30. unset($img);
  31. $base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  32. $img = new \OC_Image($base64);
  33. $this->assertInstanceOf('\OC_Image', $img);
  34. unset($img);
  35. $img = new \OC_Image(null);
  36. $this->assertInstanceOf('\OC_Image', $img);
  37. unset($img);
  38. }
  39. public function testValid() {
  40. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  41. $this->assertTrue($img->valid());
  42. $text = base64_encode("Lorem ipsum dolor sir amet …");
  43. $img = new \OC_Image($text);
  44. $this->assertFalse($img->valid());
  45. $img = new \OC_Image(null);
  46. $this->assertFalse($img->valid());
  47. }
  48. public function testMimeType() {
  49. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  50. $this->assertEquals('image/png', $img->mimeType());
  51. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  52. $this->assertEquals('image/jpeg', $img->mimeType());
  53. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  54. $this->assertEquals('image/gif', $img->mimeType());
  55. $img = new \OC_Image(null);
  56. $this->assertEquals('', $img->mimeType());
  57. }
  58. public function testWidth() {
  59. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  60. $this->assertEquals(128, $img->width());
  61. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  62. $this->assertEquals(1680, $img->width());
  63. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  64. $this->assertEquals(64, $img->width());
  65. $img = new \OC_Image(null);
  66. $this->assertEquals(-1, $img->width());
  67. }
  68. public function testHeight() {
  69. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  70. $this->assertEquals(128, $img->height());
  71. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  72. $this->assertEquals(1050, $img->height());
  73. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  74. $this->assertEquals(64, $img->height());
  75. $img = new \OC_Image(null);
  76. $this->assertEquals(-1, $img->height());
  77. }
  78. public function testSave() {
  79. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  80. $img->resize(16);
  81. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png');
  82. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data());
  83. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  84. $img->resize(128);
  85. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  86. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data());
  87. }
  88. public function testData() {
  89. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  90. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
  91. ob_start();
  92. imagepng($raw);
  93. $expected = ob_get_clean();
  94. $this->assertEquals($expected, $img->data());
  95. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  96. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  97. ob_start();
  98. imagejpeg($raw);
  99. $expected = ob_get_clean();
  100. $this->assertEquals($expected, $img->data());
  101. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  102. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  103. ob_start();
  104. imagegif($raw);
  105. $expected = ob_get_clean();
  106. $this->assertEquals($expected, $img->data());
  107. }
  108. /**
  109. * @depends testData
  110. */
  111. public function testToString() {
  112. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  113. $expected = base64_encode($img->data());
  114. $this->assertEquals($expected, (string)$img);
  115. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  116. $expected = base64_encode($img->data());
  117. $this->assertEquals($expected, (string)$img);
  118. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  119. $expected = base64_encode($img->data());
  120. $this->assertEquals($expected, (string)$img);
  121. }
  122. public function testResize() {
  123. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  124. $this->assertTrue($img->resize(32));
  125. $this->assertEquals(32, $img->width());
  126. $this->assertEquals(32, $img->height());
  127. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  128. $this->assertTrue($img->resize(840));
  129. $this->assertEquals(840, $img->width());
  130. $this->assertEquals(525, $img->height());
  131. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  132. $this->assertTrue($img->resize(100));
  133. $this->assertEquals(100, $img->width());
  134. $this->assertEquals(100, $img->height());
  135. }
  136. public function testPreciseResize() {
  137. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  138. $this->assertTrue($img->preciseResize(128, 512));
  139. $this->assertEquals(128, $img->width());
  140. $this->assertEquals(512, $img->height());
  141. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  142. $this->assertTrue($img->preciseResize(64, 840));
  143. $this->assertEquals(64, $img->width());
  144. $this->assertEquals(840, $img->height());
  145. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  146. $this->assertTrue($img->preciseResize(1000, 1337));
  147. $this->assertEquals(1000, $img->width());
  148. $this->assertEquals(1337, $img->height());
  149. }
  150. public function testCenterCrop() {
  151. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  152. $img->centerCrop();
  153. $this->assertEquals(128, $img->width());
  154. $this->assertEquals(128, $img->height());
  155. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  156. $img->centerCrop();
  157. $this->assertEquals(1050, $img->width());
  158. $this->assertEquals(1050, $img->height());
  159. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  160. $img->centerCrop(512);
  161. $this->assertEquals(512, $img->width());
  162. $this->assertEquals(512, $img->height());
  163. }
  164. public function testCrop() {
  165. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  166. $this->assertTrue($img->crop(0, 0, 50, 20));
  167. $this->assertEquals(50, $img->width());
  168. $this->assertEquals(20, $img->height());
  169. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  170. $this->assertTrue($img->crop(500, 700, 550, 300));
  171. $this->assertEquals(550, $img->width());
  172. $this->assertEquals(300, $img->height());
  173. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  174. $this->assertTrue($img->crop(10, 10, 15, 15));
  175. $this->assertEquals(15, $img->width());
  176. $this->assertEquals(15, $img->height());
  177. }
  178. public function testFitIn() {
  179. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  180. $this->assertTrue($img->fitIn(200, 100));
  181. $this->assertEquals(100, $img->width());
  182. $this->assertEquals(100, $img->height());
  183. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  184. $this->assertTrue($img->fitIn(840, 840));
  185. $this->assertEquals(840, $img->width());
  186. $this->assertEquals(525, $img->height());
  187. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  188. $this->assertTrue($img->fitIn(200, 250));
  189. $this->assertEquals(200, $img->width());
  190. $this->assertEquals(200, $img->height());
  191. }
  192. }