Created
August 27, 2018 11:43
-
-
Save Cannonb4ll/91f61dd92026a19921073bf07b8e8521 to your computer and use it in GitHub Desktop.
Laravel Get ScreenshotJob
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\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