|
2 | 2 |
|
3 | 3 | namespace App\Http\Controllers\API\v2\FontAwesome\v5; |
4 | 4 |
|
| 5 | +use App\Actions\FontAwesome\v5\CreateIconStack; |
5 | 6 | use App\Actions\Image\ResizeToHeight; |
6 | 7 | use App\Actions\Image\TrimTransparentBorder; |
7 | | -use App\Helpers\Encoders\FontAwesome\v5\FontAwesomeV5Encoder; |
8 | 8 | use App\Http\Controllers\Controller as Controller; |
9 | 9 | use Illuminate\Http\Request; |
10 | | -use Intervention\Image\Facades\Image; |
11 | 10 |
|
12 | 11 | class IconStackController extends Controller |
13 | 12 | { |
14 | | - public function show(Request $request) |
15 | | - { |
16 | | - $params = $request->all(); |
17 | | - $size = data_get($params, 'size') ?: 30; |
18 | | - $finalSize = $size; |
19 | | - |
20 | | - // calculate temp size |
21 | | - // up 2x and make odd for better auto-centering |
22 | | - $size = round($size * 2.0); |
23 | | - if ($size % 2 == 0) { |
24 | | - $size += 1; |
25 | | - } |
26 | | - $params['size'] = $size; |
27 | | - |
28 | | - // OPEN ICON DEFAULT IMAGE |
29 | | - $img = file_get_contents(resource_path('images/icon.png')); |
30 | | - $img = Image::make($img); |
31 | | - |
32 | | - // INPUTS |
33 | | - $icon = data_get($params, 'icon') ?: null; |
34 | | - $on = data_get($params, 'on') ?: $this->getDefaultOnIcon(); |
35 | | - |
36 | | - // RESIZE THE ICON |
37 | | - $iconSize = data_get($params, 'iconsize') * 2.0 ?: ($size * 0.3); |
38 | | - $voffset = data_get($params, 'voffset') * 2.0 ?: 0; |
39 | | - $hoffset = data_get($params, 'hoffset') * 2.0 ?: 0; |
40 | | - |
41 | | - $img->resize($size, $size, function ($constraint) { |
42 | | - $constraint->aspectRatio(); |
43 | | - }); |
| 13 | + const SCALE = 2.0; |
44 | 14 |
|
45 | | - $large_color = (data_get($params, 'oncolor') ?: data_get($params, 'color')) ?: '000000'; |
46 | | - $img->text($this->getUnicodeCharFromIcon($on), $size / 2, $size / 2, function ($font) use ($on, $size, $large_color) { |
47 | | - $font->file($this->getIconFontPath($on)); |
48 | | - $font->size($size * 0.8); |
49 | | - $font->color($large_color); |
50 | | - $font->align('center'); |
51 | | - $font->valign('center'); |
52 | | - }); |
53 | | - |
54 | | - $small_color = data_get($params, 'color') ?: null; |
55 | | - $img->text($this->getUnicodeCharFromIcon($icon), ($size / 2) + $hoffset, ($size / 2) + $voffset, function ($font) use ($icon, $iconSize, $large_color, $small_color) { |
56 | | - $font->file($this->getIconFontPath($icon)); |
57 | | - $font->size($iconSize); |
58 | | - $font->color($small_color ?: $large_color); |
59 | | - $font->align('center'); |
60 | | - $font->valign('center'); |
61 | | - }); |
62 | | - |
63 | | - $label = data_get($params, 'label') ?: null; |
64 | | - $labelOffset = data_get($params, 'labelOffset') ?: 0; |
65 | | - if (! is_null($label)) { |
66 | | - |
67 | | - // ADD THE LABEL |
68 | | - $img->text($this->getUnicodeCharFromIcon('fa-circle'), $size * 0.8, $size * 0.8, function ($font) use ($size, $params) { |
69 | | - $font->file($this->getIconFontPath('fa-circle')); |
70 | | - $font->size($size * 0.44); |
71 | | - $font->color(data_get($params, 'labelColor') ?: 'D9534F'); |
72 | | - $font->align('center'); |
73 | | - $font->valign('center'); |
74 | | - }); |
75 | | - |
76 | | - // ADD THE LABEL TEXT |
77 | | - $img->text($label, $size * 0.8 + $labelOffset, $size * 0.8, function ($font) use ($size, $params) { |
78 | | - $font->file($this->getTextFontPath()); |
79 | | - $font->size($size * 0.25); |
80 | | - $font->color(data_get($params, 'labelTextColor') ?: 'FFFFFF'); |
81 | | - $font->align('center'); |
82 | | - $font->valign('center'); |
83 | | - }); |
84 | | - } |
| 15 | + const FORCE_ODD_SIZE = true; |
85 | 16 |
|
| 17 | + public function show(Request $request) |
| 18 | + { |
| 19 | + $size = $request->get('size') ?: 30; |
| 20 | + $img = CreateIconStack::run($request->all(), self::SCALE, self::FORCE_ODD_SIZE); |
86 | 21 | TrimTransparentBorder::run($img); |
87 | | - ResizeToHeight::run($img, $finalSize); |
| 22 | + ResizeToHeight::run($img, $size); |
88 | 23 |
|
89 | 24 | return $img->response('png'); |
90 | 25 | } |
91 | | - |
92 | | - protected function getDefaultOnIcon() |
93 | | - { |
94 | | - return 'fa-circle-regular'; |
95 | | - } |
96 | | - |
97 | | - protected function getIconFontPath($icon) |
98 | | - { |
99 | | - return FontAwesomeV5Encoder::getFile($icon); |
100 | | - } |
101 | | - |
102 | | - protected function getTextFontPath() |
103 | | - { |
104 | | - return base_path('resources/fonts/monofonto.ttf'); |
105 | | - } |
106 | | - |
107 | | - protected function getUnicodeCharFromIcon($icon) |
108 | | - { |
109 | | - return FontAwesomeV5Encoder::getUnicodeFromIcon($icon); |
110 | | - } |
111 | 26 | } |
0 commit comments