From 7fc3f8ef9f4730c145736ed4ced6fa57b04f93dc Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Sun, 17 Oct 2021 14:27:53 +0200 Subject: [PATCH] Obtain the correct image type from buffered images Signed-off-by: Christian Wolf --- lib/private/legacy/OC_Image.php | 44 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index 1e00b606ea0..9eb9c2b8dcb 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -662,6 +662,42 @@ class OC_Image implements \OCP\IImage { return $this->resource; } + /** + * Get the correct file type from a buffered image + * + * @param string $buffer The image data to analyze + * @return void + */ + private function updateImageTypes($buffer) { + if ($this->valid()) { + $finfo = new finfo(FILEINFO_MIME_TYPE); + $this->mimeType = $finfo->buffer($buffer); + switch ($this->mimeType) { + case 'image/gif': + $this->imageType = IMAGETYPE_GIF; + break; + case 'image/jpeg': + $this->imageType = IMAGETYPE_JPEG; + break; + case 'image/png': + $this->imageType = IMAGETYPE_PNG; + break; + case 'image/xbm': + $this->imageType = IMAGETYPE_XBM; + break; + case 'image/vnd.wap.wbmp': + $this->imageType = IMAGETYPE_WBMP; + break; + case 'image/bmp': + $this->imageType = IMAGETYPE_BMP; + break; + case 'image/webp': + $this->imageType = IMAGETYPE_WEBP; + break; + } + } + } + /** * Loads an image from a string of data. * @@ -673,13 +709,11 @@ class OC_Image implements \OCP\IImage { return false; } $this->resource = @imagecreatefromstring($str); - if ($this->fileInfo) { - $this->mimeType = $this->fileInfo->buffer($str); - } if (is_resource($this->resource)) { imagealphablending($this->resource, false); imagesavealpha($this->resource, true); } + $this->updateImageTypes($str); if (!$this->resource) { $this->logger->debug('OC_Image->loadFromFile, could not load', ['app' => 'core']); @@ -701,9 +735,7 @@ class OC_Image implements \OCP\IImage { $data = base64_decode($str); if ($data) { // try to load from string data $this->resource = @imagecreatefromstring($data); - if ($this->fileInfo) { - $this->mimeType = $this->fileInfo->buffer($data); - } + $this->updateImageTypes($data); if (!$this->resource) { $this->logger->debug('OC_Image->loadFromBase64, could not load', ['app' => 'core']); return false;