image.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 \Test\TestCase {
  9. public static function tearDownAfterClass() {
  10. @unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
  11. @unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  12. parent::tearDownAfterClass();
  13. }
  14. public function testGetMimeTypeForFile() {
  15. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  16. $this->assertEquals('image/png', $mimetype);
  17. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  18. $this->assertEquals('image/jpeg', $mimetype);
  19. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
  20. $this->assertEquals('image/gif', $mimetype);
  21. $mimetype = \OC_Image::getMimeTypeForFile(null);
  22. $this->assertEquals('', $mimetype);
  23. }
  24. public function testConstructDestruct() {
  25. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  26. $this->assertInstanceOf('\OC_Image', $img);
  27. $this->assertInstanceOf('\OCP\IImage', $img);
  28. unset($img);
  29. $imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  30. $img = new \OC_Image($imgcreate);
  31. $this->assertInstanceOf('\OC_Image', $img);
  32. $this->assertInstanceOf('\OCP\IImage', $img);
  33. unset($img);
  34. $base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  35. $img = new \OC_Image($base64);
  36. $this->assertInstanceOf('\OC_Image', $img);
  37. $this->assertInstanceOf('\OCP\IImage', $img);
  38. unset($img);
  39. $img = new \OC_Image(null);
  40. $this->assertInstanceOf('\OC_Image', $img);
  41. $this->assertInstanceOf('\OCP\IImage', $img);
  42. unset($img);
  43. }
  44. public function testValid() {
  45. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  46. $this->assertTrue($img->valid());
  47. $text = base64_encode("Lorem ipsum dolor sir amet …");
  48. $img = new \OC_Image($text);
  49. $this->assertFalse($img->valid());
  50. $img = new \OC_Image(null);
  51. $this->assertFalse($img->valid());
  52. }
  53. public function testMimeType() {
  54. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  55. $this->assertEquals('image/png', $img->mimeType());
  56. $img = new \OC_Image(null);
  57. $this->assertEquals('', $img->mimeType());
  58. if (\OC_Util::runningOnWindows()) {
  59. $this->markTestSkipped('[Windows] Images created with imagecreate() are pngs on windows');
  60. }
  61. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  62. $this->assertEquals('image/jpeg', $img->mimeType());
  63. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  64. $this->assertEquals('image/gif', $img->mimeType());
  65. }
  66. public function testWidth() {
  67. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  68. $this->assertEquals(128, $img->width());
  69. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  70. $this->assertEquals(1680, $img->width());
  71. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  72. $this->assertEquals(64, $img->width());
  73. $img = new \OC_Image(null);
  74. $this->assertEquals(-1, $img->width());
  75. }
  76. public function testHeight() {
  77. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  78. $this->assertEquals(128, $img->height());
  79. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  80. $this->assertEquals(1050, $img->height());
  81. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  82. $this->assertEquals(64, $img->height());
  83. $img = new \OC_Image(null);
  84. $this->assertEquals(-1, $img->height());
  85. }
  86. public function testSave() {
  87. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  88. $img->resize(16);
  89. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png');
  90. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data());
  91. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  92. $img->resize(128);
  93. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  94. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data());
  95. }
  96. public function testData() {
  97. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  98. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
  99. // Preserve transparency
  100. imagealphablending($raw, true);
  101. imagesavealpha($raw, true);
  102. ob_start();
  103. imagepng($raw);
  104. $expected = ob_get_clean();
  105. $this->assertEquals($expected, $img->data());
  106. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  107. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  108. ob_start();
  109. imagejpeg($raw);
  110. $expected = ob_get_clean();
  111. $this->assertEquals($expected, $img->data());
  112. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  113. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  114. ob_start();
  115. imagegif($raw);
  116. $expected = ob_get_clean();
  117. $this->assertEquals($expected, $img->data());
  118. }
  119. public function testDataNoResource() {
  120. $img = new \OC_Image();
  121. $this->assertNull($img->data());
  122. }
  123. /**
  124. * @depends testData
  125. */
  126. public function testToString() {
  127. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  128. $expected = base64_encode($img->data());
  129. $this->assertEquals($expected, (string)$img);
  130. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  131. $expected = base64_encode($img->data());
  132. $this->assertEquals($expected, (string)$img);
  133. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  134. $expected = base64_encode($img->data());
  135. $this->assertEquals($expected, (string)$img);
  136. }
  137. public function testResize() {
  138. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  139. $this->assertTrue($img->resize(32));
  140. $this->assertEquals(32, $img->width());
  141. $this->assertEquals(32, $img->height());
  142. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  143. $this->assertTrue($img->resize(840));
  144. $this->assertEquals(840, $img->width());
  145. $this->assertEquals(525, $img->height());
  146. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  147. $this->assertTrue($img->resize(100));
  148. $this->assertEquals(100, $img->width());
  149. $this->assertEquals(100, $img->height());
  150. }
  151. public function testPreciseResize() {
  152. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  153. $this->assertTrue($img->preciseResize(128, 512));
  154. $this->assertEquals(128, $img->width());
  155. $this->assertEquals(512, $img->height());
  156. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  157. $this->assertTrue($img->preciseResize(64, 840));
  158. $this->assertEquals(64, $img->width());
  159. $this->assertEquals(840, $img->height());
  160. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  161. $this->assertTrue($img->preciseResize(1000, 1337));
  162. $this->assertEquals(1000, $img->width());
  163. $this->assertEquals(1337, $img->height());
  164. }
  165. public function testCenterCrop() {
  166. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  167. $img->centerCrop();
  168. $this->assertEquals(128, $img->width());
  169. $this->assertEquals(128, $img->height());
  170. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  171. $img->centerCrop();
  172. $this->assertEquals(1050, $img->width());
  173. $this->assertEquals(1050, $img->height());
  174. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  175. $img->centerCrop(512);
  176. $this->assertEquals(512, $img->width());
  177. $this->assertEquals(512, $img->height());
  178. }
  179. public function testCrop() {
  180. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  181. $this->assertTrue($img->crop(0, 0, 50, 20));
  182. $this->assertEquals(50, $img->width());
  183. $this->assertEquals(20, $img->height());
  184. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  185. $this->assertTrue($img->crop(500, 700, 550, 300));
  186. $this->assertEquals(550, $img->width());
  187. $this->assertEquals(300, $img->height());
  188. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  189. $this->assertTrue($img->crop(10, 10, 15, 15));
  190. $this->assertEquals(15, $img->width());
  191. $this->assertEquals(15, $img->height());
  192. }
  193. public function testFitIn() {
  194. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  195. $this->assertTrue($img->fitIn(200, 100));
  196. $this->assertEquals(100, $img->width());
  197. $this->assertEquals(100, $img->height());
  198. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  199. $this->assertTrue($img->fitIn(840, 840));
  200. $this->assertEquals(840, $img->width());
  201. $this->assertEquals(525, $img->height());
  202. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  203. $this->assertTrue($img->fitIn(200, 250));
  204. $this->assertEquals(200, $img->width());
  205. $this->assertEquals(200, $img->height());
  206. }
  207. function convertDataProvider() {
  208. return array(
  209. array( 'image/gif'),
  210. array( 'image/jpeg'),
  211. array( 'image/png'),
  212. );
  213. }
  214. /**
  215. * @dataProvider convertDataProvider
  216. */
  217. public function testConvert($mimeType) {
  218. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  219. $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
  220. $img->save($tempFile, $mimeType);
  221. $actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
  222. $this->assertEquals($mimeType, $actualMimeType);
  223. }
  224. }