Jump to content

image resize/crop


raduenea

Recommended Posts

I have a code that make a re-size and crop to a picture.

First he resize the picture and after that put the picture in the middle of the white background.

 

The only problem is if a picture is vertical add black color in the right side instead of white.

1376587750.jpg

 

 

For horizontal pictures it's ok.

1376466670.jpg

 

Why is adding the black color in the right side ?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/281359-image-resizecrop/
Share on other sites

I forgot to add the code:

function resize_image($max_width,$max_height){	

	$width = $this->getWidth($this->image_location);
	$height = $this->getHeight($this->image_location);
		
	$new_width = "";
	$new_height = "";
	
	$with_scale = $width/$max_width;
	$height_scale = $height/$max_height;	
		
	if($with_scale > $height_scale){
		$new_width = $max_width;
		$new_height = ($max_width/$width) * $height;			
		
	}else{
		$new_height = $max_height;
		$new_width = ($max_height/$height) * $width;			
		
	}	

        $x_mid  = $new_width / 2;
        $y_mid  = $new_height / 2; 

	$newImage = imagecreatetruecolor($new_width,$new_height);
	$source = imagecreatefromjpeg($this->image_location);
	imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height);
	
	
    $final = imagecreatetruecolor($max_width, $max_height);
    imagecopyresampled($final, $newImage, 0, 0, ($x_mid - ($max_width / 2)), ($y_mid - ($max_height / 2)), $max_width, $max_height, $max_width, $max_height);
	$bg_color = imagecolorallocate ($final, 255, 255, 255);
	imagefill($final, 0, 0, $bg_color);
	
	
	imagejpeg($final,$this->new_location,80);
	chmod($this->new_location, 0777);
	return $this->new_location;
}

Link to comment
https://forums.phpfreaks.com/topic/281359-image-resizecrop/#findComment-1445872
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.