Skip to content

Instantly share code, notes, and snippets.

@secretstache
Last active January 16, 2024 09:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save secretstache/3f55b4bc2ac7d23c516f to your computer and use it in GitHub Desktop.
Save secretstache/3f55b4bc2ac7d23c516f to your computer and use it in GitHub Desktop.
Get Video Thumbnail from Youtube or Vimeo
<?php
/**
* Retrieves the thumbnail from a youtube or vimeo video
* @param - $src: the url of the "player"
* @return - string
* @todo - do some real world testing.
*
**/
function get_video_thumbnail( $src ) {
$url_pieces = explode('/', $src);
if ( $url_pieces[2] == 'player.vimeo.com' ) { // If Vimeo
$id = $url_pieces[4];
$hash = unserialize(file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php'));
$thumbnail = $hash[0]['thumbnail_large'];
} elseif ( $url_pieces[2] == 'www.youtube.com' ) { // If Youtube
$extract_id = explode('?', $url_pieces[4]);
$id = $extract_id[0];
$thumbnail = 'http://img.youtube.com/vi/' . $id . '/mqdefault.jpg';
}
return $thumbnail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment