Skip to content

Instantly share code, notes, and snippets.

@gdnwebmedia
Last active February 26, 2021 04:30
Show Gist options
  • Save gdnwebmedia/174bef37b50b3227eb0f62a7d6cc8219 to your computer and use it in GitHub Desktop.
Save gdnwebmedia/174bef37b50b3227eb0f62a7d6cc8219 to your computer and use it in GitHub Desktop.
Retrieves the thumbnail from a youtube or vimeo video
<?php
/**
* Retrieves the thumbnail from a youtube or vimeo video
* @param - $src: the url of the "player"
* @return - string
*
**/
function get_video_thumbnail( $src ) {
$url_pieces = explode('/', $src);
if ( $url_pieces[2] == 'vimeo.com' ) { // If Vimeo
$id = $url_pieces[3];
$hash = unserialize(file_get_contents('https://vimeo.com/api/v2/video/' . $id . '.php'));
$thumbnail = $hash[0]['thumbnail_large'];
} elseif ( $url_pieces[2] == 'www.youtube.com' ) { // If Youtube
$extract_id = explode('v=', $url_pieces[3]);
$id = $extract_id[1];
// $thumbnail = 'https://img.youtube.com/vi/' . $id . '/mqdefault.jpg';
$thumbnail = 'https://img.youtube.com/vi/' . $id . '/maxresdefault.jpg';
}
$image = '<img class="" src="'.$thumbnail.'" width="960">';
return $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment