‪TYPO3CMS  11.5
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  // @deprecated $relativeToCurrentScript since v11, will be removed in TYPO3 v12.0
37  $videoId = $this->‪getOnlineMediaId($file);
38  return sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($videoId));
39  }
40 
47  public function ‪getPreviewImage(‪File $file)
48  {
49  $videoId = $this->‪getOnlineMediaId($file);
50  $temporaryFileName = $this->‪getTempFolderPath() . 'youtube_' . md5($videoId) . '.jpg';
51 
52  if (!file_exists($temporaryFileName)) {
53  $tryNames = ['maxresdefault.jpg', 'sddefault.jpg', 'hqdefault.jpg', 'mqdefault.jpg', '0.jpg'];
54  foreach ($tryNames as $tryName) {
55  $previewImage = ‪GeneralUtility::getUrl(
56  sprintf('https://img.youtube.com/vi/%s/%s', $videoId, $tryName)
57  );
58  if ($previewImage !== false) {
59  file_put_contents($temporaryFileName, $previewImage);
60  ‪GeneralUtility::fixPermissions($temporaryFileName);
61  break;
62  }
63  }
64  }
65 
66  return $temporaryFileName;
67  }
68 
76  public function ‪transformUrlToFile($url, ‪Folder $targetFolder)
77  {
78  $videoId = null;
79  // Try to get the YouTube code from given url.
80  // These formats are supported with and without http(s)://
81  // - youtu.be/<code> # Share URL
82  // - www.youtube.com/watch?v=<code> # Normal web link
83  // - www.youtube.com/v/<code>
84  // - www.youtube-nocookie.com/v/<code> # youtube-nocookie.com web link
85  // - www.youtube.com/embed/<code> # URL form iframe embed code, can also get code from full iframe snippet
86  // - www.youtube.com/shorts/<code>
87  if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?|shorts)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
88  $videoId = $match[1];
89  }
90  if (empty($videoId)) {
91  return null;
92  }
93  return $this->‪transformMediaIdToFile($videoId, $targetFolder, $this->extension);
94  }
95 
103  protected function ‪getOEmbedUrl($mediaId, $format = 'json')
104  {
105  return sprintf(
106  'https://www.youtube.com/oembed?url=%s&format=%s&maxwidth=2048&maxheight=2048',
107  rawurlencode(sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($mediaId))),
108  rawurlencode($format)
109  );
110  }
111 }
‪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\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:47
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static string false getUrl($url)
Definition: GeneralUtility.php:1697
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static mixed fixPermissions($path, $recursive=false)
Definition: GeneralUtility.php:1749
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getOEmbedUrl
‪string getOEmbedUrl($mediaId, $format='json')
Definition: YouTubeHelper.php:103
‪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:76
‪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:50
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper
Definition: AbstractOEmbedHelper.php:27