Skip to content

Commit

Permalink
more help function
Browse files Browse the repository at this point in the history
  • Loading branch information
caothu159 committed Jun 23, 2024
1 parent b4cf7b6 commit f069d2d
Showing 1 changed file with 66 additions and 3 deletions.
69 changes: 66 additions & 3 deletions Model/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author Tran Ngoc Duc <ductn@diepxuan.com>
* @author Tran Ngoc Duc <caothu91@gmail.com>
*
* @lastupdate 2024-06-22 18:56:17
* @lastupdate 2024-06-23 10:33:42
*/

namespace Diepxuan\Images\Model;
Expand All @@ -18,11 +18,27 @@ class Extension
/**
* @var array
*/
protected $allowedMimeTypes = [
'svg' => 'image/svg+xml',
protected $webMimeTypes = [
'webp' => 'image/webp',
];

/**
* @var array
*/
protected $vectorMimeTypes = [
'svg' => 'image/svg+xml',
];

/**
* @var array
*/
protected $allowedMimeTypes = [];

public function __construct(
) {
$this->allowedMimeTypes = array_merge($this->webMimeTypes, $this->vectorMimeTypes);
}

/**
* Getter for allowed extensions of uploaded files.
*
Expand All @@ -32,4 +48,51 @@ public function getAllowedExtensions()
{
return array_keys($this->allowedMimeTypes);
}

public function getWebExtensions()
{
return array_keys($this->webMimeTypes);
}

public function getVectorExtensions()
{
return array_keys($this->vectorMimeTypes);
}

/**
* File is a vector image.
*
* @param mixed $filePath
*
* @return bool
*/
public function isVectorImage($filePath)
{
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if (empty($extension) && file_exists($filePath)) {
$mimeType = mime_content_type($filePath);
$extension = str_replace('image/', '', $mimeType);
}

$allowed = \in_array($extension, $this->getVectorExtensions(), true);
if (!$allowed) {
return false;
}

try {
$xmlReader = new \XMLReader();
$xmlReader->open($filePath);
if (\XMLReader::ELEMENT === $xmlReader->moveToElement() && 'svg' === strtolower($xmlReader->name)) {
return true;
}

return false;
} catch (\Throwable $th) {
// throw $th;

return false;
} finally {
$xmlReader->close();
}
}
}

0 comments on commit f069d2d

Please sign in to comment.