Skip to content

Commit 37505bd

Browse files
committed
- Added youtube embed url and youtube thumbnail methods
- Added google embed url methods
1 parent cc25ff5 commit 37505bd

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

docs/modules/available-methods.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,45 @@ $formatted = number_format_exact('1120.500', max_decimals: 2); // '1,120.50'
3737
$formatted = number_format_exact('1120.500', max_decimals: 0); // '1,120.5'
3838
$formatted = number_format_exact('1120.000', max_decimals: 0); // '1,120'
3939
```
40+
41+
## youtube_video_id
42+
43+
Extracts the video id from YouTube URLs.
44+
45+
```php
46+
$video_id = youtube_video_id('https://www.youtube.com/watch?v=p7P8DmTbjUY'); // 'p7P8DmTbjUY'
47+
```
48+
49+
## youtube_embed_url
50+
51+
Generates the YouTube embed URL from a YouTube link
52+
53+
```php
54+
$embed_url = youtube_embed_url('https://www.youtube.com/watch?v=p7P8DmTbjUY'); // 'https://www.youtube.com/embed/p7P8DmTbjUY'
55+
```
56+
57+
## youtube_thumbnail_url
58+
59+
Finds the YouTube thumbnail URL from a YouTube link. Supports `'sd'`, `'mq'`, `'hq'`, and `'max'` as the second argument to get image in different sizes. The default is `'max'`.
60+
61+
```php
62+
$thumbnail = youtube_thumbnail_url('https://www.youtube.com/watch?v=p7P8DmTbjUY'); // 'https://img.youtube.com/vi/p7P8DmTbjUY/maxresdefault.jpg'
63+
$thumbnail = youtube_thumbnail_url('https://www.youtube.com/watch?v=p7P8DmTbjUY', 'hq'); // 'https://img.youtube.com/vi/p7P8DmTbjUY/hqdefault.jpg'
64+
```
65+
66+
## google_drive_file_id
67+
68+
Extracts the file id from Google Drive URLs.
69+
70+
```php
71+
$file_id = google_drive_file_id('https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/view?usp=drive_link'); // '13YYH0OlibUYzT2q7uRPOQotbE03rdsDr'
72+
```
73+
74+
## google_drive_embed_url
75+
76+
Generates a Google Drive embed URL from a file link
77+
78+
```php
79+
$embed_url = google_drive_embed_url('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'); // 'https://docs.google.com/viewer?embedded=true&url=https%3A%2F%2Fwww.w3.org%2FWAI%2FER%2Ftests%2Fxhtml%2Ftestfiles%2Fresources%2Fpdf%2Fdummy.pdf'
80+
$embed_url = google_drive_embed_url('https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/view?usp=drive_link'); // 'https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/preview'
81+
```

src/helpers.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,3 +930,101 @@ function relative_date(Carbon $date, $locale = null) {
930930
__(':time ago', ['time' => $diff], $locale);
931931
}
932932
}
933+
934+
if (! function_exists('youtube_thumbnail_url')) {
935+
936+
/**
937+
* Get the video thumbnail from a youtube url
938+
*
939+
* @param string
940+
* @return string
941+
*/
942+
function youtube_thumbnail_url(?string $url, string $resolution = 'max'): ?string
943+
{
944+
$resolutions = [
945+
'sd' => 'sddefault.jpg',
946+
'mq' => 'mqdefault.jpg',
947+
'hq' => 'hqdefault.jpg',
948+
'max' => 'maxresdefault.jpg',
949+
];
950+
951+
$youtube_id = youtube_video_id($url);
952+
953+
if ($youtube_id) {
954+
return 'https://img.youtube.com/vi/' . $youtube_id . '/' . $resolutions[$resolution];
955+
}
956+
957+
return null;
958+
}
959+
}
960+
961+
if (! function_exists('youtube_embed_url')) {
962+
/**
963+
* Get the video id from a youtube url
964+
*
965+
* @param string
966+
* @return string
967+
*/
968+
function youtube_embed_url(?string $url): string {
969+
$video_id = youtube_video_id($url);
970+
return $video_id ? 'https://www.youtube.com/embed/'.$video_id : '';
971+
}
972+
}
973+
974+
if (! function_exists('youtube_video_id')) {
975+
/**
976+
* Get the video id from a youtube url
977+
*
978+
* @param string
979+
* @return string
980+
*/
981+
function youtube_video_id(?string $url): ?string {
982+
if ($url) {
983+
return (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) ? $match[1] : null;
984+
}
985+
986+
return null;
987+
}
988+
}
989+
990+
if (! function_exists('google_drive_embed_url')) {
991+
/**
992+
* Get Google Drive embed url
993+
*
994+
* @param string
995+
* @return string
996+
*/
997+
function google_drive_embed_url(?string $url): string {
998+
$file_id = google_drive_file_id($url);
999+
return $file_id ? "https://drive.google.com/file/d/$file_id/preview" : "https://docs.google.com/viewer?embedded=true&url=".urlencode($url);
1000+
}
1001+
}
1002+
1003+
if (! function_exists('google_drive_file_id')) {
1004+
/**
1005+
* Get the file id from a Google Drive url
1006+
* https://stackoverflow.com/questions/55663035/i-want-to-get-id-from-the-google-drive-file-using-php
1007+
*
1008+
* @param string
1009+
* @return string
1010+
*/
1011+
function google_drive_file_id(?string $url): ?string {
1012+
if ($url && Str::startsWith($url, 'https://drive.google.com')) {
1013+
$url_parts = parse_url($url);
1014+
1015+
if ($query = ($url_parts['query'] ?? '')) {
1016+
$query_parts = [];
1017+
1018+
parse_str($query, $query_parts);
1019+
1020+
if (! empty($query_parts['id'])) {
1021+
return $query_parts['id'];
1022+
}
1023+
}
1024+
1025+
return explode('/', $url_parts['path'])[3] ?? null;
1026+
}
1027+
1028+
return null;
1029+
}
1030+
}

tests/Unit/HelpersTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,40 @@ protected function defineEnvironment($app)
1313
config()->set('filesystems.disks.local.serve', false);
1414
}
1515

16+
#[Test]
17+
public function it_can_extract_youtube_thumbnail_from_youtube_url()
18+
{
19+
$this->assertEquals('https://img.youtube.com/vi/p7P8DmTbjUY/maxresdefault.jpg', youtube_thumbnail_url('https://www.youtube.com/watch?v=p7P8DmTbjUY'));
20+
$this->assertEquals('https://img.youtube.com/vi/p7P8DmTbjUY/hqdefault.jpg', youtube_thumbnail_url('https://www.youtube.com/watch?v=p7P8DmTbjUY', 'hq'));
21+
}
22+
23+
#[Test]
24+
public function it_can_extract_youtube_video_id_from_youtube_url()
25+
{
26+
$this->assertEquals('p7P8DmTbjUY', youtube_video_id('https://www.youtube.com/watch?v=p7P8DmTbjUY'));
27+
$this->assertEquals('p7P8DmTbjUY', youtube_video_id('https://youtu.be/p7P8DmTbjUY?si=cYqLYlT73rHBnx8F'));
28+
}
29+
30+
#[Test]
31+
public function it_can_get_youtube_embed_url_from_youtube_url()
32+
{
33+
$this->assertEquals('https://www.youtube.com/embed/p7P8DmTbjUY', youtube_embed_url('https://www.youtube.com/watch?v=p7P8DmTbjUY'));
34+
$this->assertEquals('https://www.youtube.com/embed/p7P8DmTbjUY', youtube_embed_url('https://youtu.be/p7P8DmTbjUY?si=cYqLYlT73rHBnx8F'));
35+
}
36+
37+
#[Test]
38+
public function it_can_get_google_drive_file_id_from_google_drive_url()
39+
{
40+
$this->assertEquals('13YYH0OlibUYzT2q7uRPOQotbE03rdsDr', google_drive_file_id('https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/view?usp=drive_link'));
41+
}
42+
43+
#[Test]
44+
public function it_can_get_google_drive_embed_url_from_url()
45+
{
46+
$this->assertEquals('https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/preview', google_drive_embed_url('https://drive.google.com/file/d/13YYH0OlibUYzT2q7uRPOQotbE03rdsDr/view?usp=drive_link'));
47+
$this->assertEquals('https://docs.google.com/viewer?embedded=true&url=https%3A%2F%2Fwww.w3.org%2FWAI%2FER%2Ftests%2Fxhtml%2Ftestfiles%2Fresources%2Fpdf%2Fdummy.pdf', google_drive_embed_url('https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf'));
48+
}
49+
1650
#[Test]
1751
public function it_can_convert_seconds_to_human_readable_time_with_zeros_omitted()
1852
{

0 commit comments

Comments
 (0)