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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.