Last active
February 24, 2020 10:55
-
-
Save bxtimur/b58f7694d324321244ecde10cb53ddb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
class YoutubeVideo | |
{ | |
function __construct($video) | |
{ | |
$this->video = $video; | |
} | |
private function Prefix() | |
{ | |
if (preg_match('/[http|https]+:\/\/(?:www\.|)youtube\.com\/watch\?(?:.*)?v=([a-zA-Z0-9_\-]+)/i', $this->video, $matches) | |
|| preg_match('/(?:www\.|)youtube\.com\/embed\/([a-zA-Z0-9_\-]+)/i', $this->video, $matches) | |
|| preg_match('/(?:www\.|)youtu\.be\/([a-zA-Z0-9_\-]+)/i', $this->video, $matches) | |
) { | |
return $matches[1]; | |
} else { | |
return false; | |
} | |
} | |
public function GetImage() | |
{ | |
$prefix = $this->Prefix(); | |
if ($prefix) | |
{ | |
$image = 'http://img.youtube.com/vi/' . $prefix . '/0.jpg'; | |
$arFile = CFile::MakeFileArray($image); // функция Битрикс | |
return $arFile; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public function GetLink() | |
{ | |
$prefix = $this->Prefix(); | |
if ($prefix) | |
{ | |
$video = 'https://www.youtube.com/embed/' . $prefix . ''; | |
return $video; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
// Пример использования | |
if ($arResult["PROPERTIES"]["PRODUCT_VIDEO"]["VALUE"]) { | |
$youtubeVideo = new YoutubeVideo($arResult["PROPERTIES"]["PRODUCT_VIDEO"]["VALUE"]); | |
$arResult["PROPERTIES"]["PRODUCT_VIDEO"]["LINK"] = $youtubeVideo->GetLink(); | |
if ($arResult['PROPERTIES']['YOUTUBE_IMAGE']['VALUE']) | |
{ | |
$arResult['PROPERTIES']['PRODUCT_VIDEO']['IMAGE'] = CFile::ResizeImageGet( | |
$arResult['PROPERTIES']['YOUTUBE_IMAGE']['VALUE'], array('width' => 454, 'height' => 690), BX_RESIZE_IMAGE_PROPORTIONAL | |
); | |
} | |
else | |
{ | |
$youtubeImage["picture"] = $youtubeVideo->GetImage(); | |
$youtubeImage["id"] = CFile::SaveFile($youtubeImage['picture'], "youtube"); | |
CIBlockElement::SetPropertyValuesEx($arResult["ID"], $arResult["IBLOCK_ID"], array("YOUTUBE_IMAGE" => $youtubeImage["picture"])); | |
$arResult['PROPERTIES']['PRODUCT_VIDEO']['IMAGE'] = CFile::ResizeImageGet($youtubeImage['id'], array('width' => 454, 'height' => 260), BX_RESIZE_IMAGE_PROPORTIONAL); | |
CFile::Delete($youtubeImage["id"]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment