Skip to content

Instantly share code, notes, and snippets.

@maximishchenko
Created May 8, 2017 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maximishchenko/7114a90c19fbfe403b1c22d9db6def1f to your computer and use it in GitHub Desktop.
Save maximishchenko/7114a90c19fbfe403b1c22d9db6def1f to your computer and use it in GitHub Desktop.
Download Youtube Thumbnail
<?php
/**
* Get Preview Image from Youtube video link (not from embeded code) and download it if video exist
* @author Maxim Ishchenko <maxim.ishchenko@gmail.com>
* @version 1.0*
* @uses
*
* $getThumbs = new YoutubeThumbs('https://youtu.be/xxxxxxxxxxx', 'quality', 'directory');
* echo '<img src="'.$getThumbs->getThumbnail().'"/>';
*
* xxxxxxxxxxx - youtube video ID
* quality - image quality (default, hqdefault, mqdefault, sddefault)
* directory - path to save thumbnails
*
* @property string $url
* @property string $size
* @property string $path
*/
class YoutubeThumbs
{
/**
* @access public
* @property string $youtubeThumbsPath
* @property string $youtubeUrl
* @property string $youtubeThumbsPath
*/
public $youtubeThumbsPath;
public $youtubeUrl;
public $youtubeThumbnailSize;
function __construct($url, $size, $path)
{
$this->youtubeUrl = $url;
$this->youtubeThumbnailSize = $size;
$this->youtubeThumbsPath = $path;
}
/**
* Check if Youtube Url Exist
* @var string $videoJson formated url from class agruments
* @var array $headers headers url from $videoJson
* @var integer $code parsed http-status code from $headers array
* @access private
* @return true|false
*/
private function isValidYoutubeUrl()
{
$videoJson = "http://www.youtube.com/oembed?url=$this->youtubeUrl&format=json";
$headers = get_headers($videoJson);
$code = substr($headers[0], 9, 3);
return ($code != "404") ? true : false;
}
/**
* @var string $youtubeThumbnailSize return index for Youtube image url from quality name. Return default quality if index incorrect.
* @access private
* @return integer return image index from size string
*/
private function getThumbsSize()
{
switch ($this->youtubeThumbnailSize) {
case 'default':
return 0;
break;
case 'hqdefault':
return 1;
break;
case 'mqdefault':
return 2;
break;
case 'sddefault':
return 3;
break;
// Return default quality if quality from class argument is incorrect
default:
return 0;
break;
}
}
/**
* @var array $urlArr get array from url class argument, "/" - delimiter
* @var integer $urlArrNum count $urlArr elements
* @var string $thumbURL thumbnail url
* @access private
* @return string $thumbURL
*/
private function getThumbsUrl()
{
// Parse Youtube video URL
$urlArr = explode("/",$this->youtubeUrl);
$urlArrNum = count($urlArr);
// Get Youtube video ID
$youtubeVideoId = $urlArr[$urlArrNum - 1];
// Generate youtube thumbnail url
$thumbURL = 'http://img.youtube.com/vi/'.$youtubeVideoId.'/'.$this->getThumbsSize($this->youtubeThumbnailSize).'.jpg';
// Return thumbnail image url
return $thumbURL;
}
/**
* @uses string $youtubeThumbsPath path to save thumbnails from class argument
* @var string $youtubeImageFileName generate md5 hash string from current time with .jpg extension for filename
* @var string $thumbnailPath concatename directory path from class argument and filename from $youtubeImageFileName
* @access private
* @return string|Exception return path to save thumbnails of Exception if path incorrect
*/
private function getThumbnailPath()
{
if(is_dir($this->youtubeThumbsPath))
{
$youtubeImageFileName = md5(time()).'.jpg';
$thumbnailPath = $this->youtubeThumbsPath . '/' . $youtubeImageFileName;
}
else
{
throw new Exception("Incorrect Path", 1);
}
return $thumbnailPath;
}
/**
* Download thumbnails and return full path to image
*
* @uses string $url youtube video url
* @throws Exception return an exception if url is incorrect
* @access public
* @return string|Exception return thumbs url if exist or set Exception
*/
public function getThumbnail()
{
if($this->isValidYoutubeUrl())
{
$thumbsPath = $this->getThumbnailPath();
file_put_contents($thumbsPath, fopen($this->getThumbsUrl($this->youtubeUrl, $this->youtubeThumbnailSize), 'r'));
return $thumbsPath;
}
else
{
throw new Exception("Incorrect URL", 1);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment