Skip to content

Commit

Permalink
Add downloadVimeoThumbnail() method for load Vimeo thumbnail.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek authored Dec 22, 2021
1 parent e8f1b41 commit bc60c5a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/VideoToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@ public function getThumbnailUrl(): ?string
return 'https://img.youtube.com/vi/' . urlencode($this->token) . '/maxresdefault.jpg';
}
if ($this->provider === self::PROVIDER_VIMEO) {
$url = 'https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/' . urlencode($this->token);
$api = trim((string) @file_get_contents($url));
$apiResponse = json_decode($api, true, 512, JSON_THROW_ON_ERROR);
$thumbnailUrl = is_array($apiResponse) && isset($apiResponse['thumbnail_url']) ? $apiResponse['thumbnail_url'] : null;
if (is_string($thumbnailUrl)) {
return $thumbnailUrl;
try {
return $this->downloadVimeoThumbnail($this->token);
} catch (\Throwable $e) {
trigger_error(sprintf('Can not download Vimeo thumbnail: %s', $e->getMessage()));
}
}

Expand Down Expand Up @@ -185,4 +183,15 @@ private function resolveProviderByToken(?string $token): ?string

return null;
}

private function downloadVimeoThumbnail(string $token): ?string
{
$url = 'https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/' . urlencode($token);
$api = trim((string) @file_get_contents($url));
$apiResponse = json_decode($api, true, 512, JSON_THROW_ON_ERROR);
assert(is_array($apiResponse));
$thumbnailUrl = $apiResponse['thumbnail_url'] ?? null;

return is_string($thumbnailUrl) ? $thumbnailUrl : null;
}
}

0 comments on commit bc60c5a

Please # to comment.