namespace OC;
+use finfo;
use OCP\IImage;
/**
* @param \OCP\IConfig $config
* @throws \InvalidArgumentException in case the $imageRef parameter is not null
*/
- public function __construct($imageRef = null, ?\OCP\ILogger $logger = null, ?\OCP\IConfig $config = null) {
+ public function __construct(
+ $imageRef = null,
+ ?\OCP\ILogger $logger = null,
+ ?\OCP\IConfig $config = null
+ ) {
$this->logger = $logger;
if ($logger === null) {
$this->logger = \OC::$server->getLogger();
*/
public function widthTopLeft(): int {
$o = $this->getOrientation();
- $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, ['app' => 'core']);
+ $this->logger->debug('Image->widthTopLeft() Orientation: ' . $o, ['app' => 'core']);
switch ($o) {
case -1:
case 1:
*/
public function heightTopLeft(): int {
$o = $this->getOrientation();
- $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, ['app' => 'core']);
+ $this->logger->debug('Image->heightTopLeft() Orientation: ' . $o, ['app' => 'core']);
switch ($o) {
case -1:
case 1:
$imageType = IMAGETYPE_WEBP;
break;
default:
- throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
+ throw new \Exception('Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format');
}
}
if (function_exists('imagexbm')) {
$retVal = imagexbm($this->resource, $filePath);
} else {
- throw new Exception('\OC_Image::_output(): imagexbm() is not supported.');
+ throw new \Exception('Image::_output(): imagexbm() is not supported.');
}
break;
break;
default:
$res = imagepng($this->resource);
- $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
+ $this->logger->info('Image->data. Could not guess mime-type, defaulting to png', ['app' => 'core']);
break;
}
if (!$res) {
- $this->logger->error('OC_Image->data. Error getting image data.', ['app' => 'core']);
+ $this->logger->error('Image->data. Error getting image data.', ['app' => 'core']);
}
return ob_get_clean();
}
}
if ($this->imageType !== IMAGETYPE_JPEG) {
- $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Image is not a JPEG.', ['app' => 'core']);
return -1;
}
if (!is_callable('exif_read_data')) {
- $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Exif module not enabled.', ['app' => 'core']);
return -1;
}
if (!$this->valid()) {
- $this->logger->debug('OC_Image->fixOrientation() No image loaded.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() No image loaded.', ['app' => 'core']);
return -1;
}
if (is_null($this->filePath) || !is_readable($this->filePath)) {
- $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() No readable file path set.', ['app' => 'core']);
return -1;
}
$exif = @exif_read_data($this->filePath, 'IFD0');
public function readExif($data): void {
if (!is_callable('exif_read_data')) {
- $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Exif module not enabled.', ['app' => 'core']);
return;
}
if (!$this->valid()) {
- $this->logger->debug('OC_Image->fixOrientation() No image loaded.', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() No image loaded.', ['app' => 'core']);
return;
}
return false;
}
$o = $this->getOrientation();
- $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Orientation: ' . $o, ['app' => 'core']);
$rotate = 0;
$flip = false;
switch ($o) {
$this->resource = $res;
return true;
} else {
- $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Error during alpha-saving', ['app' => 'core']);
return false;
}
} else {
- $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Error during alpha-blending', ['app' => 'core']);
return false;
}
} else {
- $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', ['app' => 'core']);
+ $this->logger->debug('Image->fixOrientation() Error during orientation fixing', ['app' => 'core']);
return false;
}
}
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
- $this->logger->debug('OC_Image->loadFromFile, GIF image not valid: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, GIF image not valid: ' . $imagePath, ['app' => 'core']);
}
} else {
- $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, GIF images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_JPEG:
if (@getimagesize($imagePath) !== false) {
$this->resource = @imagecreatefromjpeg($imagePath);
} else {
- $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, JPG image not valid: ' . $imagePath, ['app' => 'core']);
}
} else {
- $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, JPG images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_PNG:
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
- $this->logger->debug('OC_Image->loadFromFile, PNG image not valid: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, PNG image not valid: ' . $imagePath, ['app' => 'core']);
}
} else {
- $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, PNG images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_XBM:
}
$this->resource = @imagecreatefromxbm($imagePath);
} else {
- $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_WBMP:
}
$this->resource = @imagecreatefromwbmp($imagePath);
} else {
- $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, WBMP images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
case IMAGETYPE_BMP:
// Check for animation indicators
if (strpos(strtoupper($header['Chunk']), 'ANIM') !== false || strpos(strtoupper($header['Chunk']), 'ANMF') !== false) {
// Animated so don't let it reach libgd
- $this->logger->debug('OC_Image->loadFromFile, animated WEBP images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, animated WEBP images not supported: ' . $imagePath, ['app' => 'core']);
} else {
// We're safe so give it to libgd
$this->resource = @imagecreatefromwebp($imagePath);
}
} else {
- $this->logger->debug('OC_Image->loadFromFile, WEBP images not supported: ' . $imagePath, ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, WEBP images not supported: ' . $imagePath, ['app' => 'core']);
}
break;
/*
}
$this->resource = @imagecreatefromstring($data);
$iType = IMAGETYPE_PNG;
- $this->logger->debug('OC_Image->loadFromFile, Default', ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, Default', ['app' => 'core']);
break;
}
if ($this->valid()) {
}
if (!$this->resource) {
- $this->logger->debug('OC_Image->loadFromFile, could not load', ['app' => 'core']);
+ $this->logger->debug('Image->loadFromFile, could not load', ['app' => 'core']);
return false;
}
return $this->resource;
$this->mimeType = $this->fileInfo->buffer($data);
}
if (!$this->resource) {
- $this->logger->debug('OC_Image->loadFromBase64, could not load', ['app' => 'core']);
+ $this->logger->debug('Image->loadFromBase64, could not load', ['app' => 'core']);
return false;
}
return $this->resource;
*/
public function centerCrop(int $size = 0): bool {
if (!$this->valid()) {
- $this->logger->debug('OC_Image->centerCrop, No image loaded', ['app' => 'core']);
+ $this->logger->debug('Image->centerCrop, No image loaded', ['app' => 'core']);
return false;
}
$widthOrig = imagesx($this->resource);
}
$process = imagecreatetruecolor($targetWidth, $targetHeight);
if ($process === false) {
- $this->logger->debug('OC_Image->centerCrop, Error creating true color image', ['app' => 'core']);
+ $this->logger->debug('Image->centerCrop, Error creating true color image', ['app' => 'core']);
return false;
}
$result = imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height);
if ($result === false) {
- $this->logger->debug('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, ['app' => 'core']);
+ $this->logger->debug('Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, ['app' => 'core']);
return false;
}
imagedestroy($this->resource);
namespace Test;
use OC;
+use OC\Image;
use OCP\IConfig;
class ImageTest extends \Test\TestCase {
}
public function testConstructDestruct() {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
- $this->assertInstanceOf('\OC_Image', $img);
+ $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 = new Image();
$img->setResource($imgcreate);
- $this->assertInstanceOf('\OC_Image', $img);
+ $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 = new Image();
$img->loadFromBase64($base64);
- $this->assertInstanceOf('\OC_Image', $img);
+ $this->assertInstanceOf('\OC\Image', $img);
$this->assertInstanceOf('\OCP\IImage', $img);
unset($img);
- $img = new \OC_Image();
- $this->assertInstanceOf('\OC_Image', $img);
+ $img = new Image();
+ $this->assertInstanceOf('\OC\Image', $img);
$this->assertInstanceOf('\OCP\IImage', $img);
unset($img);
}
public function testValid() {
- $img = new \OC_Image();
+ $img = new 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 = new Image();
$img->loadFromBase64($text);
$this->assertFalse($img->valid());
- $img = new \OC_Image();
+ $img = new Image();
$this->assertFalse($img->valid());
}
public function testMimeType() {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$this->assertEquals('image/png', $img->mimeType());
- $img = new \OC_Image();
+ $img = new Image();
$this->assertEquals('', $img->mimeType());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->assertEquals('image/jpeg', $img->mimeType());
- $img = new \OC_Image();
+ $img = new 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 = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$this->assertEquals(128, $img->width());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->assertEquals(1680, $img->width());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
$this->assertEquals(64, $img->width());
- $img = new \OC_Image();
+ $img = new Image();
$this->assertEquals(-1, $img->width());
}
public function testHeight() {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$this->assertEquals(128, $img->height());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromData(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));
$this->assertEquals(1050, $img->height());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
$this->assertEquals(64, $img->height());
- $img = new \OC_Image();
+ $img = new Image();
$this->assertEquals(-1, $img->height());
}
public function testSave() {
- $img = new \OC_Image();
+ $img = new 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 = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.jpg');
$img->resize(128);
$img->save(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
}
public function testData() {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.png'));
// Preserve transparency
->method('getSystemValueInt')
->with('preview_max_memory', 256)
->willReturn(256);
- $img = new \OC_Image(null, null, $config);
+ $img = new 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, true);
$expected = ob_get_clean();
$this->assertEquals($expected, $img->data());
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.gif');
$raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif'));
ob_start();
}
public function testDataNoResource() {
- $img = new \OC_Image();
+ $img = new Image();
$this->assertNull($img->data());
}
* @depends testData
*/
public function testToString() {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$expected = base64_encode($img->data());
$this->assertEquals($expected, (string)$img);
- $img = new \OC_Image();
+ $img = new 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 = new 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 = new 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 = new 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 = new Image();
$img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
$this->assertTrue($img->resize(100));
$this->assertEquals(100, $img->width());
}
public function testPreciseResize() {
- $img = new \OC_Image();
+ $img = new 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 = new 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 = new 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());
}
public function testCenterCrop() {
- $img = new \OC_Image();
+ $img = new 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 = new 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 = new Image();
$img->loadFromBase64(base64_encode(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.gif')));
$img->centerCrop(512);
$this->assertEquals(512, $img->width());
}
public function testCrop() {
- $img = new \OC_Image();
+ $img = new 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 = new 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 = new 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());
* @param int[] $expected
*/
public function testFitIn($filename, $asked, $expected) {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT . '/tests/data/' . $filename);
$this->assertTrue($img->fitIn($asked[0], $asked[1]));
$this->assertEquals($expected[0], $img->width());
* @param string $filename
*/
public function testScaleDownToFitWhenSmallerAlready($filename) {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
$currentWidth = $img->width();
$currentHeight = $img->height();
* @param int[] $expected
*/
public function testScaleDownWhenBigger($filename, $asked, $expected) {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/' . $filename);
//$this->assertTrue($img->scaleDownToFit($asked[0], $asked[1]));
$img->scaleDownToFit($asked[0], $asked[1]);
* @dataProvider convertDataProvider
*/
public function testConvert($mimeType) {
- $img = new \OC_Image();
+ $img = new Image();
$img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.png');
$tempFile = tempnam(sys_get_temp_dir(), 'img-test');
}
public function testMemoryLimitFromFile() {
- $img = new \OC_Image();
+ $img = new 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 = new Image();
$img->loadFromData($data);
$this->assertFalse($img->valid());
}