diff --git a/src/Plugin/video_embed_field/Provider/YouTube.php b/src/Plugin/video_embed_field/Provider/YouTube.php index aa11aea..2643e5a 100644 --- a/src/Plugin/video_embed_field/Provider/YouTube.php +++ b/src/Plugin/video_embed_field/Provider/YouTube.php @@ -60,28 +60,38 @@ class YouTube extends ProviderPluginBase { protected function getLanguagePreference() { preg_match('/[&\?]hl=(?[a-z\-]*)/', $this->getInput(), $matches); return isset($matches['language']) ? $matches['language'] : FALSE; } /** * {@inheritdoc} */ public function getRemoteThumbnailUrl() { $url = 'http://img.youtube.com/vi/%s/%s.jpg'; - $high_resolution = sprintf($url, $this->getVideoId(), 'maxresdefault'); - $backup = sprintf($url, $this->getVideoId(), 'mqdefault'); - try { - $this->httpClient->head($high_resolution); - return $high_resolution; - } - catch (\Exception $e) { - return $backup; + $resolutions = [ + 'maxresdefault', + 'hqdefault', + 'mqdefault', + 'sddefault', + 'default', + ]; + + foreach ($resolutions as $res) { + $thumbnail_url = sprintf($url, $this->getVideoId(), $res); + + try { + $this->httpClient->head($thumbnail_url); + return $thumbnail_url; + } + catch (\Exception $e) { + continue; + } } } /** * {@inheritdoc} */ public static function getIdFromInput($input) { preg_match('/^https?:\/\/(www\.)?((?!.*list=)youtube\.com\/watch\?.*v=|youtu\.be\/)(?[0-9A-Za-z_-]*)/', $input, $matches); return isset($matches['id']) ? $matches['id'] : FALSE; }