Jump to content

resize image + white background


raduenea

Recommended Posts

I have a php function that resize an image:

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

Link to comment
Share on other sites

resize_image_and_pad(200,200);

function resize_image_and_pad($width,$height){	

$img_width = $this->getWidth($this->image_location);
$img_height = $this->getHeight($this->image_location);
$img_scale = $img_width/$img_height;	
        	
$new_width = null;
$new_height = null;

$scale = $width/$height;

if($scale < $img_scale){
	$new_width = $width;
	$new_height = $new_width * $img_scale;
}else{
	$new_height = $height;
	$new_width = $new_height * $img_scale;
}

$x = ($width - $new_width) / 2;
$y = ($height - $new_height) / 2;


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

Link to comment
Share on other sites

The code that you change it doesn't return me anything.

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 ?

My code does exactly what you want it to.  It may have some bugs, but it is correct.  Debug it and be happy.

Link to comment
Share on other sites

I figure it out. here is the final 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,85);
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.