-
-
Notifications
You must be signed in to change notification settings - Fork 829
/
UploadFaviconController.php
56 lines (46 loc) · 1.7 KB
/
UploadFaviconController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Foundation\ValidationException;
use Flarum\Locale\TranslatorInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Psr\Http\Message\UploadedFileInterface;
class UploadFaviconController extends UploadImageController
{
protected string $filePathSettingKey = 'favicon_path';
protected string $filenamePrefix = 'favicon';
public function __construct(
SettingsRepositoryInterface $settings,
Factory $filesystemFactory,
protected TranslatorInterface $translator,
protected ImageManager $imageManager
) {
parent::__construct($settings, $filesystemFactory);
}
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
{
$this->fileExtension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
if ($this->fileExtension === 'ico') {
// @todo remove in 2.0
throw new ValidationException([
'message' => strtr($this->translator->trans('validation.mimes'), [
':attribute' => 'favicon',
':values' => 'jpeg,png,gif,webp',
])
]);
}
$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))
->scale(64, 64)
->toPng();
$this->fileExtension = 'png';
return $encodedImage;
}
}