@damarkuzz

Как избавиться от черных краев при обрезке фото PHP GD?

Как избавиться от черных краев, которые получаются при обрезке предварительно измененного в размерах фото?
Необходимо, чтобы фото заполняло холст.
61e6c441c7167201763410.jpeg

<?
	$filename = 'picture.jpg';
	$filename_save = 'picture_new.jpg';
	
	$info   = getimagesize($filename);
	$width  = $info[0];
	$height = $info[1];
	$type   = $info[2];
	 
	switch ($type) { 
		case 1: 
			$img = imageCreateFromGif($filename);
			imageSaveAlpha($img, true);
			break;					
		case 2: 
			$img = imageCreateFromJpeg($filename);
			break;
		case 3: 
			$img = imageCreateFromPng($filename); 
			imageSaveAlpha($img, true);
			break;
	}
	
	#параметры изменений
		##изменяем размер
			$w = 265;
			$h = 0;
			 
			if (empty($w)) {
				$w = ceil($h / ($height / $width));
			}
			if (empty($h)) {
				$h = ceil($w / ($width / $height));
			}
			 
			$tmp = imageCreateTrueColor($w, $h);
			if ($type == 1 || $type == 3) {
				imagealphablending($tmp, true); 
				imageSaveAlpha($tmp, true);
				$transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127); 
				imagefill($tmp, 0, 0, $transparent); 
				imagecolortransparent($tmp, $transparent);    
			}   
			 
			$tw = ceil($h / ($height / $width));
			$th = ceil($w / ($width / $height));
			if ($tw < $w) {
				imageCopyResampled($tmp, $img, ceil(($w - $tw) / 2), 0, 0, 0, $tw, $h, $width, $height);        
			} else {
				imageCopyResampled($tmp, $img, 0, ceil(($h - $th) / 2), 0, 0, $w, $th, $width, $height);    
			}
			$img = $tmp;
	
	#сохранение фото
		switch ($type) {
			case 1:
				imageGif($img, $filename_save);
				break;			
			case 2:
				imageJpeg($img, $filename_save, 100);
				break;			
			case 3:
				imagePng($img, $filename_save);
				break;
		}
		 
		imagedestroy($img);
		
		
################

		$info   = getimagesize($filename_save);
		$width  = $info[0];
		$height = $info[1];
		$type   = $info[2];
		 
		switch ($type) { 
			case 1: 
				$img = imageCreateFromGif($filename_save);
				imageSaveAlpha($img, true);
				break;					
			case 2: 
				$img = imageCreateFromJpeg($filename_save);
				break;
			case 3: 
				$img = imageCreateFromPng($filename_save); 
				imageSaveAlpha($img, true);
				break;
		}
		##обрезаем
		 $w = 265;
		 $h = 198;

		 $x = '50%';
		 $y = '50%';

		 if (strpos($x, '%') !== false) {
			 $x = intval($x);
			 $x = ceil(($width * $x / 100) - ($w / 100 * $x));
		 }
		 if (strpos($y, '%') !== false) {
			 $y = intval($y);
			 $y = ceil(($height * $y / 100) - ($h / 100 * $y));
		 }

		 $tmp = imageCreateTrueColor($w, $h);
		 if ($type == 1 || $type == 3) {
			 imagealphablending($tmp, true); 
			 imageSaveAlpha($tmp, true);
			 $transparent = imagecolorallocatealpha($tmp, 0, 0, 0, 127); 
			 imagefill($tmp, 0, 0, $transparent); 
			 imagecolortransparent($tmp, $transparent);    
		 }

		 imageCopyResampled($tmp, $img, 0, 0, $x, $y, $width, $height, $width, $height);
		 $img = $tmp;
	
	#сохранение фото 2
		switch ($type) {
			case 1:
				imageGif($img, $filename_save);
				break;			
			case 2:
				imageJpeg($img, $filename_save, 100);
				break;			
			case 3:
				imagePng($img, $filename_save);
				break;
		}
		 
		imagedestroy($img);
  • Вопрос задан
  • 73 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы