Image resize ++

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
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Image resize ++

Post by JKM »

Hi there!

Is it possible to add a background picture to a image if it's less than XX wide or YY high? And is it possible to center the uploaded image (both height and width)?

Code: Select all

// resize
 
$maxWidth = '100';
$maxHeight = '150';
 
function addBackground();
    add 'background.jpg';
}
 
if(($uploaded_file_width < $maxWidth) || ($uploaded_file_height < $maxHeight)) { // --> after resize!
    // Add background.jpg under the uploaded image
    if($uploaded_file_width < $maxWidth) {
        $get_width_space = $maxWidth/$imgWidth/2; // Finds the space between the image and the end of the background image
        // add padding/margin to the left and right of the uploaded image (value = $get_width_space)
    } else {
        $get_heigh_space = $maxHeight/$imgHeight/2; // Finds the space between the image and the end of the background image
        // add padding/margin to the top and bottom of the uploaded image (value = $get_height_space)
    }
}
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: Image resize ++

Post by Defiline »

It is possible.
I did it myself for my project.
At the moment I have no time to explain you how to do it.
If nobody answers you till I will be sleeping, tommorow I will try to show you example. :)
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: Image resize ++

Post by JKM »

Thanks a lot, mate!
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: Image resize ++

Post by Defiline »

I will not write to you PHP code.
I explain only algorithm:

We have two images:
1. Uploaded image (x = width; y = height)
2. Background (z = width; w = height)

Where is the center of the uploaded image?
1. x/2 = x center; y/2 = y center
2. z/2 = z center; w/2 = w center — this is our background

Okay.
Now we have to calculate where to place the uploaded image.
1. (z center) - (x center) = 0 point
2. (w center) - (y center) = 0 point

0 points is the start position of the uploaded image on the background.
Get it? :)
Post Reply