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.

341 lines
11 KiB

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