2013-08-01 14:24:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2014-11-10 21:59:50 +00:00
|
|
|
class Test_Image extends \Test\TestCase {
|
2013-08-06 14:56:50 +00:00
|
|
|
public static function tearDownAfterClass() {
|
2014-03-17 07:40:59 +00:00
|
|
|
@unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
|
|
|
|
@unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
|
2014-11-10 21:59:50 +00:00
|
|
|
|
|
|
|
parent::tearDownAfterClass();
|
2013-08-06 14:56:50 +00:00
|
|
|
}
|
2013-08-01 14:24:29 +00:00
|
|
|
|
|
|
|
public function testGetMimeTypeForFile() {
|
|
|
|
$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertEquals('image/png', $mimetype);
|
|
|
|
|
|
|
|
$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
|
|
|
|
$this->assertEquals('image/jpeg', $mimetype);
|
|
|
|
|
|
|
|
$mimetype = \OC_Image::getMimeTypeForFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
|
|
|
|
$this->assertEquals('image/gif', $mimetype);
|
|
|
|
|
|
|
|
$mimetype = \OC_Image::getMimeTypeForFile(null);
|
|
|
|
$this->assertEquals('', $mimetype);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructDestruct() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertInstanceOf('\OC_Image', $img);
|
2015-03-13 09:10:11 +00:00
|
|
|
$this->assertInstanceOf('\OCP\IImage', $img);
|
2013-08-01 14:24:29 +00:00
|
|
|
unset($img);
|
|
|
|
|
|
|
|
$imgcreate = imagecreatefromjpeg(OC::$SERVERROOT.'/tests/data/testimage.jpg');
|
|
|
|
$img = new \OC_Image($imgcreate);
|
|
|
|
$this->assertInstanceOf('\OC_Image', $img);
|
2015-03-13 09:10:11 +00:00
|
|
|
$this->assertInstanceOf('\OCP\IImage', $img);
|
2013-08-01 14:24:29 +00:00
|
|
|
unset($img);
|
|
|
|
|
|
|
|
$base64 = base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
|
|
|
|
$img = new \OC_Image($base64);
|
|
|
|
$this->assertInstanceOf('\OC_Image', $img);
|
2015-03-13 09:10:11 +00:00
|
|
|
$this->assertInstanceOf('\OCP\IImage', $img);
|
2013-08-01 14:24:29 +00:00
|
|
|
unset($img);
|
|
|
|
|
|
|
|
$img = new \OC_Image(null);
|
|
|
|
$this->assertInstanceOf('\OC_Image', $img);
|
2015-03-13 09:10:11 +00:00
|
|
|
$this->assertInstanceOf('\OCP\IImage', $img);
|
2013-08-01 14:24:29 +00:00
|
|
|
unset($img);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValid() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertTrue($img->valid());
|
|
|
|
|
|
|
|
$text = base64_encode("Lorem ipsum dolor sir amet …");
|
|
|
|
$img = new \OC_Image($text);
|
|
|
|
$this->assertFalse($img->valid());
|
|
|
|
|
|
|
|
$img = new \OC_Image(null);
|
|
|
|
$this->assertFalse($img->valid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMimeType() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertEquals('image/png', $img->mimeType());
|
|
|
|
|
2014-11-10 10:01:43 +00:00
|
|
|
$img = new \OC_Image(null);
|
|
|
|
$this->assertEquals('', $img->mimeType());
|
|
|
|
|
|
|
|
if (\OC_Util::runningOnWindows()) {
|
|
|
|
$this->markTestSkipped('[Windows] Images created with imagecreate() are pngs on windows');
|
|
|
|
}
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals('image/jpeg', $img->mimeType());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals('image/gif', $img->mimeType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWidth() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertEquals(128, $img->width());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals(1680, $img->width());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals(64, $img->width());
|
|
|
|
|
|
|
|
$img = new \OC_Image(null);
|
|
|
|
$this->assertEquals(-1, $img->width());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHeight() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertEquals(128, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals(1050, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals(64, $img->height());
|
|
|
|
|
|
|
|
$img = new \OC_Image(null);
|
|
|
|
$this->assertEquals(-1, $img->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSave() {
|
|
|
|
$img = new \OC_Image(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());
|
2013-08-06 14:56:50 +00:00
|
|
|
|
|
|
|
$img = new \OC_Image(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());
|
2013-08-01 14:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testData() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
2013-08-06 14:56:50 +00:00
|
|
|
$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
|
2014-08-12 09:00:00 +00:00
|
|
|
// Preserve transparency
|
|
|
|
imagealphablending($raw, true);
|
|
|
|
imagesavealpha($raw, true);
|
2013-08-06 14:56:50 +00:00
|
|
|
ob_start();
|
|
|
|
imagepng($raw);
|
|
|
|
$expected = ob_get_clean();
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, $img->data());
|
|
|
|
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.jpg');
|
2013-08-06 14:56:50 +00:00
|
|
|
$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
|
|
|
ob_start();
|
|
|
|
imagejpeg($raw);
|
|
|
|
$expected = ob_get_clean();
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, $img->data());
|
|
|
|
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
|
2013-08-06 14:56:50 +00:00
|
|
|
$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
|
|
|
|
ob_start();
|
|
|
|
imagegif($raw);
|
|
|
|
$expected = ob_get_clean();
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, $img->data());
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:23:02 +00:00
|
|
|
public function testDataNoResource() {
|
|
|
|
$img = new \OC_Image();
|
|
|
|
$this->assertNull($img->data());
|
|
|
|
}
|
|
|
|
|
2013-09-01 13:50:58 +00:00
|
|
|
/**
|
|
|
|
* @depends testData
|
|
|
|
*/
|
2013-08-01 14:24:29 +00:00
|
|
|
public function testToString() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
2013-08-06 14:56:50 +00:00
|
|
|
$expected = base64_encode($img->data());
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, (string)$img);
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-06 14:56:50 +00:00
|
|
|
$expected = base64_encode($img->data());
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, (string)$img);
|
|
|
|
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.gif');
|
2013-08-06 14:56:50 +00:00
|
|
|
$expected = base64_encode($img->data());
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertEquals($expected, (string)$img);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testResize() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertTrue($img->resize(32));
|
|
|
|
$this->assertEquals(32, $img->width());
|
|
|
|
$this->assertEquals(32, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->resize(840));
|
|
|
|
$this->assertEquals(840, $img->width());
|
|
|
|
$this->assertEquals(525, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->resize(100));
|
|
|
|
$this->assertEquals(100, $img->width());
|
|
|
|
$this->assertEquals(100, $img->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPreciseResize() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertTrue($img->preciseResize(128, 512));
|
|
|
|
$this->assertEquals(128, $img->width());
|
|
|
|
$this->assertEquals(512, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->preciseResize(64, 840));
|
|
|
|
$this->assertEquals(64, $img->width());
|
|
|
|
$this->assertEquals(840, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->preciseResize(1000, 1337));
|
|
|
|
$this->assertEquals(1000, $img->width());
|
|
|
|
$this->assertEquals(1337, $img->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCenterCrop() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$img->centerCrop();
|
|
|
|
$this->assertEquals(128, $img->width());
|
|
|
|
$this->assertEquals(128, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$img->centerCrop();
|
|
|
|
$this->assertEquals(1050, $img->width());
|
|
|
|
$this->assertEquals(1050, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$img->centerCrop(512);
|
|
|
|
$this->assertEquals(512, $img->width());
|
|
|
|
$this->assertEquals(512, $img->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCrop() {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$this->assertTrue($img->crop(0, 0, 50, 20));
|
|
|
|
$this->assertEquals(50, $img->width());
|
|
|
|
$this->assertEquals(20, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->crop(500, 700, 550, 300));
|
|
|
|
$this->assertEquals(550, $img->width());
|
|
|
|
$this->assertEquals(300, $img->height());
|
|
|
|
|
2013-08-01 15:18:02 +00:00
|
|
|
$img = new \OC_Image(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
|
2013-08-01 14:24:29 +00:00
|
|
|
$this->assertTrue($img->crop(10, 10, 15, 15));
|
|
|
|
$this->assertEquals(15, $img->width());
|
|
|
|
$this->assertEquals(15, $img->height());
|
|
|
|
}
|
|
|
|
|
2015-06-06 14:21:36 +00:00
|
|
|
public static function sampleProvider() {
|
|
|
|
return [
|
|
|
|
['testimage.png', [200, 100], [100, 100]],
|
|
|
|
['testimage.jpg', [840, 840], [840, 525]],
|
|
|
|
['testimage.gif', [200, 250], [200, 200]]
|
|
|
|
];
|
|
|
|
}
|
2013-08-01 14:24:29 +00:00
|
|
|
|
2015-06-06 14:21:36 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider sampleProvider
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param int[] $asked
|
|
|
|
* @param int[] $expected
|
|
|
|
*/
|
|
|
|
public function testFitIn($filename, $asked, $expected) {
|
|
|
|
$img = new \OC_Image(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());
|
|
|
|
}
|
2013-08-01 14:24:29 +00:00
|
|
|
|
2015-06-06 14:21:36 +00:00
|
|
|
public static function sampleFilenamesProvider() {
|
|
|
|
return [
|
|
|
|
['testimage.png'],
|
|
|
|
['testimage.jpg'],
|
|
|
|
['testimage.gif']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Image should not be resized if it's already smaller than what is required
|
|
|
|
*
|
|
|
|
* @dataProvider sampleFilenamesProvider
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
*/
|
|
|
|
public function testScaleDownToFitWhenSmallerAlready($filename) {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
|
|
|
|
$currentWidth = $img->width();
|
|
|
|
$currentHeight = $img->height();
|
|
|
|
// We pick something larger than the image we want to scale down
|
|
|
|
$this->assertFalse($img->scaleDownToFit(4000, 4000));
|
|
|
|
// The dimensions of the image should not have changed since it's smaller already
|
|
|
|
$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]],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider largeSampleProvider
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param int[] $asked
|
|
|
|
* @param int[] $expected
|
|
|
|
*/
|
|
|
|
public function testScaleDownWhenBigger($filename, $asked, $expected) {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT . '/tests/data/' . $filename);
|
|
|
|
//$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
|
|
|
|
$img->scaleDownToFit($asked[0], $asked[1]);
|
|
|
|
$this->assertEquals($expected[0], $img->width());
|
|
|
|
$this->assertEquals($expected[1], $img->height());
|
2013-08-01 14:24:29 +00:00
|
|
|
}
|
2014-03-17 07:40:59 +00:00
|
|
|
|
|
|
|
function convertDataProvider() {
|
|
|
|
return array(
|
|
|
|
array( 'image/gif'),
|
|
|
|
array( 'image/jpeg'),
|
|
|
|
array( 'image/png'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider convertDataProvider
|
|
|
|
*/
|
|
|
|
public function testConvert($mimeType) {
|
|
|
|
$img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
|
|
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'img-test');
|
|
|
|
|
|
|
|
$img->save($tempFile, $mimeType);
|
|
|
|
$actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
|
|
|
|
$this->assertEquals($mimeType, $actualMimeType);
|
|
|
|
}
|
2013-08-01 14:24:29 +00:00
|
|
|
}
|