|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelFavicon\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\LaravelFavicon\Favicon; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
use Intervention\Image\AbstractFont; |
|
8
|
|
|
use Intervention\Image\Gd\Font as GdFont; |
|
9
|
|
|
use Intervention\Image\Image; |
|
10
|
|
|
use Intervention\Image\ImageManager; |
|
11
|
|
|
use Intervention\Image\Imagick\Font as ImagickFont; |
|
12
|
|
|
|
|
13
|
|
|
class EnvironmentGenerator implements FaviconGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var Favicon */ |
|
16
|
|
|
protected $favicon; |
|
17
|
|
|
|
|
18
|
|
|
/** @var ImageManager */ |
|
19
|
|
|
protected $manager; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $environment; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Favicon $favicon) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->favicon = $favicon; |
|
27
|
|
|
|
|
28
|
|
|
$this->manager = new ImageManager([ |
|
29
|
|
|
'driver' => config('favicon.image_driver'), |
|
30
|
|
|
]); |
|
31
|
|
|
|
|
32
|
|
|
$this->environment = config('app.env'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function generate(string $icon): Response |
|
36
|
|
|
{ |
|
37
|
|
|
$paddingX = config('favicon.padding.x'); |
|
38
|
|
|
$paddingY = config('favicon.padding.y'); |
|
39
|
|
|
|
|
40
|
|
|
$img = $this->manager->make($icon); |
|
41
|
|
|
|
|
42
|
|
|
$font = $this->getFont($this->favicon->getFaviconText($this->environment)); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
$font->file(config('favicon.font') ?? __DIR__.'/../../resources/fonts/OpenSans-Regular.ttf'); |
|
45
|
|
|
|
|
46
|
|
|
$font->valign('top'); |
|
47
|
|
|
|
|
48
|
|
|
$font->color($this->favicon->getFaviconColor($this->environment)); |
|
49
|
|
|
|
|
50
|
|
|
$this->calculateDynamicFontSize($font, $img, $paddingX); |
|
51
|
|
|
|
|
52
|
|
|
$environmentTextImage = $this->createEnvironmentTextImage($font); |
|
53
|
|
|
|
|
54
|
|
|
$output = $this->manager->canvas($img->width(), $img->height()); |
|
55
|
|
|
|
|
56
|
|
|
$output->insert($img); |
|
57
|
|
|
|
|
58
|
|
|
$output->insert($environmentTextImage, 'bottom-right', $paddingX, $paddingY); |
|
59
|
|
|
|
|
60
|
|
|
return $output->response('png'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getFont(string $text): AbstractFont |
|
64
|
|
|
{ |
|
65
|
|
|
if (config('favicon.image_driver') === 'imagick') { |
|
66
|
|
|
return new ImagickFont($text); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new GdFont($text); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function calculateDynamicFontSize(AbstractFont $font, Image $icon, $paddingX) |
|
73
|
|
|
{ |
|
74
|
|
|
$size = $font->getBoxSize(); |
|
75
|
|
|
|
|
76
|
|
|
while ($size['width'] + $paddingX > $icon->width() && $font->getSize() > 0) { |
|
77
|
|
|
$fontSize = $font->getSize(); |
|
78
|
|
|
|
|
79
|
|
|
$font->size($fontSize - 1); |
|
80
|
|
|
|
|
81
|
|
|
$size = $font->getBoxSize(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function createEnvironmentTextImage(AbstractFont $font) |
|
86
|
|
|
{ |
|
87
|
|
|
$size = $font->getBoxSize(); |
|
88
|
|
|
|
|
89
|
|
|
$environmentText = $this->manager->canvas($size['width'], $size['height'], $this->favicon->getFaviconBackgroundColor($this->environment)); |
|
90
|
|
|
|
|
91
|
|
|
$font->applyToImage($environmentText); |
|
92
|
|
|
|
|
93
|
|
|
return $environmentText; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function shouldGenerateFavicon(): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return config('favicon.enabled_environments.'.$this->environment) !== null; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|