OpenCart 🇺🇦

Схема

public mixed resize ( $width = 0, $height = 0, $default = '' )

Аргументы

Аргумент Возможный тип Описание
$width = 0
$height = 0
$default = ''

Описание

Метод пока еще не документирован.

Исходный код

if (!$this->info['width'] || !$this->info['height']) {
	return;
}

$xpos = 0;
$ypos = 0;
$scale = 1;

$scale_w = $width / $this->info['width'];
$scale_h = $height / $this->info['height'];

if ($default == 'w') {
	$scale = $scale_w;
} elseif ($default == 'h'){
	$scale = $scale_h;
} else {
	$scale = min($scale_w, $scale_h);
}

if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
	return;
}

$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);			
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);

$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);

if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {		
	imagealphablending($this->image, false);
	imagesavealpha($this->image, true);
	$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
	imagecolortransparent($this->image, $background);
} else {
	$background = imagecolorallocate($this->image, 255, 255, 255);
}

imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
imagedestroy($image_old);

$this->info['width']  = $width;
$this->info['height'] = $height;