1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
4
|
|
|
|
5
|
|
|
class Media |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var HttpInterface |
9
|
|
|
*/ |
10
|
|
|
protected $http; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Media constructor. |
14
|
|
|
* @param HttpInterface $http |
15
|
|
|
*/ |
16
|
87 |
|
public function __construct(HttpInterface $http) |
17
|
|
|
{ |
18
|
87 |
|
$this->http = $http; |
19
|
87 |
|
} |
20
|
|
|
|
21
|
87 |
|
public function get($url) |
22
|
|
|
{ |
23
|
87 |
|
if (strpos($url, "youtu") !== false) { |
24
|
12 |
|
$link = $this->youtubeFormat($url); |
25
|
12 |
|
if ($link !== false) { |
26
|
9 |
|
return $link; |
27
|
|
|
} |
28
|
3 |
|
} |
29
|
78 |
|
if (strpos($url, "vimeo") !== false) { |
30
|
6 |
|
$link = $this->vimeoFormat($url); |
31
|
6 |
|
if ($link !== false) { |
32
|
3 |
|
return $link; |
33
|
|
|
} |
34
|
3 |
|
} |
35
|
75 |
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $url |
40
|
|
|
* @return array|bool |
41
|
|
|
*/ |
42
|
12 |
|
private function youtubeFormat($url) |
43
|
|
|
{ |
44
|
12 |
|
$id = null; |
45
|
|
|
$re = "/https?:\\/\\/(?:www\\.)?youtu(?:be\\.com\\/(watch\\?)?". |
46
|
12 |
|
"(.*?&?)?v=|\\.be\\/)([\\w\\-]+)(&(amp;)?[\\w\\?=]*)?/i"; |
47
|
12 |
|
if (preg_match($re, $url, $matching)) { |
48
|
9 |
|
$id = $matching[3]; |
49
|
9 |
|
} else { |
50
|
6 |
|
$re = "/https?:\\/\\/(?:www\\.)?youtube\\.com\\/embed\\/([^&|^?|^\\/]*)/i"; |
51
|
6 |
|
if (preg_match($re, $url, $matching)) { |
52
|
3 |
|
$id = $matching[1]; |
53
|
3 |
|
} |
54
|
|
|
} |
55
|
12 |
|
if ($id !== null) { |
56
|
|
|
return [ |
57
|
9 |
|
'url' => 'https://www.youtube.com/embed/' . $id, |
58
|
9 |
|
'thumbnail' => "http://i2.ytimg.com/vi/{$id}/default.jpg", |
59
|
9 |
|
]; |
60
|
|
|
} |
61
|
3 |
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $url |
66
|
|
|
* @param $matches |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
6 |
|
private function vimeoFormat($url) |
70
|
|
|
{ |
71
|
|
|
$re = "/https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/" . |
72
|
6 |
|
"(?:channels\\/(?:\\w+\\/)?|groups\\/([^\\/]*)\\/videos\\/". |
73
|
6 |
|
"|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)?/"; |
74
|
6 |
|
preg_match_all($re, $url, $matches); |
75
|
|
|
|
76
|
6 |
|
if (!$matches[3]) { |
77
|
3 |
|
return false; |
78
|
|
|
} |
79
|
3 |
|
$imgId = $matches[3][0]; |
80
|
|
|
|
81
|
3 |
|
$hash = json_decode($this->http->get("http://vimeo.com/api/v2/video/{$imgId}.json")); |
82
|
|
|
|
83
|
|
|
return [ |
84
|
3 |
|
'url' => "http://player.vimeo.com/video/{$imgId}", |
85
|
3 |
|
'thumbnail' => $hash[0]->thumbnail_large, |
86
|
3 |
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|