Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bjoelin/53f435ad9173abea3ecedba5e6f50c32 to your computer and use it in GitHub Desktop.
Save Bjoelin/53f435ad9173abea3ecedba5e6f50c32 to your computer and use it in GitHub Desktop.
<?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