• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/wizard_util.php
  • Класс: ifCWizardUtil
  • Вызов: CWizardUtil::CreateThumbnail
static function CreateThumbnail($sourcePath, $previewPath, $maxWidth, $maxHeight)
{
	if (!is_file($sourcePath))
		return false;

	$maxWidth = intval($maxWidth);
	$maxHeight = intval($maxHeight);

	if ($maxWidth <= 0 || $maxHeight <= 0)
		return false;

	list($sourceWidth, $sourceHeight, $type) = @getimagesize($sourcePath);

	//Image type
	if ($type == 1)
		$imageType = "gif";
	elseif ($type == 2)
		$imageType = "jpeg";
	elseif ($type == 3)
		$imageType = "png";
	else
		return false;

	$imageFunction = "imagecreatefrom".$imageType;
	$sourceImage = @$imageFunction($sourcePath);

	if (!$sourceImage)
		return false;

	$ratioWidth = $sourceWidth / $maxWidth;
	$ratioHeight = $sourceHeight / $maxHeight;
	$ratio = ($ratioWidth > $ratioHeight) ? $ratioWidth : $ratioHeight;

	//Biggest side
	if ($ratio > 0)
	{
		$previewWidth = $sourceWidth / $ratio;
		$previewHeight = $sourceHeight / $ratio;
	}
	else
	{
		$previewWidth = $maxWidth;
		$previewHeight = $maxHeight;
	}

	//GD library version
	$bGD2 = false;
	if (function_exists("gd_info"))
	{
		$arGDInfo = gd_info();
		$bGD2 = strpos($arGDInfo['GD Version'], "2.") !== false;
	}

	//Create Preview
	if ($bGD2)
	{
		$previewImage = imagecreatetruecolor($previewWidth, $previewHeight);
		imagecopyresampled($previewImage, $sourceImage, 0, 0, 0, 0, $previewWidth, $previewHeight, $sourceWidth, $sourceHeight);
	}
	else
	{
		$previewImage = imagecreate($previewWidth, $previewHeight);
		imagecopyresized($previewImage, $sourceImage, 0, 0, 0, 0, $previewWidth, $previewHeight, $sourceWidth, $sourceHeight);
	}

	//Save preview
	$imageFunction = "image".$imageType;

	if ($imageType == "jpeg")
		$success = @$imageFunction($previewImage, $previewPath, 95);
	else
		$success = @$imageFunction($previewImage, $previewPath);

	@imagedestroy($previewImage);
	@imagedestroy($sourceImage);

	return $success;

}