- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/lib/urlpreview/urlpreview.php
- Класс: BitrixMainUrlPreviewUrlPreview
- Вызов: UrlPreview::saveImage
static function saveImage($url)
{
$fileId = false;
$httpClient = new HttpClient();
$httpClient->setTimeout(5);
$httpClient->setStreamTimeout(5);
$urlComponents = parse_url($url);
$fileName = ($urlComponents && $urlComponents["path"] <> '')
? bx_basename($urlComponents["path"])
: bx_basename($url)
;
$tempFileName = Random::getString(32) . '.' . GetFileExtension($fileName);
$tempPath = CFile::GetTempName('', $tempFileName);
try
{
$httpClient->download($url, $tempPath);
}
catch (ErrorException $exception)
{
return null;
}
$fileName = $httpClient->getHeaders()->getFilename();
$localFile = CFile::MakeFileArray($tempPath);
$localFile['MODULE_ID'] = 'main';
if (is_array($localFile))
{
if ($fileName <> '')
{
$localFile['name'] = $fileName;
}
if (CFile::CheckImageFile($localFile, 0, 0, 0, array("IMAGE")) === null)
{
$fileId = CFile::SaveFile($localFile, 'urlpreview', true);
}
}
return ($fileId === false ? null : $fileId);
}