ImageTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. namespace Test;
  9. use OC;
  10. class ImageTest extends \Test\TestCase {
  11. public static function tearDownAfterClass() {
  12. @unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
  13. @unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  14. parent::tearDownAfterClass();
  15. }
  16. public function testGetMimeTypeForFile() {
  17. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  18. $this->assertEquals('image/png', $mimetype);
  19. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  20. $this->assertEquals('image/jpeg', $mimetype);
  21. $mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
  22. $this->assertEquals('image/gif', $mimetype);
  23. $mimetype = \OC_Image::getMimeTypeForFile(null);
  24. $this->assertEquals('', $mimetype);
  25. }
  26. public function testConstructDestruct() {
  27. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  28. $this->assertInstanceOf('\OC_Image', $img);
  29. $this->assertInstanceOf('\OCP\IImage', $img);
  30. unset($img);
  31. $imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  32. $img = new \OC_Image($imgcreate);
  33. $this->assertInstanceOf('\OC_Image', $img);
  34. $this->assertInstanceOf('\OCP\IImage', $img);
  35. unset($img);
  36. $base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  37. $img = new \OC_Image($base64);
  38. $this->assertInstanceOf('\OC_Image', $img);
  39. $this->assertInstanceOf('\OCP\IImage', $img);
  40. unset($img);
  41. $img = new \OC_Image(null);
  42. $this->assertInstanceOf('\OC_Image', $img);
  43. $this->assertInstanceOf('\OCP\IImage', $img);
  44. unset($img);
  45. }
  46. public function testValid() {
  47. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  48. $this->assertTrue($img->valid());
  49. $text = base64_encode("Lorem ipsum dolor sir amet …");
  50. $img = new \OC_Image($text);
  51. $this->assertFalse($img->valid());
  52. $img = new \OC_Image(null);
  53. $this->assertFalse($img->valid());
  54. }
  55. public function testMimeType() {
  56. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  57. $this->assertEquals('image/png', $img->mimeType());
  58. $img = new \OC_Image(null);
  59. $this->assertEquals('', $img->mimeType());
  60. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  61. $this->assertEquals('image/jpeg', $img->mimeType());
  62. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  63. $this->assertEquals('image/gif', $img->mimeType());
  64. }
  65. public function testWidth() {
  66. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  67. $this->assertEquals(128, $img->width());
  68. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  69. $this->assertEquals(1680, $img->width());
  70. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  71. $this->assertEquals(64, $img->width());
  72. $img = new \OC_Image(null);
  73. $this->assertEquals(-1, $img->width());
  74. }
  75. public function testHeight() {
  76. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  77. $this->assertEquals(128, $img->height());
  78. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  79. $this->assertEquals(1050, $img->height());
  80. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  81. $this->assertEquals(64, $img->height());
  82. $img = new \OC_Image(null);
  83. $this->assertEquals(-1, $img->height());
  84. }
  85. public function testSave() {
  86. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  87. $img->resize(16);
  88. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png');
  89. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data());
  90. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  91. $img->resize(128);
  92. $img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
  93. $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data());
  94. }
  95. public function testData() {
  96. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  97. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
  98. // Preserve transparency
  99. imagealphablending($raw, true);
  100. imagesavealpha($raw, true);
  101. ob_start();
  102. imagepng($raw);
  103. $expected = ob_get_clean();
  104. $this->assertEquals($expected, $img->data());
  105. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  106. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  107. ob_start();
  108. imagejpeg($raw);
  109. $expected = ob_get_clean();
  110. $this->assertEquals($expected, $img->data());
  111. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  112. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  113. ob_start();
  114. imagegif($raw);
  115. $expected = ob_get_clean();
  116. $this->assertEquals($expected, $img->data());
  117. }
  118. public function testDataNoResource() {
  119. $img = new \OC_Image();
  120. $this->assertNull($img->data());
  121. }
  122. /**
  123. * @depends testData
  124. */
  125. public function testToString() {
  126. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  127. $expected = base64_encode($img->data());
  128. $this->assertEquals($expected, (string)$img);
  129. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  130. $expected = base64_encode($img->data());
  131. $this->assertEquals($expected, (string)$img);
  132. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
  133. $expected = base64_encode($img->data());
  134. $this->assertEquals($expected, (string)$img);
  135. }
  136. public function testResize() {
  137. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  138. $this->assertTrue($img->resize(32));
  139. $this->assertEquals(32, $img->width());
  140. $this->assertEquals(32, $img->height());
  141. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  142. $this->assertTrue($img->resize(840));
  143. $this->assertEquals(840, $img->width());
  144. $this->assertEquals(525, $img->height());
  145. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  146. $this->assertTrue($img->resize(100));
  147. $this->assertEquals(100, $img->width());
  148. $this->assertEquals(100, $img->height());
  149. }
  150. public function testPreciseResize() {
  151. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  152. $this->assertTrue($img->preciseResize(128, 512));
  153. $this->assertEquals(128, $img->width());
  154. $this->assertEquals(512, $img->height());
  155. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  156. $this->assertTrue($img->preciseResize(64, 840));
  157. $this->assertEquals(64, $img->width());
  158. $this->assertEquals(840, $img->height());
  159. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  160. $this->assertTrue($img->preciseResize(1000, 1337));
  161. $this->assertEquals(1000, $img->width());
  162. $this->assertEquals(1337, $img->height());
  163. }
  164. public function testCenterCrop() {
  165. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  166. $img->centerCrop();
  167. $this->assertEquals(128, $img->width());
  168. $this->assertEquals(128, $img->height());
  169. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  170. $img->centerCrop();
  171. $this->assertEquals(1050, $img->width());
  172. $this->assertEquals(1050, $img->height());
  173. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  174. $img->centerCrop(512);
  175. $this->assertEquals(512, $img->width());
  176. $this->assertEquals(512, $img->height());
  177. }
  178. public function testCrop() {
  179. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  180. $this->assertTrue($img->crop(0, 0, 50, 20));
  181. $this->assertEquals(50, $img->width());
  182. $this->assertEquals(20, $img->height());
  183. $img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  184. $this->assertTrue($img->crop(500, 700, 550, 300));
  185. $this->assertEquals(550, $img->width());
  186. $this->assertEquals(300, $img->height());
  187. $img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  188. $this->assertTrue($img->crop(10, 10, 15, 15));
  189. $this->assertEquals(15, $img->width());
  190. $this->assertEquals(15, $img->height());
  191. }
  192. public static function sampleProvider() {
  193. return [
  194. ['testimage.png', [200, 100], [100, 100]],
  195. ['testimage.jpg', [840, 840], [840, 525]],
  196. ['testimage.gif', [200, 250], [200, 200]]
  197. ];
  198. }
  199. /**
  200. * @dataProvider sampleProvider
  201. *
  202. * @param string $filename
  203. * @param int[] $asked
  204. * @param int[] $expected
  205. */
  206. public function testFitIn($filename, $asked, $expected) {
  207. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  208. $this->assertTrue($img->fitIn($asked[0], $asked[1]));
  209. $this->assertEquals($expected[0], $img->width());
  210. $this->assertEquals($expected[1], $img->height());
  211. }
  212. public static function sampleFilenamesProvider() {
  213. return [
  214. ['testimage.png'],
  215. ['testimage.jpg'],
  216. ['testimage.gif']
  217. ];
  218. }
  219. /**
  220. * Image should not be resized if it's already smaller than what is required
  221. *
  222. * @dataProvider sampleFilenamesProvider
  223. *
  224. * @param string $filename
  225. */
  226. public function testScaleDownToFitWhenSmallerAlready($filename) {
  227. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  228. $currentWidth = $img->width();
  229. $currentHeight = $img->height();
  230. // We pick something larger than the image we want to scale down
  231. $this->assertFalse($img->scaleDownToFit(4000, 4000));
  232. // The dimensions of the image should not have changed since it's smaller already
  233. $resizedWidth = $img->width();
  234. $resizedHeight = $img->height();
  235. $this->assertEquals(
  236. $currentWidth, $img->width(), "currentWidth $currentWidth resizedWidth $resizedWidth \n"
  237. );
  238. $this->assertEquals(
  239. $currentHeight, $img->height(),
  240. "currentHeight $currentHeight resizedHeight $resizedHeight \n"
  241. );
  242. }
  243. public static function largeSampleProvider() {
  244. return [
  245. ['testimage.png', [200, 100], [100, 100]],
  246. ['testimage.jpg', [840, 840], [840, 525]],
  247. ];
  248. }
  249. /**
  250. * @dataProvider largeSampleProvider
  251. *
  252. * @param string $filename
  253. * @param int[] $asked
  254. * @param int[] $expected
  255. */
  256. public function testScaleDownWhenBigger($filename, $asked, $expected) {
  257. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  258. //$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
  259. $img->scaleDownToFit($asked[0], $asked[1]);
  260. $this->assertEquals($expected[0], $img->width());
  261. $this->assertEquals($expected[1], $img->height());
  262. }
  263. function convertDataProvider() {
  264. return array(
  265. array( 'image/gif'),
  266. array( 'image/jpeg'),
  267. array( 'image/png'),
  268. );
  269. }
  270. /**
  271. * @dataProvider convertDataProvider
  272. */
  273. public function testConvert($mimeType) {
  274. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  275. $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
  276. $img->save($tempFile, $mimeType);
  277. $actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
  278. $this->assertEquals($mimeType, $actualMimeType);
  279. }
  280. }