‪TYPO3CMS  10.4
YouTubeHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 
26 {
34  public function ‪getPublicUrl(‪File $file, $relativeToCurrentScript = false)
35  {
36  $videoId = $this->‪getOnlineMediaId($file);
37  return sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($videoId));
38  }
39 
46  public function ‪getPreviewImage(‪File $file)
47  {
48  $videoId = $this->‪getOnlineMediaId($file);
49  $temporaryFileName = $this->‪getTempFolderPath() . 'youtube_' . md5($videoId) . '.jpg';
50 
51  if (!file_exists($temporaryFileName)) {
52  $tryNames = ['maxresdefault.jpg', 'mqdefault.jpg', '0.jpg'];
53  foreach ($tryNames as $tryName) {
54  $previewImage = ‪GeneralUtility::getUrl(
55  sprintf('https://img.youtube.com/vi/%s/%s', $videoId, $tryName)
56  );
57  if ($previewImage !== false) {
58  file_put_contents($temporaryFileName, $previewImage);
59  ‪GeneralUtility::fixPermissions($temporaryFileName);
60  break;
61  }
62  }
63  }
64 
65  return $temporaryFileName;
66  }
67 
75  public function ‪transformUrlToFile($url, ‪Folder $targetFolder)
76  {
77  $videoId = null;
78  // Try to get the YouTube code from given url.
79  // These formats are supported with and without http(s)://
80  // - youtu.be/<code> # Share URL
81  // - www.youtube.com/watch?v=<code> # Normal web link
82  // - www.youtube.com/v/<code>
83  // - www.youtube-nocookie.com/v/<code> # youtube-nocookie.com web link
84  // - www.youtube.com/embed/<code> # URL form iframe embed code, can also get code from full iframe snippet
85  if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
86  $videoId = $match[1];
87  }
88  if (empty($videoId)) {
89  return null;
90  }
91  return $this->‪transformMediaIdToFile($videoId, $targetFolder, $this->extension);
92  }
93 
101  protected function ‪getOEmbedUrl($mediaId, $format = 'json')
102  {
103  return sprintf(
104  'https://www.youtube.com/oembed?url=%s&format=%s&maxwidth=2048&maxheight=2048',
105  rawurlencode(sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($mediaId))),
106  rawurlencode($format)
107  );
108  }
109 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getTempFolderPath
‪string getTempFolderPath()
Definition: AbstractOnlineMediaHelper.php:124
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:59
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static mixed getUrl($url, $includeHeader=0, $requestHeaders=null, &$report=null)
Definition: GeneralUtility.php:1748
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper\transformMediaIdToFile
‪File transformMediaIdToFile($mediaId, Folder $targetFolder, $fileExtension)
Definition: AbstractOEmbedHelper.php:43
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getPreviewImage
‪string getPreviewImage(File $file)
Definition: YouTubeHelper.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static mixed fixPermissions($path, $recursive=false)
Definition: GeneralUtility.php:1863
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getOEmbedUrl
‪string getOEmbedUrl($mediaId, $format='json')
Definition: YouTubeHelper.php:101
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\transformUrlToFile
‪File null transformUrlToFile($url, Folder $targetFolder)
Definition: YouTubeHelper.php:75
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getPublicUrl
‪string null getPublicUrl(File $file, $relativeToCurrentScript=false)
Definition: YouTubeHelper.php:34
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper
Definition: YouTubeHelper.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper
Definition: AbstractOEmbedHelper.php:27