Skip to content

Instantly share code, notes, and snippets.

@spiffin
Last active August 29, 2015 14:08
Show Gist options
  • Save spiffin/c00be88f06359b1293e5 to your computer and use it in GitHub Desktop.
Save spiffin/c00be88f06359b1293e5 to your computer and use it in GitHub Desktop.
Textpattern form for cached custom Vimeo thumbs. Requires /images/vimcache directory and custom field no 3 = 'vthumbid"
<txp:if_custom_field name="vthumbid">
<img src="<txp:site_url />images/vimcache/<txp:custom_field name='vthumbid' />.jpg" alt="<txp:site_name /> - <txp:title />" class="ex-thumb">
<txp:else />
<txp:php>//<?// CodeMirror
global $thisarticle, $prefs, $id;
$thisid = $thisarticle['thisid'];
if(!function_exists('get')) {
function get($url) {
if (ini_get('allow_url_fopen')) return file_get_contents($url);
}
}
// Get vimeo id array from custom field
$vids = custom_field(array('name' => 'VimeoID'));
// Separate ids
$vidarray = (explode(',',$vids));
// Get first id only
$firstid = array_shift($vidarray);
// Request the image hash from Vimeo API
if ($firstid > 0) $hash = unserialize(get('http://vimeo.com/api/v2/video/' . $firstid . '.php'));
// explode image hash
if ($hash && isset($hash[0]) && isset($hash[0]['thumbnail_large'])) {
$thumb640 = ($hash[0]['thumbnail_large']);
$imgurl = (str_replace("_640","_740",$thumb640));
$urlparts = (explode('/',$imgurl));
$imgname = array_pop($urlparts);
}
// get vimeo image id
$vidarray = pathinfo($imgname);
$vthumbid = array_pop($vidarray);
// write vimeo image id to custom field
if ($id=$thisid) {
safe_update("textpattern", "custom_3 = '$vthumbid'", 'id = '.$id);
}
// variables incl. cache dir
$cachedir = $prefs['path_to_site'].'/images/vimcache/';
$localurl = txpspecialchars(hu.'images/vimcache/'.$imgname);
$alt = $thisarticle['title'];
// fetch remote image and save to cache
$imgfetch = file_get_contents($imgurl);
$localimg = fopen($cachedir.$imgname, 'w+');
chmod($cachedir.$imgname,0755);
fwrite($localimg, $imgfetch);
fclose($localimg);
echo '<img src="' . $localurl . '" alt="<txp:site_name /> - ' . $alt . '" class="ex-thumb">';
</txp:php>
</txp:if_custom_field>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment