ImageTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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(): void {
  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', '80')
  117. ->willReturn(null);
  118. $config->expects($this->once())
  119. ->method('getSystemValueInt')
  120. ->with('preview_max_memory', 256)
  121. ->willReturn(256);
  122. $img = new \OC_Image(null, null, $config);
  123. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
  124. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  125. imageinterlace($raw, true);
  126. ob_start();
  127. imagejpeg($raw, null, 80);
  128. $expected = ob_get_clean();
  129. $this->assertEquals($expected, $img->data());
  130. $img = new \OC_Image();
  131. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
  132. $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
  133. ob_start();
  134. imagegif($raw);
  135. $expected = ob_get_clean();
  136. $this->assertEquals($expected, $img->data());
  137. }
  138. public function testDataNoResource() {
  139. $img = new \OC_Image();
  140. $this->assertNull($img->data());
  141. }
  142. /**
  143. * @depends testData
  144. */
  145. public function testToString() {
  146. $img = new \OC_Image();
  147. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  148. $expected = base64_encode($img->data());
  149. $this->assertEquals($expected, (string)$img);
  150. $img = new \OC_Image();
  151. $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  152. $expected = base64_encode($img->data());
  153. $this->assertEquals($expected, (string)$img);
  154. $img = new \OC_Image();
  155. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
  156. $expected = base64_encode($img->data());
  157. $this->assertEquals($expected, (string)$img);
  158. }
  159. public function testResize() {
  160. $img = new \OC_Image();
  161. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  162. $this->assertTrue($img->resize(32));
  163. $this->assertEquals(32, $img->width());
  164. $this->assertEquals(32, $img->height());
  165. $img = new \OC_Image();
  166. $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  167. $this->assertTrue($img->resize(840));
  168. $this->assertEquals(840, $img->width());
  169. $this->assertEquals(525, $img->height());
  170. $img = new \OC_Image();
  171. $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  172. $this->assertTrue($img->resize(100));
  173. $this->assertEquals(100, $img->width());
  174. $this->assertEquals(100, $img->height());
  175. }
  176. public function testPreciseResize() {
  177. $img = new \OC_Image();
  178. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  179. $this->assertTrue($img->preciseResize(128, 512));
  180. $this->assertEquals(128, $img->width());
  181. $this->assertEquals(512, $img->height());
  182. $img = new \OC_Image();
  183. $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  184. $this->assertTrue($img->preciseResize(64, 840));
  185. $this->assertEquals(64, $img->width());
  186. $this->assertEquals(840, $img->height());
  187. $img = new \OC_Image();
  188. $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  189. $this->assertTrue($img->preciseResize(1000, 1337));
  190. $this->assertEquals(1000, $img->width());
  191. $this->assertEquals(1337, $img->height());
  192. }
  193. public function testCenterCrop() {
  194. $img = new \OC_Image();
  195. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  196. $img->centerCrop();
  197. $this->assertEquals(128, $img->width());
  198. $this->assertEquals(128, $img->height());
  199. $img = new \OC_Image();
  200. $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  201. $img->centerCrop();
  202. $this->assertEquals(1050, $img->width());
  203. $this->assertEquals(1050, $img->height());
  204. $img = new \OC_Image();
  205. $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  206. $img->centerCrop(512);
  207. $this->assertEquals(512, $img->width());
  208. $this->assertEquals(512, $img->height());
  209. }
  210. public function testCrop() {
  211. $img = new \OC_Image();
  212. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  213. $this->assertTrue($img->crop(0, 0, 50, 20));
  214. $this->assertEquals(50, $img->width());
  215. $this->assertEquals(20, $img->height());
  216. $img = new \OC_Image();
  217. $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  218. $this->assertTrue($img->crop(500, 700, 550, 300));
  219. $this->assertEquals(550, $img->width());
  220. $this->assertEquals(300, $img->height());
  221. $img = new \OC_Image();
  222. $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
  223. $this->assertTrue($img->crop(10, 10, 15, 15));
  224. $this->assertEquals(15, $img->width());
  225. $this->assertEquals(15, $img->height());
  226. }
  227. public static function sampleProvider() {
  228. return [
  229. ['testimage.png', [200, 100], [100, 100]],
  230. ['testimage.jpg', [840, 840], [840, 525]],
  231. ['testimage.gif', [200, 250], [200, 200]]
  232. ];
  233. }
  234. /**
  235. * @dataProvider sampleProvider
  236. *
  237. * @param string $filename
  238. * @param int[] $asked
  239. * @param int[] $expected
  240. */
  241. public function testFitIn($filename, $asked, $expected) {
  242. $img = new \OC_Image();
  243. $img->loadFromFile(OC::$SERVERROOT . '/tests/data/' . $filename);
  244. $this->assertTrue($img->fitIn($asked[0], $asked[1]));
  245. $this->assertEquals($expected[0], $img->width());
  246. $this->assertEquals($expected[1], $img->height());
  247. }
  248. public static function sampleFilenamesProvider() {
  249. return [
  250. ['testimage.png'],
  251. ['testimage.jpg'],
  252. ['testimage.gif']
  253. ];
  254. }
  255. /**
  256. * Image should not be resized if it's already smaller than what is required
  257. *
  258. * @dataProvider sampleFilenamesProvider
  259. *
  260. * @param string $filename
  261. */
  262. public function testScaleDownToFitWhenSmallerAlready($filename) {
  263. $img = new \OC_Image();
  264. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
  265. $currentWidth = $img->width();
  266. $currentHeight = $img->height();
  267. // We pick something larger than the image we want to scale down
  268. $this->assertFalse($img->scaleDownToFit(4000, 4000));
  269. // The dimensions of the image should not have changed since it's smaller already
  270. $resizedWidth = $img->width();
  271. $resizedHeight = $img->height();
  272. $this->assertEquals(
  273. $currentWidth, $img->width(), "currentWidth $currentWidth resizedWidth $resizedWidth \n"
  274. );
  275. $this->assertEquals(
  276. $currentHeight, $img->height(),
  277. "currentHeight $currentHeight resizedHeight $resizedHeight \n"
  278. );
  279. }
  280. public static function largeSampleProvider() {
  281. return [
  282. ['testimage.png', [200, 100], [100, 100]],
  283. ['testimage.jpg', [840, 840], [840, 525]],
  284. ];
  285. }
  286. /**
  287. * @dataProvider largeSampleProvider
  288. *
  289. * @param string $filename
  290. * @param int[] $asked
  291. * @param int[] $expected
  292. */
  293. public function testScaleDownWhenBigger($filename, $asked, $expected) {
  294. $img = new \OC_Image();
  295. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
  296. //$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
  297. $img->scaleDownToFit($asked[0], $asked[1]);
  298. $this->assertEquals($expected[0], $img->width());
  299. $this->assertEquals($expected[1], $img->height());
  300. }
  301. public function convertDataProvider() {
  302. return [
  303. [ 'image/gif'],
  304. [ 'image/jpeg'],
  305. [ 'image/png'],
  306. ];
  307. }
  308. /**
  309. * @dataProvider convertDataProvider
  310. */
  311. public function testConvert($mimeType) {
  312. $img = new \OC_Image();
  313. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
  314. $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
  315. $img->save($tempFile, $mimeType);
  316. $this->assertEquals($mimeType, image_type_to_mime_type(exif_imagetype($tempFile)));
  317. }
  318. public function testMemoryLimitFromFile() {
  319. $img = new \OC_Image();
  320. $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage-badheader.jpg');
  321. $this->assertFalse($img->valid());
  322. }
  323. public function testMemoryLimitFromData() {
  324. $data = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage-badheader.jpg');
  325. $img = new \OC_Image();
  326. $img->loadFromData($data);
  327. $this->assertFalse($img->valid());
  328. }
  329. }