diff options
author | Julius Härtl <jus@bitgrid.net> | 2018-01-18 14:39:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-18 14:39:08 +0100 |
commit | f0b7b2dedf75ef3e483de584094586fbadc4455a (patch) | |
tree | 0650346678c3e291b682cd23fb0f857fd0205987 /lib | |
parent | d0c46ab45e48454bd76cbe6b61cc604074c57a76 (diff) | |
parent | 3d4c698f448925aca62976175ffd4bab9694c7d6 (diff) | |
download | nextcloud-server-f0b7b2dedf75ef3e483de584094586fbadc4455a.tar.gz nextcloud-server-f0b7b2dedf75ef3e483de584094586fbadc4455a.zip |
Merge pull request #7911 from nextcloud/improve-oc_image
Improve OC_Image code to not guess the type of input
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Avatar.php | 19 | ||||
-rw-r--r-- | lib/private/Preview/TXT.php | 5 | ||||
-rw-r--r-- | lib/private/PreviewManager.php | 4 | ||||
-rw-r--r-- | lib/private/legacy/image.php | 50 |
4 files changed, 38 insertions, 40 deletions
diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php index afa9118c509..ce3fcff396e 100644 --- a/lib/private/Avatar.php +++ b/lib/private/Avatar.php @@ -117,7 +117,24 @@ class Avatar implements IAvatar { $img = $data; $data = $img->data(); } else { - $img = new OC_Image($data); + $img = new OC_Image(); + if (is_resource($data) && get_resource_type($data) === "gd") { + $img->setResource($data); + } elseif(is_resource($data)) { + $img->loadFromFileHandle($data); + } else { + try { + // detect if it is a path or maybe the images as string + $result = @realpath($data); + if ($result === false || $result === null) { + $img->loadFromData($data); + } else { + $img->loadFromFile($data); + } + } catch (\Error $e) { + $img->loadFromData($data); + } + } } $type = substr($img->mimeType(), -3); if ($type === 'peg') { diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php index a4b7ec769c0..88c6e87f38e 100644 --- a/lib/private/Preview/TXT.php +++ b/lib/private/Preview/TXT.php @@ -91,8 +91,9 @@ class TXT extends Provider { } } - $image = new \OC_Image($image); + $imageObject = new \OC_Image(); + $imageObject->setResource($image); - return $image->valid() ? $image : false; + return $imageObject->valid() ? $imageObject : false; } } diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index db575a8cbe0..46ef8862e6b 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -166,7 +166,9 @@ class PreviewManager implements IPreview { return new \OC_Image(); } - return new \OC_Image($preview->getContent()); + $previewImage = new \OC_Image(); + $previewImage->loadFromData($preview->getContent()); + return $previewImage; } /** diff --git a/lib/private/legacy/image.php b/lib/private/legacy/image.php index 873f9711d5c..6ad9426a717 100644 --- a/lib/private/legacy/image.php +++ b/lib/private/legacy/image.php @@ -69,6 +69,7 @@ class OC_Image implements \OCP\IImage { * an imagecreate* function. * @param \OCP\ILogger $logger * @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) { $this->logger = $logger; @@ -85,7 +86,7 @@ class OC_Image implements \OCP\IImage { } if ($imageRef !== null) { - $this->load($imageRef); + throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); } } @@ -298,6 +299,18 @@ class OC_Image implements \OCP\IImage { } /** + * @param resource Returns the image resource in any. + * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" + */ + public function setResource($resource) { + if (get_resource_type($resource) === 'gd') { + $this->resource = $resource; + return; + } + throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); + } + + /** * @return resource Returns the image resource in any. */ public function resource() { @@ -504,31 +517,6 @@ class OC_Image implements \OCP\IImage { } /** - * 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') { - $this->resource = $imageRef; - return $this->resource; - } elseif (in_array(get_resource_type($imageRef), array('file', 'stream'))) { - return $this->loadFromFileHandle($imageRef); - } - } elseif ($this->loadFromBase64($imageRef) !== false) { - return $this->resource; - } elseif ($this->loadFromFile($imageRef) !== false) { - return $this->resource; - } elseif ($this->loadFromData($imageRef) !== false) { - return $this->resource; - } - $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. * @@ -550,16 +538,6 @@ class OC_Image implements \OCP\IImage { * @return bool|resource An image resource or false on error */ public function loadFromFile($imagePath = false) { - try { - // detect if it is a path or maybe the images as string - // needed because the constructor iterates over all load* methods - $result = @realpath($imagePath); - if ($result === false) { - return false; - } - } catch (Error $e) { - return 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)) { return false; |