Skip to content

Commit

Permalink
generate image
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavsomaiya committed Mar 17, 2023
1 parent e0f652b commit 21d2a1f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions config/image-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
12 changes: 12 additions & 0 deletions resources/views/theme.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Image Generator</title>
</head>
<body style="background-color: {{ $backgroundColor }}">
<h1 style="text-align: center; color: {{ $textColor }} ">{{ $name }}</h1>
</body>
</html>
48 changes: 22 additions & 26 deletions src/Commands/ImageGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -95,4 +82,13 @@ private function successMessage(string $name)
$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();
}
}
1 change: 1 addition & 0 deletions src/ImageGeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function configurePackage(Package $package): void
$package
->name('image-generator-for-laravel')
->hasConfigFile('image-generator')
->hasViews()
->hasCommand(ImageGeneratorCommand::class);
}
}

0 comments on commit 21d2a1f

Please sign in to comment.