mirror of
https://github.com/flarum/framework.git
synced 2024-11-28 03:32:49 +08:00
Refactor MIME type guessing to work without any PHP extension
Closes #1241.
This commit is contained in:
parent
34588a74e2
commit
ba96f311a9
|
@ -37,6 +37,13 @@ use Illuminate\Support\Str;
|
||||||
*/
|
*/
|
||||||
class Extension implements Arrayable
|
class Extension implements Arrayable
|
||||||
{
|
{
|
||||||
|
const LOGO_MIMETYPES = [
|
||||||
|
'svg' => 'image/svg+xml',
|
||||||
|
'png' => 'image/png',
|
||||||
|
'jpeg' => 'image/jpeg',
|
||||||
|
'jpg' => 'image/jpeg',
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unique Id of the extension.
|
* Unique Id of the extension.
|
||||||
*
|
*
|
||||||
|
@ -176,23 +183,29 @@ class Extension implements Arrayable
|
||||||
*/
|
*/
|
||||||
public function getIcon()
|
public function getIcon()
|
||||||
{
|
{
|
||||||
if (($icon = $this->composerJsonAttribute('extra.flarum-extension.icon'))) {
|
$icon = $this->composerJsonAttribute('extra.flarum-extension.icon');
|
||||||
if ($file = Arr::get($icon, 'image')) {
|
$file = Arr::get($icon, 'image');
|
||||||
|
|
||||||
|
if (is_null($icon) || is_null($file)) {
|
||||||
|
return $icon;
|
||||||
|
}
|
||||||
|
|
||||||
$file = $this->path.'/'.$file;
|
$file = $this->path.'/'.$file;
|
||||||
|
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
$mimetype = pathinfo($file, PATHINFO_EXTENSION) === 'svg'
|
$extension = pathinfo($file, PATHINFO_EXTENSION);
|
||||||
? 'image/svg+xml'
|
if (!array_key_exists($extension, self::LOGO_MIMETYPES)) {
|
||||||
: finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
|
throw new \RuntimeException('Invalid image type');
|
||||||
$data = file_get_contents($file);
|
|
||||||
|
|
||||||
$icon['backgroundImage'] = 'url(\'data:'.$mimetype.';base64,'.base64_encode($data).'\')';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$mimetype = self::LOGO_MIMETYPES[$extension];
|
||||||
|
$data = base64_encode(file_get_contents($file));
|
||||||
|
|
||||||
|
$icon['backgroundImage'] = "url('data:$mimetype;base64,$data')";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $icon;
|
return $icon;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $enabled
|
* @param bool $enabled
|
||||||
|
|
Loading…
Reference in New Issue
Block a user