Created
March 17, 2020 05:20
-
-
Save vanpariyar/be6d2e94d34ad5c1c53a9eec7b8eba58 to your computer and use it in GitHub Desktop.
Get the thumbnail image of Youtube by Youtube Video URL.
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
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