Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bxtimur/b58f7694d324321244ecde10cb53ddb7 to your computer and use it in GitHub Desktop.
Save bxtimur/b58f7694d324321244ecde10cb53ddb7 to your computer and use it in GitHub Desktop.
<?
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