A simple PHP class for downloading the thumbnail of a Youtube video to a local directory – which than can be used for creating a privacy friendly 2-click-solution as demonstrated in /demo/index.php
.
// load class
require 'YtThumbLoader.php';
$ytvideo = new YtThumbLoader();
// set cache directory
$ytvideo->setCacheDirectory("Path/to/your/cache-directory");
// set cache base url (needed for return of full qualified thumbnail image url)
$ytvideo->setCacheBaseUrl("https://example.com/ytcache");
// set the ID of the youtube video you want
// for https://www.youtube.com/watch?v=dQw4w9WgXcQ the ID would be dQw4w9WgXcQ
$ytvideo->setVideoID("dQw4w9WgXcQ");
// get (local) URL for the cache thumbnail
$thumburl = $ytvideo->getThumbnailUrl();
// get (remote) URL of the YouTube video
$videourl = $ytvideo->getVideoUrl();
setCacheDirectory(string)
required, sets the path to your local cache directorysetCacheBaseUrl(string)
required, sets the URL for the cache directory in your project, used for the (local) thumbnail urlsetVideoID(string)
required, sets thesetCacheDuration(int)
optional, sets the cache duration for the thumbnail (default is 10800 wich is one week)setYtBaseUrl(string)
optional, sets the base video url (default is https://www.youtube-nocookie.com/embed/ for YouTube in privacy mode – may be altered to https://www.youtube.com/embed/ if you want)
getThumbnailUrl()
gets the URL of the locally stored thumbnailgetVideoUrl()
gets the embed video URL (for iframe usage)