123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- namespace Test;
- use OC;
- use OCP\IConfig;
- class ImageTest extends \Test\TestCase {
- public static function tearDownAfterClass(): void {
- @unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
- @unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
- parent::tearDownAfterClass();
- }
- public function testConstructDestruct() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertInstanceOf('\OC_Image', $img);
- $this->assertInstanceOf('\OCP\IImage', $img);
- unset($img);
- $imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
- $img = new \OC_Image();
- $img->setResource($imgcreate);
- $this->assertInstanceOf('\OC_Image', $img);
- $this->assertInstanceOf('\OCP\IImage', $img);
- unset($img);
- $base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
- $img = new \OC_Image();
- $img->loadFromBase64($base64);
- $this->assertInstanceOf('\OC_Image', $img);
- $this->assertInstanceOf('\OCP\IImage', $img);
- unset($img);
- $img = new \OC_Image();
- $this->assertInstanceOf('\OC_Image', $img);
- $this->assertInstanceOf('\OCP\IImage', $img);
- unset($img);
- }
- public function testValid() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertTrue($img->valid());
- $text = base64_encode("Lorem ipsum dolor sir amet …");
- $img = new \OC_Image();
- $img->loadFromBase64($text);
- $this->assertFalse($img->valid());
- $img = new \OC_Image();
- $this->assertFalse($img->valid());
- }
- public function testMimeType() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertEquals('image/png', $img->mimeType());
- $img = new \OC_Image();
- $this->assertEquals('', $img->mimeType());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertEquals('image/jpeg', $img->mimeType());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertEquals('image/gif', $img->mimeType());
- }
- public function testWidth() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertEquals(128, $img->width());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertEquals(1680, $img->width());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertEquals(64, $img->width());
- $img = new \OC_Image();
- $this->assertEquals(-1, $img->width());
- }
- public function testHeight() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertEquals(128, $img->height());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertEquals(1050, $img->height());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertEquals(64, $img->height());
- $img = new \OC_Image();
- $this->assertEquals(-1, $img->height());
- }
- public function testSave() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $img->resize(16);
- $img->save(OC::$SERVERROOT.'/tests/data/testimage2.png');
- $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.png'), $img->data());
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
- $img->resize(128);
- $img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
- $this->assertEquals(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage2.jpg'), $img->data());
- }
- public function testData() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
-
- imagealphablending($raw, true);
- imagesavealpha($raw, true);
- ob_start();
- imagepng($raw);
- $expected = ob_get_clean();
- $this->assertEquals($expected, $img->data());
- $config = $this->createMock(IConfig::class);
- $config->expects($this->once())
- ->method('getAppValue')
- ->with('preview', 'jpeg_quality', '80')
- ->willReturn(null);
- $config->expects($this->once())
- ->method('getSystemValueInt')
- ->with('preview_max_memory', 256)
- ->willReturn(256);
- $img = new \OC_Image(null, null, $config);
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
- $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
-
- imageinterlace($raw, (PHP_VERSION_ID >= 80000 ? true : 1));
- ob_start();
- imagejpeg($raw, null, 80);
- $expected = ob_get_clean();
- $this->assertEquals($expected, $img->data());
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
- $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
- ob_start();
- imagegif($raw);
- $expected = ob_get_clean();
- $this->assertEquals($expected, $img->data());
- }
- public function testDataNoResource() {
- $img = new \OC_Image();
- $this->assertNull($img->data());
- }
-
- public function testToString() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $expected = base64_encode($img->data());
- $this->assertEquals($expected, (string)$img);
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $expected = base64_encode($img->data());
- $this->assertEquals($expected, (string)$img);
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
- $expected = base64_encode($img->data());
- $this->assertEquals($expected, (string)$img);
- }
- public function testResize() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertTrue($img->resize(32));
- $this->assertEquals(32, $img->width());
- $this->assertEquals(32, $img->height());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertTrue($img->resize(840));
- $this->assertEquals(840, $img->width());
- $this->assertEquals(525, $img->height());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertTrue($img->resize(100));
- $this->assertEquals(100, $img->width());
- $this->assertEquals(100, $img->height());
- }
- public function testPreciseResize() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertTrue($img->preciseResize(128, 512));
- $this->assertEquals(128, $img->width());
- $this->assertEquals(512, $img->height());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertTrue($img->preciseResize(64, 840));
- $this->assertEquals(64, $img->width());
- $this->assertEquals(840, $img->height());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertTrue($img->preciseResize(1000, 1337));
- $this->assertEquals(1000, $img->width());
- $this->assertEquals(1337, $img->height());
- }
- public function testCenterCrop() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $img->centerCrop();
- $this->assertEquals(128, $img->width());
- $this->assertEquals(128, $img->height());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $img->centerCrop();
- $this->assertEquals(1050, $img->width());
- $this->assertEquals(1050, $img->height());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $img->centerCrop(512);
- $this->assertEquals(512, $img->width());
- $this->assertEquals(512, $img->height());
- }
- public function testCrop() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertTrue($img->crop(0, 0, 50, 20));
- $this->assertEquals(50, $img->width());
- $this->assertEquals(20, $img->height());
- $img = new \OC_Image();
- $img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $this->assertTrue($img->crop(500, 700, 550, 300));
- $this->assertEquals(550, $img->width());
- $this->assertEquals(300, $img->height());
- $img = new \OC_Image();
- $img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
- $this->assertTrue($img->crop(10, 10, 15, 15));
- $this->assertEquals(15, $img->width());
- $this->assertEquals(15, $img->height());
- }
- public static function sampleProvider() {
- return [
- ['testimage.png', [200, 100], [100, 100]],
- ['testimage.jpg', [840, 840], [840, 525]],
- ['testimage.gif', [200, 250], [200, 200]]
- ];
- }
-
- public function testFitIn($filename, $asked, $expected) {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT . '/tests/data/' . $filename);
- $this->assertTrue($img->fitIn($asked[0], $asked[1]));
- $this->assertEquals($expected[0], $img->width());
- $this->assertEquals($expected[1], $img->height());
- }
- public static function sampleFilenamesProvider() {
- return [
- ['testimage.png'],
- ['testimage.jpg'],
- ['testimage.gif']
- ];
- }
-
- public function testScaleDownToFitWhenSmallerAlready($filename) {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
- $currentWidth = $img->width();
- $currentHeight = $img->height();
-
- $this->assertFalse($img->scaleDownToFit(4000, 4000));
-
- $resizedWidth = $img->width();
- $resizedHeight = $img->height();
- $this->assertEquals(
- $currentWidth, $img->width(), "currentWidth $currentWidth resizedWidth $resizedWidth \n"
- );
- $this->assertEquals(
- $currentHeight, $img->height(),
- "currentHeight $currentHeight resizedHeight $resizedHeight \n"
- );
- }
- public static function largeSampleProvider() {
- return [
- ['testimage.png', [200, 100], [100, 100]],
- ['testimage.jpg', [840, 840], [840, 525]],
- ];
- }
-
- public function testScaleDownWhenBigger($filename, $asked, $expected) {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
-
- $img->scaleDownToFit($asked[0], $asked[1]);
- $this->assertEquals($expected[0], $img->width());
- $this->assertEquals($expected[1], $img->height());
- }
- public function convertDataProvider() {
- return [
- [ 'image/gif'],
- [ 'image/jpeg'],
- [ 'image/png'],
- ];
- }
-
- public function testConvert($mimeType) {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
- $img->save($tempFile, $mimeType);
- $this->assertEquals($mimeType, image_type_to_mime_type(exif_imagetype($tempFile)));
- }
- public function testMemoryLimitFromFile() {
- $img = new \OC_Image();
- $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage-badheader.jpg');
- $this->assertFalse($img->valid());
- }
- public function testMemoryLimitFromData() {
- $data = file_get_contents(OC::$SERVERROOT.'/tests/data/testimage-badheader.jpg');
- $img = new \OC_Image();
- $img->loadFromData($data);
- $this->assertFalse($img->valid());
- }
- }
|