Page 1 of 1

Image resize ++

Posted: Tue May 05, 2009 6:49 pm
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)
    }
}

Re: Image resize ++

Posted: Tue May 05, 2009 7:10 pm
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. :)

Re: Image resize ++

Posted: Wed May 06, 2009 8:21 am
by JKM
Thanks a lot, mate!

Re: Image resize ++

Posted: Wed May 06, 2009 8:38 am
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? :)