Last active
October 28, 2023 06:09
-
-
Save neverything/9901b0912a4d9323b62f7b32e6801207 to your computer and use it in GitHub Desktop.
Check out the post on https://silvanhagen.com/social-share-images-with-laravel/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use App\Models\Project; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Storage; | |
use Spatie\Browsershot\Browsershot; | |
class GenerateOpenGraphImage extends Command | |
{ | |
protected $signature = 'open-graph-image:generate {project?}'; | |
protected $description = 'Creates a screenshot for each project to be used as an open graph image'; | |
public function handle() | |
{ | |
$project = $this->argument('project'); | |
if (! $project) { | |
$projects = Project::published()->get(); | |
$this->info("Generating open graph images for {$projects->count()} projects..."); | |
$projects->each(function (Project $project) { | |
$this->generateOpenGraphImage($project); | |
}); | |
} else { | |
$project = Project::published()->where('slug', $project)->firstOrFail(); | |
$this->generateOpenGraphImage($project); | |
} | |
$this->info('All done!'); | |
return 0; | |
} | |
private function generateOpenGraphImage(Project $project): void | |
{ | |
$this->info("Generating open graph image for project {$project->name}..."); | |
Browsershot::url(route('project.share-image', $project)) | |
->windowSize(1200, 630) | |
->deviceScaleFactor(2) | |
->save(Storage::disk('public')->path("share/{$project->slug}.png")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment