Skip to content

Commit

Permalink
Added cover image functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagof committed Nov 3, 2020
1 parent eda0833 commit 31362ba
Show file tree
Hide file tree
Showing 12 changed files with 18,015 additions and 24,003 deletions.
40 changes: 40 additions & 0 deletions app/Http/Crawlers/OpenGraphMetaCrawler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Crawlers;

use Illuminate\Support\Str;

class OpenGraphMetaCrawler
{
protected ?string $content = null;
protected ?\DOMDocument $doc = null;
protected ?\DOMXPath $xPath = null;

public function crawl(string $link): self
{
$this->content = file_get_contents($link);

$this->doc = new \DOMDocument();
libxml_use_internal_errors(true);
$this->doc->loadHTML($this->content);
libxml_clear_errors();

$this->xPath = new \DOMXPath($this->doc);

return $this;
}

public function getOGImage(): ?string
{
$imgUrl = optional($this->xPath->query('//head/meta[@property="og:image"]/@content')[0])
->value;

if (!$imgUrl) {
return null;
}

return Str::startsWith($imgUrl, '/')
? 'http://' . trim($imgUrl, '/')
: $imgUrl;
}
}
70 changes: 61 additions & 9 deletions app/Http/Livewire/SubmitLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
namespace App\Http\Livewire;

use App\Http\Clients\ApiClient;
use App\Http\Crawlers\OpenGraphMetaCrawler;
use App\Rules\UniqueLink;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithFileUploads;
use Spatie\Browsershot\Browsershot;

class SubmitLink extends Component
{
use WithFileUploads;

protected $client;
protected ApiClient $client;
protected OpenGraphMetaCrawler $crawler;
protected array $config;

public $title;
public $name;
Expand All @@ -24,17 +31,28 @@ class SubmitLink extends Component
public $photo;
public $generatedPhoto;

public function __construct($id = null)
{
parent::__construct($id);

$this->crawler = new OpenGraphMetaCrawler();
$this->config = config('laravel-portugal.links');
$this->client = resolve(ApiClient::class);
}

public function mount(): void
{
$client = resolve(ApiClient::class);
$this->avaliableTags = $client->getTags();
$this->avaliableTags = $this->client->getTags();
}

public function updatedWebsite(): void
{
$this->validate(['website' => $this->getRules()['website']]);
// TODO: generate the cover_photo from the url. For now just do:
$this->generatedPhoto = 'https://picsum.photos/200/300';
}

public function generateCoverImage(): void
{
$this->generatedPhoto = $this->getOGImage() ?? $this->getBrowserShotImage();
}

public function clearPhoto(): void
Expand All @@ -48,10 +66,7 @@ public function submit(): void

$this->tags = collect($this->tags)->filter()->all();

/** @var \App\Http\Clients\ApiClient $client */
$client = resolve(ApiClient::class);

$this->response = $client->submitLink([
$this->response = $this->client->submitLink([
'title' => $this->title,
'author_name' => $this->name,
'author_email' => $this->email,
Expand All @@ -77,4 +92,41 @@ protected function getRules()
'tags' => 'required',
];
}

protected function getOGImage(): ?string
{
return $this->crawler
->crawl($this->website)
->getOGImage();
}

protected function getBrowserShotImage(): ?string
{
$targetFile = $this->config['storage']['path'] . '/' . uniqid() . '.' . $this->config['cover_image']['format'];
$targetPath = Storage::disk('public')
->path($targetFile);

$img = null;
try {
Storage::disk('public')
->makeDirectory($this->config['storage']['path']);

Browsershot::url($this->website)
->dismissDialogs()
->ignoreHttpsErrors()
->setScreenshotType(
$this->config['cover_image']['format'],
$this->config['cover_image']['quality']
)
->windowSize(
$this->config['cover_image']['size']['w'],
$this->config['cover_image']['size']['h']
)
->save($targetPath);

return URL::to($targetFile);
} catch (\Exception $exception) {
return null;
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"laravel/sanctum": "^2.6",
"laravel/tinker": "^2.0",
"laravolt/avatar": "^4.0",
"livewire/livewire": "^2.0"
"livewire/livewire": "^2.0",
"spatie/browsershot": "^3.40"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.8",
Expand Down
Loading

0 comments on commit 31362ba

Please sign in to comment.