Skip to content

Instantly share code, notes, and snippets.

@vanpariyar
Created March 17, 2020 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanpariyar/be6d2e94d34ad5c1c53a9eec7b8eba58 to your computer and use it in GitHub Desktop.
Save vanpariyar/be6d2e94d34ad5c1c53a9eec7b8eba58 to your computer and use it in GitHub Desktop.
Get the thumbnail image of Youtube by Youtube Video URL.
function get_youtube_thumb($video_url)
{
if(preg_match('/src="([^"]+)"/', $video_url, $match))
$video_url = $match[1];
$ytRegExp = "/^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/";
preg_match($ytRegExp, $video_url, $ytMatch);
$large = "";
if ($ytMatch && strlen($ytMatch[1]) === 11)
{
$youtubeVideoId = $thumbURL = "";
$youtubeVideoId = $ytMatch[1];
if($youtubeVideoId)
$thumbURL = 'https://img.youtube.com/vi/'.$youtubeVideoId.'/0.jpg';
else
$thumbURL = get_template_directory_uri() . '/assets/images/video-placeholder.png';
}
$large = $thumbURL;
return $large;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment