From 8f5e0ad4a2a7419f41ee5356e55a092169ac0fab Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Mon, 9 Oct 2023 13:21:45 +0200 Subject: [PATCH] Generate image without blade directive (#17) * Create image from params * Fix styling * Add test * Fix styling * Remove test as it's not working at the moment * Mark test skipped --------- Co-authored-by: Baspa --- src/OpenGraphImage.php | 26 ++++++++++++++++++++++++++ tests/OpenGraphTest.php | 15 +++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/OpenGraphTest.php diff --git a/src/OpenGraphImage.php b/src/OpenGraphImage.php index 1dec5cb..c142062 100644 --- a/src/OpenGraphImage.php +++ b/src/OpenGraphImage.php @@ -2,7 +2,10 @@ namespace Vormkracht10\LaravelOpenGraphImage; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Facades\View; use Illuminate\View\ComponentAttributeBag; +use Vormkracht10\LaravelOpenGraphImage\Http\Controllers\LaravelOpenGraphImageController; class OpenGraphImage { @@ -24,4 +27,27 @@ public function url(array|ComponentAttributeBag $parameters): string return url() ->signedRoute('open-graph-image.file', $parameters); } + + public function createImageFromParams(array $params): string + { + $url = $this->url($params); + + $url = parse_url($url); + + parse_str($url['query'], $params); + + $signature = $params['signature']; + + $imageController = new LaravelOpenGraphImageController; + + if (! $imageController->getStorageFileExists($signature)) { + $html = View::make('open-graph-image::template', $params) + ->render(); + + $imageController->saveOpenGraphImage($html, $signature); + } + + return Storage::disk(config('open-graph-image.storage.disk')) + ->get($imageController->getStorageFilePath($signature)); + } } diff --git a/tests/OpenGraphTest.php b/tests/OpenGraphTest.php new file mode 100644 index 0000000..be65066 --- /dev/null +++ b/tests/OpenGraphTest.php @@ -0,0 +1,15 @@ +markTestSkipped('Pest is not configured correctly yet.'); + + $image = OpenGraphImage::createImageFromParams([ + 'title' => 'title', + 'description' => 'description', + ]); + + expect($image)->toBeString(); +});