Skip to content

Instantly share code, notes, and snippets.

@organizm
Last active June 15, 2016 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save organizm/9c7fafa03a473c4d58cc2e28f4fd6424 to your computer and use it in GitHub Desktop.
Save organizm/9c7fafa03a473c4d58cc2e28f4fd6424 to your computer and use it in GitHub Desktop.
Media class for Wordpress. Can generate video HTML with preview image. #wordpress
<?php
/**
* Клас для обработки видео ссылок и генерации HTML видео.
* User: Alex
* Date: 24.09.2014
* Time: 17:32
*/
class wpc_media
{
public $width = '367';
public $height = '276';
public $img = '';
private $code = "";
private $site = "defalt";
private $all_types = array(
'youtube' => '%(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?: /embed/| /v/| /watch\?v=))([\w-]{10,12})%x',
'yandex' => '#([^\"\']+video\.yandex\.ru[^\"\']+)#'
);
function __construct($input)
{
foreach ($this->all_types as $site => $regexp) {
preg_match($regexp, $input, $match);
if (!empty($match)) {
for ($i = 1; $i < sizeof($match); $i++) {
if ($match[$i] != "") {
$this->code = $match[$i];
$this->site = $site;
$this->imgUrl();
break;
}
}
if ($this->code != "") {
break;
}
}
if ($this->code != "") {
break;
}
}
}
/**
* Функция выводит HTML видео плеера указанной ширины и высоты
*
* @param int $width default 367
* @param int $height default 276
*
* @return string
*/
public function videoHtml($width = 0, $height = 0)
{
if (intval($width) != 0)
$this->width = intval($width);
if (intval($height) != 0)
$this->height = intval($height);
switch ($this->site) {
case 'youtube':
return $this->videoYoutube();
break;
case 'yandex':
return $this->videoYandex();
break;
case 'defalt':
return '<h3>Video url not recognized.</h3>';
break;
}
return '';
}
/**
* Приватная функция для формирования iframe кода видеоплеера Youtube
*
* @return string
*/
private function videoYoutube()
{
$html = '<iframe width="' . $this->width . '" height="' . $this->height . '" src="//www.youtube.com/embed/' . $this->code . '" frameborder="0" allowfullscreen></iframe>';
return $html;
}
/**
* Приватная функция для формирования iframe кода видеоплеера Yandex
*
* @return string
*/
private function videoYandex()
{
$html = '<iframe width="' . $this->width . '" height="' . $this->height . '" frameborder="0" src="' . $this->code . '"> </iframe>';
return $html;
}
/**
* Функция для установки своей ширины и высоты видео
*
* @param $width
* @param $height
*/
public function setCustomSize($width, $height)
{
$this->width = intval($width);
$this->height = intval($height);
}
/**
* Генерируем url картинки для видео
*
* @return string
*/
public function imgUrl()
{
switch ($this->site) {
case 'youtube':
$this->img = 'http://img.youtube.com/vi/' . $this->code . '/0.jpg';
break;
case 'yandex':
$request = 'http://video.yandex.ru/oembed?url=' . $this->code . '&format=json';
$result = file_get_contents($request);
$data = json_decode($result);
if (isset($data->thumbnail_url))
$this->img = $data->thumbnail_url;
break;
case 'defalt':
$this->img = '';
break;
}
return $this->img;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment