• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/tradingplatform/vk/photoresizer.php
  • Класс: BitrixSaleTradingPlatformVkPhotoResizer
  • Вызов: PhotoResizer::ScaleImage
static function ScaleImage($sourceImageWidth, $sourceImageHeight, $arSize, $resizeType, &$bNeedCreatePicture, &$arSourceSize, &$arDestinationSize)
{
	if (!is_array($arSize))
		$arSize = array();
	if (!array_key_exists("width", $arSize) || intval($arSize["width"]) <= 0)
		$arSize["width"] = 0;
	if (!array_key_exists("height", $arSize) || intval($arSize["height"]) <= 0)
		$arSize["height"] = 0;
	$arSize["width"] = intval($arSize["width"]);
	$arSize["height"] = intval($arSize["height"]);
	
	$bNeedCreatePicture = false;
	$arSourceSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
	$arDestinationSize = array("x" => 0, "y" => 0, "width" => 0, "height" => 0);
	
	if ($sourceImageWidth > 0 && $sourceImageHeight > 0)
	{
		if ($arSize["width"] > 0 && $arSize["height"] > 0)
		{
			switch ($resizeType)
			{
				case BX_RESIZE_IMAGE_EXACT:
					$bNeedCreatePicture = true;
					
					$ratio = (($sourceImageWidth / $sourceImageHeight) < ($arSize["width"] / $arSize["height"])) ?
						$arSize["width"] / $sourceImageWidth : $arSize["height"] / $sourceImageHeight;
					
					$x = max(0, ceil($sourceImageWidth / 2 - ($arSize["width"] / 2) / $ratio));
					$y = max(0, ceil($sourceImageHeight / 2 - ($arSize["height"] / 2) / $ratio));
					
					$arDestinationSize["width"] = $arSize["width"];
					$arDestinationSize["height"] = $arSize["height"];
					
					$arSourceSize["x"] = $x;
					$arSourceSize["y"] = $y;
					$arSourceSize["width"] = ceil($arSize["width"] / $ratio);
					$arSourceSize["height"] = ceil($arSize["height"] / $ratio);
					break;
				
				default:
					if ($resizeType == BX_RESIZE_IMAGE_PROPORTIONAL_ALT)
					{
						$width = max($sourceImageWidth, $sourceImageHeight);
						$height = min($sourceImageWidth, $sourceImageHeight);
					}
					else
					{
						$width = $sourceImageWidth;
						$height = $sourceImageHeight;
					}
					$ResizeCoeff["width"] = $arSize["width"] / $width;
					$ResizeCoeff["height"] = $arSize["height"] / $height;
					
					$iResizeCoeff = min($ResizeCoeff["width"], $ResizeCoeff["height"]);
//						$iResizeCoeff = ((0 < $iResizeCoeff) && ($iResizeCoeff < 1) ? $iResizeCoeff : 1);
					$bNeedCreatePicture = ($iResizeCoeff > 0 ? true : false);
					
					$arDestinationSize["width"] = max(1, intval(ceil($iResizeCoeff * $sourceImageWidth)));
					$arDestinationSize["height"] = max(1, intval(ceil($iResizeCoeff * $sourceImageHeight)));
					
					$arSourceSize["x"] = 0;
					$arSourceSize["y"] = 0;
					$arSourceSize["width"] = $sourceImageWidth;
					$arSourceSize["height"] = $sourceImageHeight;
					break;
			}
		}
		else
		{
			$arSourceSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
			$arDestinationSize = array("x" => 0, "y" => 0, "width" => $sourceImageWidth, "height" => $sourceImageHeight);
		}
	}
}