generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageGeneratorCommand.php
94 lines (72 loc) · 3.52 KB
/
ImageGeneratorCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace FreshbitsWeb\ImageGenerator\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
class ImageGeneratorCommand extends Command
{
public $signature = 'generate:image {--name=} {--width=} {--height=} {--bgColor=} {--textColor=}';
public $description = 'This command lets you generate images.';
public function handle(): int
{
[$name, $width, $height, $bgColor, $textColor] = $this->promptForImageDetails();
if ($this->validateTheArgument($name, $width, $height)) {
return static::INVALID;
}
$html = $this->renderView($name, $bgColor, $textColor);
$this->generateImage($name, $width, $height, $html);
$this->successMessage($name);
return static::SUCCESS;
}
private function promptForImageDetails(): array
{
$name = $this->option('name') ?: $this->ask('What is the name of the image?', 'Hello world!!');
$width = (int) $this->option('width') ?: (int) $this->ask('What is the width of the image?', config('image-generator.width'));
$height = (int) $this->option('height') ?: (int) $this->ask('What is the height of the image?', config('image-generator.height'));
$bgColor = $this->option('bgColor') ?: $this->ask('What is the background color of the image?', config('image-generator.background_color'));
$textColor = $this->option('textColor') ?: $this->ask('What is the text color of the image?', config('image-generator.text_color'));
return [$name, $width, $height, $bgColor, $textColor];
}
private function validateTheArgument(string $name, int $width, int $height): bool
{
if ($width <= 0) {
$this->newLine();
$this->line('<options=bold,reverse;fg=red>Oops! 😔 The width must be greater than 0. Please try again.</>');
return true;
}
if ($height <= 0) {
$this->newLine();
$this->line('<options=bold,reverse;fg=red>Oops! 😔 The height must be greater than 0. Please try again.</>');
return true;
}
if (Storage::exists("public/$name.png")) {
$this->newLine();
$this->line("<options=bold,reverse;fg=red> WHOOPS! </> 😳 \n");
$this->line('<fg=red;options=bold>File is reserved:</>'.Storage::path("public/$name.png"));
return true;
}
return false;
}
private function generateImage(string $name, int $width, int $height, string $html): void
{
Browsershot::html($html)
->windowSize($width, $height)
->save(Storage::path("public/$name.png"));
}
private function successMessage(string $name)
{
$this->info('╭───────────────────────────────────────────╮');
$this->info('│'.str_pad(' IMAGE CREATED 🤙', 43, ' ', STR_PAD_BOTH).' │');
$this->info('╰───────────────────────────────────────────╯');
$this->newLine();
$this->line('<options=bold;fg=green>IMAGE PATH:</> '.Storage::path("public/$name.png"));
}
private function renderView(string $name, string $bgColor, string $textColor)
{
return view('image-generator-for-laravel::theme', [
'name' => $name,
'backgroundColor' => $bgColor,
'textColor' => $textColor,
])->render();
}
}