‪TYPO3CMS  ‪main
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 {
32  public function ‪getPublicUrl(‪File $file)
33  {
34  $videoId = $this->‪getOnlineMediaId($file);
35  return sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($videoId));
36  }
37 
43  public function ‪getPreviewImage(‪File $file)
44  {
45  $videoId = $this->‪getOnlineMediaId($file);
46  $temporaryFileName = $this->‪getTempFolderPath() . 'youtube_' . md5($videoId) . '.jpg';
47 
48  if (!file_exists($temporaryFileName)) {
49  $tryNames = ['maxresdefault.jpg', 'sddefault.jpg', 'hqdefault.jpg', 'mqdefault.jpg', '0.jpg'];
50  foreach ($tryNames as $tryName) {
51  $previewImage = ‪GeneralUtility::getUrl(
52  sprintf('https://img.youtube.com/vi/%s/%s', $videoId, $tryName)
53  );
54  if ($previewImage !== false) {
55  file_put_contents($temporaryFileName, $previewImage);
56  ‪GeneralUtility::fixPermissions($temporaryFileName);
57  break;
58  }
59  }
60  }
61 
62  return $temporaryFileName;
63  }
64 
71  public function ‪transformUrlToFile(‪$url, ‪Folder $targetFolder)
72  {
73  $videoId = null;
74  // Try to get the YouTube code from given url.
75  // These formats are supported with and without http(s)://
76  // - youtu.be/<code> # Share URL
77  // - www.youtube.com/watch?v=<code> # Normal web link
78  // - www.youtube.com/v/<code>
79  // - www.youtube-nocookie.com/v/<code> # youtube-nocookie.com web link
80  // - www.youtube.com/embed/<code> # URL form iframe embed code, can also get code from full iframe snippet
81  // - www.youtube.com/shorts/<code>
82  if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?|shorts)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', ‪$url, $match)) {
83  $videoId = $match[1];
84  }
85  if (empty($videoId)) {
86  return null;
87  }
88  return $this->‪transformMediaIdToFile($videoId, $targetFolder, $this->extension);
89  }
90 
98  protected function ‪getOEmbedUrl($mediaId, $format = 'json')
99  {
100  return sprintf(
101  'https://www.youtube.com/oembed?url=%s&format=%s&maxwidth=2048&maxheight=2048',
102  rawurlencode(sprintf('https://www.youtube.com/watch?v=%s', rawurlencode($mediaId))),
103  rawurlencode($format)
104  );
105  }
106 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getTempFolderPath
‪string getTempFolderPath()
Definition: AbstractOnlineMediaHelper.php:128
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper\getOnlineMediaId
‪string getOnlineMediaId(File $file)
Definition: AbstractOnlineMediaHelper.php:60
‪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:43
‪TYPO3\CMS\Core\Utility\GeneralUtility\getUrl
‪static string false getUrl(string $url)
Definition: GeneralUtility.php:1439
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getOEmbedUrl
‪string getOEmbedUrl($mediaId, $format='json')
Definition: YouTubeHelper.php:98
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\transformUrlToFile
‪File null transformUrlToFile($url, Folder $targetFolder)
Definition: YouTubeHelper.php:71
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static bool fixPermissions(string $path, bool $recursive=false)
Definition: GeneralUtility.php:1491
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper
Definition: YouTubeHelper.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers
Definition: AbstractOEmbedHelper.php:16
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOEmbedHelper
Definition: AbstractOEmbedHelper.php:28
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\YouTubeHelper\getPublicUrl
‪string null getPublicUrl(File $file)
Definition: YouTubeHelper.php:32