ImageTest.php 12 KB

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