Skip to content

Commit 89a3168

Browse files
committed
feat(font awesome v5): added support for icon stacks
1 parent 489ba6d commit 89a3168

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API\v1;
4+
5+
use App\Http\Controllers\Controller as Controller;
6+
use App\Helpers\FontAwesomeEncoder;
7+
use Illuminate\Support\Facades\Request;
8+
use Intervention\Image\Facades\Image as Image;
9+
10+
class FaStackController extends Controller
11+
{
12+
protected function getDefaultOnIcon()
13+
{
14+
return 'fa-circle-o';
15+
}
16+
17+
protected function getIconFontPath($icon)
18+
{
19+
return base_path('resources/fonts/legacy/fontawesome-webfont.ttf');
20+
}
21+
22+
protected function getTextFontPath()
23+
{
24+
return base_path('resources/fonts/legacy/monofonto.ttf');
25+
}
26+
27+
protected function getUnicodeCharFromIcon($icon)
28+
{
29+
return FontAwesomeEncoder::getUnicodeFromIcon($icon);
30+
}
31+
32+
public function show()
33+
{
34+
35+
// OPEN ICON DEFAULT IMAGE
36+
$img = file_get_contents(resource_path('images/icon.png'));
37+
$img = Image::make($img);
38+
39+
// INPUTS
40+
$icon = Request::get('icon') ?: null;
41+
$on = Request::get('on') ?: $this->getDefaultOnIcon();
42+
43+
// RESIZE THE ICON
44+
$size = Request::get('size') ?: 30;
45+
$iconSize = Request::get('iconsize') ?: ($size * 0.3);
46+
$voffset = Request::get('voffset') ?: 0;
47+
$hoffset = Request::get('hoffset') ?: 0;
48+
49+
$img->resize($size, $size, function ($constraint) {
50+
$constraint->aspectRatio();
51+
});
52+
53+
$large_color = (Request::get('oncolor') ?: Request::get('color')) ?: '000000';
54+
$img->text($this->getUnicodeCharFromIcon($on), $size / 2, $size / 2, function ($font) use ($on, $img, $size, $large_color) {
55+
$font->file($this->getIconFontPath($on));
56+
$font->size($size * 0.8);
57+
$font->color($large_color);
58+
$font->align('center');
59+
$font->valign('center');
60+
});
61+
62+
$small_color = Request::get('color') ?: null;
63+
$img->text($this->getUnicodeCharFromIcon($icon), ($size / 2) + $hoffset, ($size / 2) + $voffset, function ($font) use ($icon, $img, $size, $iconSize, $large_color, $small_color) {
64+
$font->file($this->getIconFontPath($icon));
65+
$font->size($iconSize);
66+
$font->color($small_color ?: $large_color);
67+
$font->align('center');
68+
$font->valign('center');
69+
});
70+
71+
$label = Request::get('label') ?: null;
72+
$labelOffset = Request::get('labelOffset') ?: 0;
73+
if (!is_null($label)) {
74+
75+
// ADD THE LABEL
76+
$img->text($this->getUnicodeCharFromIcon('fa-circle'), $size * 0.8, $size * 0.8, function ($font) use ($img, $size) {
77+
$font->file($this->getIconFontPath('fa-circle'));
78+
$font->size($size * 0.44);
79+
$font->color(Request::get('labelColor') ?: 'D9534F');
80+
$font->align('center');
81+
$font->valign('center');
82+
});
83+
84+
// ADD THE LABEL TEXT
85+
$img->text($label, $size * 0.8 + $labelOffset, $size * 0.8, function ($font) use ($img, $size) {
86+
$font->file($this->getTextFontPath());
87+
$font->size($size * 0.25);
88+
$font->color(Request::get('labelTextColor') ?: 'FFFFFF');
89+
$font->align('center');
90+
$font->valign('center');
91+
});
92+
}
93+
94+
return $img->response('png');
95+
}
96+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\API\v1\FontAwesome\v5;
4+
5+
use App\Http\Controllers\API\v1\FaStackController as Controller;
6+
use App\Helpers\Encoders\FontAwesome\v5\FontAwesomeV5Encoder;
7+
8+
class IconStackController extends Controller
9+
{
10+
protected function getDefaultOnIcon()
11+
{
12+
return 'fa-circle-regular';
13+
}
14+
15+
protected function getIconFontPath($icon)
16+
{
17+
return FontAwesomeV5Encoder::getFile($icon);
18+
}
19+
20+
protected function getTextFontPath()
21+
{
22+
return base_path('resources/fonts/monofonto.ttf');
23+
}
24+
25+
protected function getUnicodeCharFromIcon($icon)
26+
{
27+
return FontAwesomeV5Encoder::getUnicodeFromIcon($icon);
28+
}
29+
}

routes/web.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// LEGACY
3232
// Route::get('pin', 'PinController@show');
3333
Route::get('fa', 'FaController@show');
34-
// Route::get('fa/stack', 'FaStackController@show');
34+
Route::get('fa/stack', 'FaStackController@show');
3535

3636
// FONT-AWESOME
3737
Route::group(['prefix' => 'font-awesome', 'namespace' => 'FontAwesome'], function () {
@@ -46,7 +46,7 @@
4646
Route::group(['prefix' => 'v5', 'namespace' => 'v5'], function () {
4747
Route::get('pin', 'PinController@show');
4848
Route::get('icon', 'IconController@show');
49-
// Route::get('icon-stack', 'IconStackController@show');
49+
Route::get('icon-stack', 'IconStackController@show');
5050
});
5151
});
5252
});

0 commit comments

Comments
 (0)