ImageTest.php 11 KB

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