View source
<?php
namespace Drupal\blazy\Plugin\Field\FieldFormatter;
use Drupal\blazy\BlazyMedia;
trait BlazyVideoTrait {
public function getImageItem($file) {
if ($item = BlazyMedia::imageItem($file)) {
$settings = (array) $item;
$settings['type'] = 'image';
$data['item'] = $item;
$data['settings'] = $settings;
return $data;
}
return [];
}
public function getMediaItem(array &$data, $media = NULL) {
if ($this->targetType != 'file') {
return;
}
$settings = $data['settings'];
$settings['media_url'] = entity_uri($this->targetType, $media)['path'];
$settings['media_id'] = $media->fid;
$settings['type'] = $media->type;
$settings['media_uri'] = $media->uri;
$settings['view_mode'] = empty($settings['view_mode']) ? 'default' : $settings['view_mode'];
list($settings['scheme']) = array_pad(array_map('trim', explode(":", $media->uri, 2)), 2, NULL);
try {
$wrapper = file_stream_wrapper_get_instance_by_uri($media->uri);
if (!is_object($wrapper)) {
throw new \Exception('Unable to find matching wrapper!');
}
$media_data = BlazyMedia::getMediaData($settings);
$settings['input_url'] = drupal_strip_dangerous_protocols($wrapper
->interpolateUrl());
$settings['embed_url'] = isset($media_data['url']) ? $media_data['url'] : '';
$settings['autoplay_url'] = $this
->getAutoplayUrl($settings['embed_url']);
$settings['video_id'] = isset($media_data['video_id']) ? $media_data['video_id'] : '';
$settings['uri'] = $wrapper
->getLocalThumbnailPath();
$settings['image_url'] = file_create_url($settings['uri']);
} catch (\Exception $e) {
}
$data['settings'] = $settings;
}
public function getVideoId($url) {
$parts = parse_url($url);
if (isset($parts['query'])) {
parse_str($parts['query'], $qs);
if (isset($qs['v'])) {
return $qs['v'];
}
elseif (isset($qs['vi'])) {
return $qs['vi'];
}
}
if (isset($parts['path'])) {
$path = explode('/', trim($parts['path'], '/'));
return $path[count($path) - 1];
}
return FALSE;
}
public function getHost($url) {
$host = preg_replace('/^www\\./', '', parse_url($url, PHP_URL_HOST));
$host = explode(".", $host);
return $host[0];
}
public function getVideoThumbnail($url) {
$vid = $this
->getVideoId($url);
if (!$vid) {
return '';
}
$dir = 'public://video_thumbnails';
$destination = $dir . '/' . $vid . ".jpg";
if (is_file($destination)) {
return file_create_url($destination);
}
$file_url = '';
try {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
if (strpos($url, 'vimeo') !== FALSE) {
$data = @file_get_contents("{$protocol}://vimeo.com/api/v2/video/{$vid}.json");
$data = drupal_json_decode($data);
$file_url = isset($data[0]) ? $data[0]->thumbnail_large : '';
}
elseif (strpos($url, 'youtu') !== FALSE) {
$context_options = [
"ssl" => [
"verify_peer" => FALSE,
"verify_peer_name" => FALSE,
],
];
if (@file_get_contents("{$protocol}://img.youtube.com/vi/{$vid}/maxresdefault.jpg", 0, stream_context_create($context_options), 0, 1)) {
$file_url = "{$protocol}://img.youtube.com/vi/{$vid}/maxresdefault.jpg";
}
elseif (@file_get_contents($protocol . "://img.youtube.com/vi/{$vid}/0.jpg", 0, stream_context_create($context_options), 0, 1)) {
$file_url = "{$protocol}://img.youtube.com/vi/{$vid}/0.jpg";
}
}
} catch (\Exception $e) {
}
if ($file_url) {
if (!is_file($dir)) {
file_prepare_directory($dir, FILE_CREATE_DIRECTORY);
}
if ($file_uri = system_retrieve_file($file_url, $destination, FALSE, FILE_EXISTS_REPLACE)) {
$file_url = file_create_url($file_uri);
}
}
return $file_url;
}
public function getAutoplayUrl($url) {
if (strpos($url, 'autoplay') === FALSE || strpos($url, 'autoplay=0') !== FALSE) {
return strpos($url, '?') === FALSE ? $url . '?autoplay=1' : $url . '&autoplay=1';
}
return $url;
}
public function getVideoEmbedUrl($url) {
if ($vid = $this
->getVideoId($url)) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
if (strpos($url, 'youtu') !== FALSE) {
return $protocol . '://www.youtube.com/embed/' . $vid;
}
elseif (strpos($url, 'vimeo') !== FALSE) {
return $protocol . '://player.vimeo.com/video/' . $vid;
}
}
return '';
}
}