Commit b2175f0e authored by Thomas Müller's avatar Thomas Müller
Browse files

Use \OCP\ILogger

parent fa9b36b7
<?php
/**
* ownCloud
*
* @author Thomas Tanghus
* @copyright 2011 Thomas Tanghus <thomas@tanghus.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
* ownCloud
*
* @author Thomas Tanghus
* @copyright 2011 Thomas Tanghus <thomas@tanghus.net>
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*
*/
/**
* Class for basic image manipulation
*/
......@@ -33,10 +25,16 @@ class OC_Image {
private $fileInfo;
/**
* Get mime type for an image file.
* @param string|null $filePath The path to a local image file.
* @return string The mime type if the it could be determined, otherwise an empty string.
*/
* @var \OCP\ILogger
*/
private $logger;
/**
* Get mime type for an image file.
*
* @param string|null $filePath The path to a local image file.
* @return string The mime type if the it could be determined, otherwise an empty string.
*/
static public function getMimeTypeForFile($filePath) {
// exif_imagetype throws "read error!" if file is less than 12 byte
if (filesize($filePath) > 11) {
......@@ -49,14 +47,19 @@ class OC_Image {
/**
* Constructor.
*
* @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by
* an imagecreate* function.
* @return \OC_Image False on error
* @param \OCP\ILogger $logger
*/
public function __construct($imageRef = null) {
//OC_Log::write('core',__METHOD__.'(): start', OC_Log::DEBUG);
if(!extension_loaded('gd') || !function_exists('gd_info')) {
OC_Log::write('core', __METHOD__.'(): GD module not installed', OC_Log::ERROR);
public function __construct($imageRef = null, $logger = null) {
$this->logger = $logger;
if (is_null($logger)) {
$this->logger = \OC::$server->getLogger();
}
if (!extension_loaded('gd') || !function_exists('gd_info')) {
$this->logger->error(__METHOD__ . '(): GD module not installed', array('app' => 'core'));
return false;
}
......@@ -64,51 +67,56 @@ class OC_Image {
$this->fileInfo = new finfo(FILEINFO_MIME_TYPE);
}
if(!is_null($imageRef)) {
if (!is_null($imageRef)) {
$this->load($imageRef);
}
}
/**
* Determine whether the object contains an image resource.
* @return bool
*/
* Determine whether the object contains an image resource.
*
* @return bool
*/
public function valid() { // apparently you can't name a method 'empty'...
return is_resource($this->resource);
}
/**
* Returns the MIME type of the image or an empty string if no image is loaded.
* @return string
*/
* Returns the MIME type of the image or an empty string if no image is loaded.
*
* @return string
*/
public function mimeType() {
return $this->valid() ? $this->mimeType : '';
}
/**
* Returns the width of the image or -1 if no image is loaded.
* @return int
*/
* Returns the width of the image or -1 if no image is loaded.
*
* @return int
*/
public function width() {
return $this->valid() ? imagesx($this->resource) : -1;
}
/**
* Returns the height of the image or -1 if no image is loaded.
* @return int
*/
* Returns the height of the image or -1 if no image is loaded.
*
* @return int
*/
public function height() {
return $this->valid() ? imagesy($this->resource) : -1;
}
/**
* Returns the width when the image orientation is top-left.
* @return int
*/
* Returns the width when the image orientation is top-left.
*
* @return int
*/
public function widthTopLeft() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->widthTopLeft() Orientation: '.$o, OC_Log::DEBUG);
switch($o) {
$this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core'));
switch ($o) {
case -1:
case 1:
case 2: // Not tested
......@@ -125,13 +133,14 @@ class OC_Image {
}
/**
* Returns the height when the image orientation is top-left.
* @return int
*/
* Returns the height when the image orientation is top-left.
*
* @return int
*/
public function heightTopLeft() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->heightTopLeft() Orientation: '.$o, OC_Log::DEBUG);
switch($o) {
$this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core'));
switch ($o) {
case -1:
case 1:
case 2: // Not tested
......@@ -149,32 +158,34 @@ class OC_Image {
/**
* Outputs the image.
*
* @param string $mimeType
* @return bool
*/
public function show($mimeType=null) {
if($mimeType === null) {
public function show($mimeType = null) {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
header('Content-Type: '.$mimeType);
header('Content-Type: ' . $mimeType);
return $this->_output(null, $mimeType);
}
/**
* Saves the image.
*
* @param string $filePath
* @param string $mimeType
* @return bool
*/
public function save($filePath=null, $mimeType=null) {
if($mimeType === null) {
public function save($filePath = null, $mimeType = null) {
if ($mimeType === null) {
$mimeType = $this->mimeType();
}
if($filePath === null && $this->filePath === null) {
OC_Log::write('core', __METHOD__.'(): called with no path.', OC_Log::ERROR);
if ($filePath === null && $this->filePath === null) {
$this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core'));
return false;
} elseif($filePath === null && $this->filePath !== null) {
} elseif ($filePath === null && $this->filePath !== null) {
$filePath = $this->filePath;
}
return $this->_output($filePath, $mimeType);
......@@ -182,22 +193,21 @@ class OC_Image {
/**
* Outputs/saves the image.
*
* @param string $filePath
* @param string $mimeType
* @return bool
* @throws Exception
*/
private function _output($filePath=null, $mimeType=null) {
if($filePath) {
private function _output($filePath = null, $mimeType = null) {
if ($filePath) {
if (!file_exists(dirname($filePath)))
mkdir(dirname($filePath), 0777, true);
if(!is_writable(dirname($filePath))) {
OC_Log::write('core',
__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.',
OC_Log::ERROR);
if (!is_writable(dirname($filePath))) {
$this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core'));
return false;
} elseif(is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
OC_Log::write('core', __METHOD__.'(): File \''.$filePath.'\' is not writable.', OC_Log::ERROR);
} elseif (is_writable(dirname($filePath)) && file_exists($filePath) && !is_writable($filePath)) {
$this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core'));
return false;
}
}
......@@ -206,8 +216,8 @@ class OC_Image {
}
$imageType = $this->imageType;
if($mimeType !== null) {
switch($mimeType) {
if ($mimeType !== null) {
switch ($mimeType) {
case 'image/gif':
$imageType = IMAGETYPE_GIF;
break;
......@@ -228,7 +238,7 @@ class OC_Image {
}
}
switch($imageType) {
switch ($imageType) {
case IMAGETYPE_GIF:
$retVal = imagegif($this->resource, $filePath);
break;
......@@ -259,22 +269,22 @@ class OC_Image {
}
/**
* Prints the image when called as $image().
*/
* Prints the image when called as $image().
*/
public function __invoke() {
return $this->show();
}
/**
* @return resource Returns the image resource in any.
*/
* @return resource Returns the image resource in any.
*/
public function resource() {
return $this->resource;
}
/**
* @return string Returns the raw image data.
*/
* @return string Returns the raw image data.
*/
function data() {
ob_start();
switch ($this->mimeType) {
......@@ -289,11 +299,11 @@ class OC_Image {
break;
default:
$res = imagepng($this->resource);
OC_Log::write('core', 'OC_Image->data. Couldn\'t guess mimetype, defaulting to png', OC_Log::INFO);
$this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core'));
break;
}
if (!$res) {
OC_Log::write('core', 'OC_Image->data. Error getting image data.', OC_Log::ERROR);
$this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core'));
}
return ob_get_clean();
}
......@@ -306,47 +316,49 @@ class OC_Image {
}
/**
* (I'm open for suggestions on better method name ;)
* Get the orientation based on EXIF data.
* @return int The orientation or -1 if no EXIF data is available.
*/
* (I'm open for suggestions on better method name ;)
* Get the orientation based on EXIF data.
*
* @return int The orientation or -1 if no EXIF data is available.
*/
public function getOrientation() {
if ($this->imageType !== IMAGETYPE_JPEG) {
OC_Log::write('core', 'OC_Image->fixOrientation() Image is not a JPEG.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core'));
return -1;
}
if(!is_callable('exif_read_data')) {
OC_Log::write('core', 'OC_Image->fixOrientation() Exif module not enabled.', OC_Log::DEBUG);
if (!is_callable('exif_read_data')) {
$this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core'));
return -1;
}
if(!$this->valid()) {
OC_Log::write('core', 'OC_Image->fixOrientation() No image loaded.', OC_Log::DEBUG);
if (!$this->valid()) {
$this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core'));
return -1;
}
if(is_null($this->filePath) || !is_readable($this->filePath)) {
OC_Log::write('core', 'OC_Image->fixOrientation() No readable file path set.', OC_Log::DEBUG);
if (is_null($this->filePath) || !is_readable($this->filePath)) {
$this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core'));
return -1;
}
$exif = @exif_read_data($this->filePath, 'IFD0');
if(!$exif) {
if (!$exif) {
return -1;
}
if(!isset($exif['Orientation'])) {
if (!isset($exif['Orientation'])) {
return -1;
}
return $exif['Orientation'];
}
/**
* (I'm open for suggestions on better method name ;)
* Fixes orientation based on EXIF data.
* @return bool.
*/
* (I'm open for suggestions on better method name ;)
* Fixes orientation based on EXIF data.
*
* @return bool.
*/
public function fixOrientation() {
$o = $this->getOrientation();
OC_Log::write('core', 'OC_Image->fixOrientation() Orientation: '.$o, OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core'));
$rotate = 0;
switch($o) {
switch ($o) {
case -1:
return false; //Nothing to fix
case 1:
......@@ -375,24 +387,24 @@ class OC_Image {
$rotate = 90;
break;
}
if($rotate) {
if ($rotate) {
$res = imagerotate($this->resource, $rotate, 0);
if($res) {
if(imagealphablending($res, true)) {
if(imagesavealpha($res, true)) {
if ($res) {
if (imagealphablending($res, true)) {
if (imagesavealpha($res, true)) {
imagedestroy($this->resource);
$this->resource = $res;
return true;
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during alphasaving.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core'));
return false;
}
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during alphablending.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core'));
return false;
}
} else {
OC_Log::write('core', 'OC_Image->fixOrientation() Error during oriention fixing.', OC_Log::DEBUG);
$this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core'));
return false;
}
}
......@@ -401,52 +413,54 @@ class OC_Image {
/**
* Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function.
*
* @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by an imagecreate* function or a file resource (file handle ).
* @return resource|false An image resource or false on error
*/
public function load($imageRef) {
if(is_resource($imageRef)) {
if(get_resource_type($imageRef) == 'gd') {
if (is_resource($imageRef)) {
if (get_resource_type($imageRef) == 'gd') {
$this->resource = $imageRef;
return $this->resource;
} elseif(in_array(get_resource_type($imageRef), array('file', 'stream'))) {
} elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) {
return $this->loadFromFileHandle($imageRef);
}
} elseif($this->loadFromBase64($imageRef) !== false) {
} elseif ($this->loadFromBase64($imageRef) !== false) {
return $this->resource;
} elseif($this->loadFromFile($imageRef) !== false) {
} elseif ($this->loadFromFile($imageRef) !== false) {
return $this->resource;
} elseif($this->loadFromData($imageRef) !== false) {
} elseif ($this->loadFromData($imageRef) !== false) {
return $this->resource;
} else {
OC_Log::write('core', __METHOD__.'(): couldn\'t load anything. Giving up!', OC_Log::DEBUG);
return false;
}
$this->logger->debug(__METHOD__ . '(): could not load anything. Giving up!', array('app' => 'core'));
return false;
}
/**
* Loads an image from an open file handle.
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
* @param resource $handle
* @return resource|false An image resource or false on error
*/
* Loads an image from an open file handle.
* It is the responsibility of the caller to position the pointer at the correct place and to close the handle again.
*
* @param resource $handle
* @return resource|false An image resource or false on error
*/
public function loadFromFileHandle($handle) {
OC_Log::write('core', __METHOD__.'(): Trying', OC_Log::DEBUG);
$contents = stream_get_contents($handle);
if($this->loadFromData($contents)) {
if ($this->loadFromData($contents)) {
return $this->resource;
}
return false;
}
/**
* Loads an image from a local file.
* @param bool|string $imagePath The path to a local file.
* @return bool|resource An image resource or false on error
*/
public function loadFromFile($imagePath=false) {
* Loads an image from a local file.
*
* @param bool|string $imagePath The path to a local file.
* @return bool|resource An image resource or false on error
*/
public function loadFromFile($imagePath = false) {
// exif_imagetype throws "read error!" if file is less than 12 byte
if(!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
OC_Log::write('core', 'OC_Image->loadFromFile, couldn\'t load: ' . (string) urlencode($imagePath), OC_Log::DEBUG);
if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) {
$this->logger->debug('OC_Image->loadFromFile, could not load: ' . (string)urlencode($imagePath), array('app' => 'core'));
return false;
}
$iType = exif_imagetype($imagePath);
......@@ -458,18 +472,14 @@ class OC_Image {
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, GIF images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_JPEG:
if (imagetypes() & IMG_JPG) {
$this->resource = imagecreatefromjpeg($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, JPG images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_PNG:
......@@ -479,31 +489,25 @@ class OC_Image {
imagealphablending($this->resource, true);
imagesavealpha($this->resource, true);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, PNG images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_XBM:
if (imagetypes() & IMG_XPM) {
$this->resource = imagecreatefromxbm($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_WBMP:
if (imagetypes() & IMG_WBMP) {
$this->resource = imagecreatefromwbmp($imagePath);
} else {
OC_Log::write('core',
'OC_Image->loadFromFile, WBMP images not supported: '.$imagePath,
OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core'));
}
break;
case IMAGETYPE_BMP:
$this->resource = $this->imagecreatefrombmp($imagePath);
$this->resource = $this->imagecreatefrombmp($imagePath);
break;
/*
case IMAGETYPE_TIFF_II: // (intel byte order)
......@@ -534,10 +538,10 @@ class OC_Image {
// this is mostly file created from encrypted file
$this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath)));
$iType = IMAGETYPE_PNG;
OC_Log::write('core', 'OC_Image->loadFromFile, Default', OC_Log::DEBUG);
$this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core'));
break;
}
if($this->valid()) {
if ($this->valid()) {
$this->imageType = $iType;
$this->mimeType = image_type_to_mime_type($iType);
$this->filePath = $imagePath;
......@@ -546,47 +550,49 @@ class OC_Image {
}
/**
* Loads an image from a string of data.
* @param string $str A string of image data as read from a file.
* @return bool|resource An image resource or false on error
*/
* Loads an image from a string of data.
*
* @param string $str A string of image data as read from a file.
* @return bool|resource An image resource or false on error
*/
public function loadFromData($str) {
if(is_resource($str)) {
if (is_resource($str)) {
return false;
}
$this->resource = @imagecreatefromstring($str);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($str);
}
if(is_resource($this->resource)) {
if (is_resource($this->resource)) {
imagealphablending($this->resource, false);
imagesavealpha($this->resource, true);
}
if(!$this->resource) {
OC_Log::write('core', 'OC_Image->loadFromData, couldn\'t load', OC_Log::DEBUG);
if (!$this->resource) {
$this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core'));
return false;
}
return $this->resource;
}
/**
* Loads an image from a base64 encoded string.
* @param string $str A string base64 encoded string of image data.
* @return bool|resource An image resource or false on error
*/
* Loads an image from a base64 encoded string.
*
* @param string $str A string base64 encoded string of image data.
* @return bool|resource An image resource or false on error
*/
public function loadFromBase64($str) {
if(!is_string($str)) {
if (!is_string($str)) {
return false;
}
$data = base64_decode($str);
if($data) { // try to load from string data
if ($data) { // try to load from string data
$this->resource = @imagecreatefromstring($data);
if ($this->fileInfo) {
$this->mimeType = $this->fileInfo->buffer($data);
}
if(!$this->resource) {
OC_Log::write('core', 'OC_Image->loadFromBase64, couldn\'t load', OC_Log::DEBUG);
if (!$this->resource) {