Jump to content
Sign in to follow this  
incoqniski

Обрезка изображений по критерию

Recommended Posts

Доброй ночи.

Столкнулся с проблемой "белого фона". Думаю, эта проблема актуальна на сегодняшний день не только для меня.

Наткнулся на один интересный код, который автоматически "подгоняет" загружаемые изображения под заданные критерии. Вопрос лишь в том, что не удаётся найти к каким именно критериям он обращается.

По сути, он обрезает "белый фон" у картинки, но вместе с тем и урезает саму картинку и везде получается разный размер.

Был бы премного признателен за помощь в этом вопросе.

public function resize($width = 0, $height = 0) {
	if (!$this->info['width'] || !$this->info['height']) {
		return;
	}
	$scale = max($width / $this->info['width'], $height / $this->info['height']);
	/*
	if ($scale == 1 && $this->info['mime'] != 'image/png') {
		return;
	}*/
	//$new_width = (int)($this->info['width'] * $scale);
	//$new_height = (int)($this->info['height'] * $scale);
	$new_width=$width;
	$new_height=$height;
	$src_width=$new_width/$scale;
	$src_height=$new_height/$scale;
	$src_x=($this->info['width']-$src_width)/2;
	$src_y=($this->info['height']-$src_height)/2;
	$image_old = $this->image;
	//$this->image = imagecreatetruecolor($width, $height);
	// преобразуем, сохранив пропорции
	$this->image = imagecreatetruecolor($new_width, $new_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, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_width, $src_height);
	//imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $new_width, $new_height);
	imagedestroy($image_old);
	//$this->info['width']  = $width;
	// $this->info['height'] = $height;
	$this->info['width']  = $new_width;
	$this->info['height'] = $new_height;
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.