• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/File/Image/Rectangle.php
  • Класс: BitrixMainFileImageRectangle
  • Вызов: Rectangle::resize
public function resize(Rectangle $destination, $mode)
{
	if($this->width == 0 || $this->height == 0)
	{
		return false;
	}

	if($destination->width == 0 || $destination->height == 0)
	{
		return false;
	}

	if($this->width == $destination->width && $this->height == $destination->height)
	{
		return false;
	}

	$result = false;

	switch($mode)
	{
		case FileImage::RESIZE_EXACT:
			//in this mode we change the source rectangle, the destination one is intact
			if(($this->width / $this->height) < ($destination->width / $destination->height))
			{
				$ratio = $destination->width / $this->width;
			}
			else
			{
				$ratio = $destination->height / $this->height;
			}

			$this->x = max(0, round(($this->width / 2) - (($destination->width / 2) / $ratio)));
			$this->y = max(0, round(($this->height / 2) - (($destination->height / 2) / $ratio)));

			$this->width = round($destination->width / $ratio);
			$this->height = round($destination->height / $ratio);

			$result = true;
			break;

		case FileImage::RESIZE_PROPORTIONAL_ALT:
		case FileImage::RESIZE_PROPORTIONAL:
			//in this mode we change the destination rectangle, the source one is intact
			if($mode == FileImage::RESIZE_PROPORTIONAL_ALT)
			{
				$width = max($this->width, $this->height);
				$height = min($this->width, $this->height);
			}
			else
			{
				$width = $this->width;
				$height = $this->height;
			}
			$ratioWidth = $destination->width / $width;
			$ratioHeight = $destination->height / $height;

			$ratio = min($ratioWidth, $ratioHeight);

			//todo: enlarge?
			if($ratio > 0 && $ratio < 1)
			{
				// scale down
				$result = true;
			}
			else
			{
				// destination is larger than source
				$ratio = 1;
			}
			$destination->width = max(1, round($ratio * $this->width));
			$destination->height = max(1, round($ratio * $this->height));

			break;
	}

	return $result;
}