Page 1 of 1

resize image + white background

Posted: Wed Jun 06, 2012 6:42 am
by dotphp
I have a php function that resize an image:

Code: Select all

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;			
		
	}	

	$newImage = imagecreatetruecolor($new_width,$new_height);
	$source = imagecreatefromjpeg($this->image_location);
	imagecopyresampled($newImage,$source,0,0,0,0,$new_width,$new_height,$width,$height);
	imagejpeg($newImage,$this->new_location,85);
	chmod($this->new_location, 0777);
	return $this->new_location;
}

If I use a picture that have 500x300 , and after re-size I have something 200x100 , I don't want to save it with this dimensions.
I want to put this picture with 200x100 over a white square 200x200 vertical align.

How can I change this code for this ?

Thanks

Re: resize image + white background

Posted: Wed Jun 06, 2012 10:58 am
by pickle
When you call imagecreatetruecolor(), don't use $new_width and $new_height, as that will make it 200 x 100. Figure out which is the larger dimension and use that twice:

Code: Select all

$max_dim = ($new_width > $new_height) ? $new_width : $new_height;
$newImage = imagecreatetruecolor($max_dim,$max_dim);
Also, by default a new image has a black background, so you'll need to draw white on it.

Re: resize image + white background

Posted: Wed Jun 06, 2012 1:22 pm
by dotphp
I believe that I need a white picture with 200x200, and over to put the new 200x120 and save as one picture.
How can I do that in code ?

Re: resize image + white background

Posted: Wed Jun 06, 2012 1:49 pm
by pickle
You've already got imagecopyresampled(), and Google can help you with figuring out how to make your new picture have a white background.

Re: resize image + white background

Posted: Thu Jun 07, 2012 8:30 am
by dotphp
Solved.

Code: Select all

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,85);
	chmod($this->new_location, 0777);
	return $this->new_location;
}