diff --git a/composer.json b/composer.json
index 26a79e5..6ff7f9e 100644
--- a/composer.json
+++ b/composer.json
@@ -22,9 +22,11 @@
],
"require": {
"php": "^8.0",
- "spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^9.0|^10.0",
- "illuminate/support": "^9.0|^10.0"
+ "illuminate/support": "^9.0|^10.0",
+ "illuminate/view": "^9.0|^10.0",
+ "spatie/browsershot": "^3.57",
+ "spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
diff --git a/config/image-generator.php b/config/image-generator.php
index 7bed7f1..08726aa 100644
--- a/config/image-generator.php
+++ b/config/image-generator.php
@@ -6,4 +6,10 @@
// The default height of the image when not specified.
'height' => 300,
+
+ // The default background color of the image when not specified.
+ 'background_color' => 'green',
+
+ // The default font color of the image when not specified.
+ 'text_color' => 'black',
];
diff --git a/resources/views/theme.blade.php b/resources/views/theme.blade.php
new file mode 100644
index 0000000..32244cf
--- /dev/null
+++ b/resources/views/theme.blade.php
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+ Image Generator
+
+
+ {{ $name }}
+
+
diff --git a/src/Commands/ImageGeneratorCommand.php b/src/Commands/ImageGeneratorCommand.php
index cfe2d49..55a50fa 100644
--- a/src/Commands/ImageGeneratorCommand.php
+++ b/src/Commands/ImageGeneratorCommand.php
@@ -4,22 +4,25 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
+use Spatie\Browsershot\Browsershot;
class ImageGeneratorCommand extends Command
{
- public $signature = 'generate:image {--name=} {--width=} {--height=}';
+ public $signature = 'generate:image {--name=} {--width=} {--height=} {--bgColor=} {--textColor=}';
public $description = 'This command lets you generate images.';
public function handle(): int
{
- [$name, $width, $height] = $this->promptForImageDetails();
+ [$name, $width, $height, $bgColor, $textColor] = $this->promptForImageDetails();
if ($this->validateTheArgument($name, $width, $height)) {
return static::INVALID;
}
- $this->generateImage($name, $width, $height);
+ $html = $this->renderView($name, $bgColor, $textColor);
+
+ $this->generateImage($name, $width, $height, $html);
$this->successMessage($name);
@@ -31,8 +34,10 @@ 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];
+ return [$name, $width, $height, $bgColor, $textColor];
}
private function validateTheArgument(string $name, int $width, int $height): bool
@@ -62,29 +67,11 @@ private function validateTheArgument(string $name, int $width, int $height): boo
return false;
}
- private function generateImage(string $name, int $width, int $height): void
+ private function generateImage(string $name, int $width, int $height, string $html): void
{
- // Step 1: Open the file with write permissions
- $file = @fopen(Storage::path("public/$name.png"), 'w');
-
- $image = @imagecreate($width, $height);
-
- // White background
- @imagecolorallocate($image, 255, 255, 255);
-
- // Black text
- $textColor = @imagecolorallocate($image, 0, 0, 0);
-
- @imagestring($image, 5, 10, 10, $name, $textColor);
-
- // Step 3: Write the image to the file
- @imagepng($image, $file);
-
- // Step 4: Close the file
- @fclose($file);
-
- // Step 6: Free up memory by destroying the image resource
- @imagedestroy($image);
+ Browsershot::html($html)
+ ->windowSize($width, $height)
+ ->save(Storage::path("public/$name.png"));
}
private function successMessage(string $name)
@@ -95,4 +82,13 @@ private function successMessage(string $name)
$this->newLine();
$this->line('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();
+ }
}
diff --git a/src/ImageGeneratorServiceProvider.php b/src/ImageGeneratorServiceProvider.php
index efc3d44..deab8cd 100644
--- a/src/ImageGeneratorServiceProvider.php
+++ b/src/ImageGeneratorServiceProvider.php
@@ -18,6 +18,7 @@ public function configurePackage(Package $package): void
$package
->name('image-generator-for-laravel')
->hasConfigFile('image-generator')
+ ->hasViews()
->hasCommand(ImageGeneratorCommand::class);
}
}