You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

image.php 11KB

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. 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 static function sampleProvider() {
  194. return [
  195. ['testimage.png', [200, 100], [100, 100]],
  196. ['testimage.jpg', [840, 840], [840, 525]],
  197. ['testimage.gif', [200, 250], [200, 200]]
  198. ];
  199. }
  200. /**
  201. * @dataProvider sampleProvider
  202. *
  203. * @param string $filename
  204. * @param int[] $asked
  205. * @param int[] $expected
  206. */
  207. public function testFitIn($filename, $asked, $expected) {
  208. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  209. $this->assertTrue($img->fitIn($asked[0], $asked[1]));
  210. $this->assertEquals($expected[0], $img->width());
  211. $this->assertEquals($expected[1], $img->height());
  212. }
  213. public static function sampleFilenamesProvider() {
  214. return [
  215. ['testimage.png'],
  216. ['testimage.jpg'],
  217. ['testimage.gif']
  218. ];
  219. }
  220. /**
  221. * Image should not be resized if it's already smaller than what is required
  222. *
  223. * @dataProvider sampleFilenamesProvider
  224. *
  225. * @param string $filename
  226. */
  227. public function testScaleDownToFitWhenSmallerAlready($filename) {
  228. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  229. $currentWidth = $img->width();
  230. $currentHeight = $img->height();
  231. // We pick something larger than the image we want to scale down
  232. $this->assertFalse($img->scaleDownToFit(4000, 4000));
  233. // The dimensions of the image should not have changed since it's smaller already
  234. $resizedWidth = $img->width();
  235. $resizedHeight = $img->height();
  236. $this->assertEquals(
  237. $currentWidth, $img->width(), "currentWidth $currentWidth resizedWidth $resizedWidth \n"
  238. );
  239. $this->assertEquals(
  240. $currentHeight, $img->height(),
  241. "currentHeight $currentHeight resizedHeight $resizedHeight \n"
  242. );
  243. }
  244. public static function largeSampleProvider() {
  245. return [
  246. ['testimage.png', [200, 100], [100, 100]],
  247. ['testimage.jpg', [840, 840], [840, 525]],
  248. ];
  249. }
  250. /**
  251. * @dataProvider largeSampleProvider
  252. *
  253. * @param string $filename
  254. * @param int[] $asked
  255. * @param int[] $expected
  256. */
  257. public function testScaleDownWhenBigger($filename, $asked, $expected) {
  258. $img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
  259. //$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
  260. $img->scaleDownToFit($asked[0], $asked[1]);
  261. $this->assertEquals($expected[0], $img->width());
  262. $this->assertEquals($expected[1], $img->height());
  263. }
  264. function convertDataProvider() {
  265. return array(
  266. array( 'image/gif'),
  267. array( 'image/jpeg'),
  268. array( 'image/png'),
  269. );
  270. }
  271. /**
  272. * @dataProvider convertDataProvider
  273. */
  274. public function testConvert($mimeType) {
  275. $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
  276. $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
  277. $img->save($tempFile, $mimeType);
  278. $actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
  279. $this->assertEquals($mimeType, $actualMimeType);
  280. }
  281. }