resize image + white background

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

resize image + white background

Post 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
Last edited by pickle on Wed Jun 06, 2012 10:55 am, edited 1 time in total.
Reason: Changing code to syntax tags
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: resize image + white background

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: resize image + white background

Post 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 ?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: resize image + white background

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
dotphp
Forum Commoner
Posts: 25
Joined: Sat Nov 10, 2007 8:03 am

Re: resize image + white background

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

Post Reply