Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Created August 27, 2018 11:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cannonb4ll/91f61dd92026a19921073bf07b8e8521 to your computer and use it in GitHub Desktop.
Save Cannonb4ll/91f61dd92026a19921073bf07b8e8521 to your computer and use it in GitHub Desktop.
Laravel Get ScreenshotJob
<?php
namespace App\Jobs\Projects;
use App\Models\Project;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Spatie\Browsershot\Browsershot;
class GetSiteScreenshot implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $project;
/**
* Create a new job instance.
*
* @param \App\Models\Project $project
*/
public function __construct(Project $project)
{
$this->project = $project;
}
/**
* Execute the job.
*
* @return void
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
*/
public function handle()
{
try {
Browsershot::url($this->project->url)
->setScreenshotType('jpeg', 25)
->windowSize(1280, 720)
->waitUntilNetworkIdle()
->timeout(10)
->save(md5($this->project->title) . '.jpg');
$this->project->clearMediaCollection('projectSiteScreenshot');
$this->project
->addMedia(base_path(md5($this->project->title) . '.jpg'))
->toMediaCollection('projectSiteScreenshot');
} catch (\Exception $e) {
info($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment