Skip to content

Instantly share code, notes, and snippets.

@bramus
Created January 22, 2014 08:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bramus/8555450 to your computer and use it in GitHub Desktop.
Save bramus/8555450 to your computer and use it in GitHub Desktop.
Vimeo Thumbnail Script - Gets the poster frame for a Vimeo video id.
<?php
/**
* Vimeo Thumbnail Script - Gets the poster frame for a Vimeo video id.
* @author Bramus Van Damme <bramus@bram.us>
*
* Example Request: vimeothumb.php?id=83936766
*/
// Perform a GET request to a given URL.
// Uses `allow_url_fopen` if supported, or curl as a fallback.
function get($url) {
if (ini_get('allow_url_fopen')) return file_get_contents($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Extract ID from the URL
$id = isset($_GET['id']) ? $_GET['id'] : 0;
// Request the image hash with Vimeo
if ($id > 0) $hash = unserialize(get('http://vimeo.com/api/v2/video/' . $id . '.php'));
// Thumbnail found
if ($hash && isset($hash[0]) && isset($hash[0]['thumbnail_large'])) {
header('Content-type: image/jpeg');
echo get($hash[0]['thumbnail_large']);
}
// No thumbnail found: return a valid, but blank image
else {
header('Cache-Control: no-cache');
header('Content-type: image/gif');
header('Content-length: 43');
echo base64_decode('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
}
// EOF
@Nemo64
Copy link

Nemo64 commented Mar 19, 2014

a script that just returns the path to the thumbnail was more what i searched, but i could get that very quickly out of here ;)

@spiffin
Copy link

spiffin commented Oct 18, 2014

Thanks for the script - very useful. You could change the //Thumbnail found section to the edit below to get a 960px thumbnail (or change "_960" in the last line to whatever width you like up to 1280). Heights work too by adding "x480" for example, for a height of 480px.

// Thumbnail found
if ($hash && isset($hash[0]) && isset($hash[0]['thumbnail_large'])) {
header('Content-type: image/jpeg');
$thumb640 = ($hash[0]['thumbnail_large']);
echo get (str_replace("_640","_960",$thumb640));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment