diff options
author | Thomas Tanghus <thomas@tanghus.net> | 2012-02-16 10:35:35 +0100 |
---|---|---|
committer | Thomas Tanghus <thomas@tanghus.net> | 2012-02-16 10:38:00 +0100 |
commit | 0e2531ba57d5264e749f7129909b58518bbc4509 (patch) | |
tree | 2e51c32c13f64aef99df24b9e66b119c77ee3405 /lib/image.php | |
parent | fa7fabf024cf52798a52cd2406a53c333f9ec3ca (diff) | |
download | nextcloud-server-0e2531ba57d5264e749f7129909b58518bbc4509.tar.gz nextcloud-server-0e2531ba57d5264e749f7129909b58518bbc4509.zip |
Added method for loading from file handle.
Diffstat (limited to 'lib/image.php')
-rw-r--r-- | lib/image.php | 47 |
1 files changed, 31 insertions, 16 deletions
diff --git a/lib/image.php b/lib/image.php index b1d3a14f415..fe8349be543 100644 --- a/lib/image.php +++ b/lib/image.php @@ -48,6 +48,11 @@ class OC_Image { protected $imagetype = IMAGETYPE_PNG; // Default to png if file type isn't evident. protected $filepath = null; + /** + * @brief Get mime type for an image file. + * @param $filepath The path to a local image file. + * @returns string The mime type if the it could be determined, otherwise an empty string. + */ static public function getMimeTypeForFile($filepath) { $imagetype = exif_imagetype($filepath); return $imagetype ? image_type_to_mime_type($imagetype) : ''; @@ -281,18 +286,23 @@ class OC_Image { /** * @brief Loads an image from a local file, a base64 encoded string or a resource created by an imagecreate* function. - * @param $imageref The path to a local file, a base64 encoded string or a resource created by an imagecreate* function. + * @param $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 ). * @returns An image resource or false on error */ public function load($imageref) { - if($this->loadFromFile($imageref) !== false) { + if(is_resource($imageref)) { + if(get_resource_type($imageref) == 'gd') { + $this->resource = $res; + return $this->resource; + } elseif(in_array(get_resource_type($imageref), array('file','stream'))) { + return $this->loadFromFileHandle($imageref); + } + } elseif($this->loadFromFile($imageref) !== false) { return $this->resource; } elseif($this->loadFromBase64($imageref) !== false) { return $this->resource; } elseif($this->loadFromData($imageref) !== false) { return $this->resource; - } elseif($this->loadFromResource($imageref) !== false) { - return $this->resource; } else { OC_Log::write('core',__METHOD__.'(): couldn\'t load anything. Giving up!', OC_Log::DEBUG); return false; @@ -300,6 +310,23 @@ class OC_Image { } /** + * @brief 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 $handle + * @returns An image resource or false on error + */ + public function loadFromFileHandle($handle) { + OC_Log::write('core',__METHOD__.'(): Trying', OC_Log::DEBUG); + $contents = ''; + while (!feof($handle)) { + $contents .= fread($handle, 8192); + } + if($this->loadFromData($contents)) { + return $this->resource; + } + } + + /** * @brief Loads an image from a local file. * @param $imageref The path to a local file. * @returns An image resource or false on error @@ -425,18 +452,6 @@ class OC_Image { } /** - * @brief Checks if image resource is valid and assigns it to $this->resource. - * @param $res An image resource. - * @returns An image resource or false on error - */ - public function loadFromResource($res) { - if(!is_resource($res)) { - return false; - } - $this->resource = $res; - } - - /** * @brief Resizes the image preserving ratio. * @param $maxsize The maximum size of either the width or height. * @returns bool |