ImageTest.php 13 KB

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