Created
June 16, 2020 21:21
-
-
Save Bjoelin/53f435ad9173abea3ecedba5e6f50c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZC\Zebracode\ViewHelpers\Youtube; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | |
/** | |
* Class YoutubePlaceholderViewHelper | |
* @package ZC\Zebracode\ViewHelpers | |
*/ | |
class PlaceholderUrlViewHelper extends AbstractViewHelper | |
{ | |
/** | |
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception | |
*/ | |
public function initializeArguments() | |
{ | |
$this->registerArgument( | |
'ytid', | |
'string', | |
'The ID of a YouTube video to fetch a thumbnail image for.', | |
true | |
); | |
} | |
/** | |
* @param string $ytid | |
* @return string | |
*/ | |
public function render($ytid) | |
{ | |
return $this->fetchYoutubeThumbnailUrl($ytid); | |
} | |
/** | |
* Helper function for retrieving Youtube posters | |
* | |
* @param $id | |
* @return string | |
*/ | |
protected function fetchYoutubeThumbnailUrl($id) | |
{ | |
// different resolutions starting with the highest | |
$resolution = [ | |
'maxresdefault', | |
'sddefault', | |
'mqdefault', | |
'hqdefault', | |
'default' | |
]; | |
// Fallback image, should be replaced in the future | |
$url = 'https://www.placehold.it/420x255?text=YoutubeVideo'; | |
for ($x = 0; $x < sizeof($resolution); $x++) { | |
$url = 'https://img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg'; | |
if (get_headers($url)[0] == 'HTTP/1.0 200 OK') { | |
break; | |
} | |
} | |
return $url; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment