PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / core / src / Utility / Embed.php

Embed.php in Depicter — Popup & Slider Builder trunk, at vendor/averta/core/src/Utility/Embed.php

105 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Averta\Core\Utility;
3
4 /**
5 * Class to generate Embed url
6 *
7 * https://regex101.com/r/6IhI2o/1
8 */
9 class Embed{
10
11 /**
12 * Converts YouTube url to embed url
13 *
14 * @param string $videoUrl YouTube url
15 *
16 * @return bool|mixed Returns the YouTube embed url on success, and false on failure.
17 */
18 public static function getYouTubeEmbedUrl( $videoUrl ){
19 if( $code = self::getYouTubeVimeoCode( $videoUrl ) ){
20 return 'https://www.youtube.com/embed/' . $code;
21 }
22
23 return $code;
24 }
25
26 /**
27 * Converts YouTube url to poster image url
28 *
29 * @param string $videoUrl YouTube url
30 *
31 * @return bool|mixed Returns the YouTube poster image url on success, and false on failure.
32 */
33 public static function getYouTubePosterUrl( $videoUrl ){
34 if( $code = self::getYouTubeVimeoCode( $videoUrl ) ){
35 return 'http://img.youtube.com/vi/' . $code. '/maxresdefault.jpg';
36 }
37
38 return $code;
39 }
40
41 /**
42 * Converts Vimeo url to embed url
43 *
44 * @param string $videoUrl Vimeo url
45 *
46 * @return bool|mixed Returns the Vimeo embed url on success, and false on failure.
47 */
48 public static function getVimeoEmbedUrl( $videoUrl ){
49 if( $code = self::getYouTubeVimeoCode( $videoUrl ) ){
50 return 'https://player.vimeo.com/video/' . $code;
51 }
52
53 return $code;
54 }
55
56 /**
57 * Extracts and returns YouTube or Vimeo video ID from video url
58 *
59 * @param string $videoUrl YouTube url
60 *
61 * @return bool|mixed Returns the YouTube or Vimeo video code on success, and false on failure.
62 */
63 public static function getYouTubeVimeoCode( $videoUrl ){
64 $matches = $matches = static::getYouTubeVimeoMatches( $videoUrl );
65
66 if( is_array( $matches ) ){
67 return end( $matches );
68 }
69 return false;
70 }
71
72 /**
73 * Converts Youtube or Vimeo video url to embed url
74 *
75 * @param string $videoUrl YouTube or Vimeo video url
76 *
77 * @return string Returns Video embed url on success
78 */
79 public static function getYouTubeVimeoEmbedUrl( $videoUrl ){
80 $matches = static::getYouTubeVimeoMatches( $videoUrl );
81
82 if( is_array( $matches ) ){
83 if( !empty( $matches[3] ) ){
84 if( in_array( $matches[3], ["youtube.com", "youtu.be"] ) ){
85 return 'https://www.youtube.com/embed/' . end( $matches );
86 } elseif( $matches[3] === "vimeo.com" ){
87 return 'https://player.vimeo.com/video/' . end( $matches );
88 }
89 }
90 }
91 return '';
92 }
93
94 /**
95 * Generates matches for a Youtube or Vimeo url
96 *
97 * @param string $videoUrl Video url
98 *
99 * @return array|bool|mixed
100 */
101 public static function getYouTubeVimeoMatches( $videoUrl ){
102 return Str::extractByRegex( $videoUrl, '/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\\&\S+)?/', -1 );
103 }
104 }
105