|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Actions\FontAwesome\v5; |
| 4 | + |
| 5 | +use App\Helpers\Encoders\FontAwesome\v5\FontAwesomeV5Encoder; |
| 6 | +use Intervention\Image\Facades\Image; |
| 7 | +use Lorisleiva\Actions\Concerns\AsAction; |
| 8 | + |
| 9 | +class CreatePin |
| 10 | +{ |
| 11 | + use AsAction; |
| 12 | + |
| 13 | + public function handle(array $params, $scale = 1.0, $forceOddSize = false) |
| 14 | + { |
| 15 | + // OPEN ICON DEFAULT IMAGE |
| 16 | + $img = file_get_contents(resource_path('images/icon.png')); |
| 17 | + $img = Image::make($img); |
| 18 | + |
| 19 | + // GET ICON |
| 20 | + $icon = data_get($params, 'icon') ?: null; |
| 21 | + $text = data_get($params, 'text') ?: null; |
| 22 | + $background = data_get($params, 'background') ?: null; |
| 23 | + |
| 24 | + // RESIZE THE ICON |
| 25 | + $size = (data_get($params, 'size') ?: 40) * $scale; |
| 26 | + if ($forceOddSize && $size % 2 == 0) { |
| 27 | + $size++; |
| 28 | + } |
| 29 | + |
| 30 | + $iconSize = data_get($params, 'iconSize') ?: ($size / 3.2); |
| 31 | + $voffset = (data_get($params, 'voffset') ?: 0); |
| 32 | + $hoffset = (data_get($params, 'hoffset') ?: 0); |
| 33 | + |
| 34 | + $img->resize($size, $size, function ($constraint) { |
| 35 | + $constraint->aspectRatio(); |
| 36 | + }); |
| 37 | + |
| 38 | + // DRAW THE PIN |
| 39 | + $pin_color = $background ?: 'EF5646'; |
| 40 | + $img->text($this->getUnicodeCharFromIcon('fa-map-marker'), $size / 2.0, $size / 2.0, function ($font) use ($icon, $size, $pin_color) { |
| 41 | + $font->file($this->getIconFontPath($icon)); |
| 42 | + $font->size($size * 0.90); |
| 43 | + $font->color($pin_color); |
| 44 | + $font->align('center'); |
| 45 | + $font->valign('center'); |
| 46 | + }); |
| 47 | + |
| 48 | + if ($icon) { |
| 49 | + $img->text($this->getUnicodeCharFromIcon($icon), ($img->width() / 2) + $hoffset, ($img->height() / 4) + $voffset, function ($font) use ($params, $icon, $iconSize) { |
| 50 | + $font->file($this->getIconFontPath($icon)); |
| 51 | + $font->size((int) $iconSize); |
| 52 | + $font->color(data_get($params, 'color') ?: '#FFFFFF'); |
| 53 | + $font->align('center'); |
| 54 | + $font->valign('top'); |
| 55 | + }); |
| 56 | + } elseif (data_get($params, 'text')) { |
| 57 | + $img->text($text, ($img->width() / 2) + $hoffset, ($img->height() / 4) + $voffset, function ($font) use ($params, $size) { |
| 58 | + $font->file($this->getTextFontPath()); |
| 59 | + $font->size((int) ($size / 3.2)); |
| 60 | + $font->color(data_get($params, 'color') ?: '#FFFFFF'); |
| 61 | + $font->align('center'); |
| 62 | + $font->valign('top'); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + $label = data_get($params, 'label') ?: null; |
| 67 | + $labelOffset = data_get($params, 'labelOffset') ?: 0; |
| 68 | + if (! is_null($label)) { |
| 69 | + |
| 70 | + // ADD THE LABEL |
| 71 | + $img->text(FontAwesomeV5Encoder::getUnicodeFromIcon('fa-circle'), $size * 0.8, $size * 0.8, function ($font) use ($params, $size) { |
| 72 | + $font->file($this->getIconFontPath('fa-circle')); |
| 73 | + $font->size($size * 0.44); |
| 74 | + $font->color(data_get($params, 'labelColor') ?: 'D9534F'); |
| 75 | + $font->align('center'); |
| 76 | + $font->valign('center'); |
| 77 | + }); |
| 78 | + |
| 79 | + // ADD THE LABEL TEXT |
| 80 | + $img->text($label, $size * 0.8 + $labelOffset, $size * 0.8, function ($font) use ($params, $size) { |
| 81 | + $font->file($this->getTextFontPath()); |
| 82 | + $font->size($size * 0.25); |
| 83 | + $font->color(data_get($params, 'labelTextColor') ?: 'FFFFFF'); |
| 84 | + $font->align('center'); |
| 85 | + $font->valign('center'); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + return $img; |
| 90 | + } |
| 91 | + |
| 92 | + protected function getIconFontPath($icon) |
| 93 | + { |
| 94 | + return FontAwesomeV5Encoder::getFile($icon); |
| 95 | + } |
| 96 | + |
| 97 | + protected function getTextFontPath() |
| 98 | + { |
| 99 | + return base_path('resources/fonts/monofonto.ttf'); |
| 100 | + } |
| 101 | + |
| 102 | + protected function getUnicodeCharFromIcon($icon) |
| 103 | + { |
| 104 | + return FontAwesomeV5Encoder::getUnicodeFromIcon($icon); |
| 105 | + } |
| 106 | +} |
0 commit comments