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