Obtain the correct image type from buffered images

Signed-off-by: Christian Wolf <github@christianwolf.email>
fix/wrong-image-type
Christian Wolf 3 years ago
parent 9a809df960
commit 7fc3f8ef9f

@ -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;

Loading…
Cancel
Save