• Модуль: landing
  • Путь к файлу: ~/bitrix/modules/landing/lib/manager.php
  • Класс: BitrixLandingManager
  • Вызов: Manager::savePicture
static function savePicture($file, $ext = false, $params = array())
{
	// local file
	if (!is_array($file) && mb_substr($file, 0, 1) == '/')
	{
		$file = CFile::makeFileArray($file);
	}
	// url of picture
	else if (!is_array($file))
	{
		$httpClient = new BitrixMainWebHttpClient();
		$httpClient->setPrivateIp(false);
		$httpClient->setTimeout(5);
		$httpClient->setStreamTimeout(5);
		$urlComponents = parse_url($file);

		// detect tmp file name
		if ($urlComponents && $urlComponents['path'] != '')
		{
			$tempPath = CFile::getTempName('', bx_basename(urldecode($urlComponents['path'])));
		}
		else
		{
			$tempPath = CFile::getTempName('', bx_basename(urldecode($file)));
		}
		if ($ext !== false && in_array($ext, explode(',', CFile::getImageExtensions())))
		{
			if (mb_substr($tempPath, -3) != $ext)
			{
				$tempPath = $tempPath . '.' . $ext;
			}
		}

		// download and save
		if ($httpClient->download($file, $tempPath))
		{
			$fileName = $httpClient->getHeaders()->getFilename();
			$file = CFile::makeFileArray($tempPath);
			if ($file && $fileName)
			{
				$file['name'] = $fileName;
			}
		}
	}

	// base64
	elseif (
		is_array($file) &&
		isset($file[0]) &&
		isset($file[1])
	)
	{
		$fileParts = explode('.', $file[0]);
		$ext = array_pop($fileParts);
		$tempPath = CFile::getTempName(
			'',
			CUtil::translit(
				implode('', $fileParts),
				'ru'
			) . '.' . $ext
		);
		$fileIO = new BitrixMainIOFile(
			$tempPath
		);
		$fileIO->putContents(
			base64_decode($file[1])
		);
		$file = CFile::makeFileArray($tempPath);
	}

	$isSvg = false;
	$isImage = CFile::checkImageFile($file, 0, 0, 0, array('IMAGE')) === null;

	if (!$isImage && (Manager::getOption('allow_svg_content') === 'Y'))
	{
		$extension = getFileExtension(mb_strtolower($file['name']));
		if ($extension === 'svg')
		{
			$isSvg = true;
		}
	}

	// post array or file from prev. steps
	if ($isImage || $isSvg)
	{
		// resize if needed
		if (
			$isImage &&
			isset($params['width']) &&
			isset($params['height'])
		)
		{
			CFile::resizeImage(
				$file,
				$params,
				isset($params['resize_type'])
				? intval($params['resize_type'])
				: BX_RESIZE_IMAGE_PROPORTIONAL);
		}
		// if duplicate change size little (bug #167903)
		if ($isImage && self::isDuplicateExistsInAnotherModule($file['tmp_name'], $file['size']))
		{
			[$width, $height] = getimagesize($file['tmp_name']) ?: [0, 0];
			if ($width && $height)
			{
				CFile::resizeImage($file, ['width' => $width-1, 'height' => $height-1]);
			}
		}
		// save
		$module = 'landing';
		$file['name'] = File::transliterateFileName($file['name']);
		$file['name'] = File::sanitizeFileName($file['name']);
		$file['MODULE_ID'] = $module;
		$file = CFile::saveFile($file, $module);
		if ($file)
		{
			$file = CFile::getFileArray($file);
		}
		if ($file)
		{
			$file['SRC'] = str_replace(
				'%',
				'%25',
				$file['SRC']
			);
			return $file;
		}
	}

	return false;
}