Link to home
Start Free TrialLog in
Avatar of anna27
anna27

asked on

Advanced maths question (for GD in PHP)

Hi!
I need someone who is very good with maths for this because I cannot figure it out with my basic maths ...I guess I should have been paying more attention in school!

Anyway, I have 2 scripts, one that resizes an image according to the height and width i specify and another that cuts a w=90 h=120 thumb from *within* an image according to the x and y parameters I send it. This functionality is distributed in 2 scripts.

I think I just basically need 2 functions that take the height and width and  return the calculated  height and width...

Heres my maths problems in 2 parts:

Problem 1)
I need to scale down an image (eg: example.jpg) to height of 120 pixels while maintaining the images proportions.

eg:
if i have an image of (height and width) 800x600 what would the dimensions be if the height was 120?

usually for resizeing I just do something like this:

if($image_height > 400)
{$image_height=$image_height / 2;
 $image_width=$image_width / 2;}

which gets me a pretty decent image....but now I need an image no more than 120px in height with a proportionate width.


Problem 2)
After getting the above image now I need the x and y parameters to cut a h120 x w90 thumb *from the center of the image*

eg: if from problem 1 the result is an image of 120 x 160 then x=35 and y=125 (I think, i told you i suck at maths.. :-D )


Anyone up to the challenge? or just want to help a poor person with some maths? :-)

Thanks,
Anna
ASKER CERTIFIED SOLUTION
Avatar of minichicken
minichicken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of minichicken
minichicken

Sorry I think my calculation for problem 1's width is incorrect, i should divide and not multiply, here it again.

Problem 1:

$img_file = "my_image.jpg";
$src_img = imagecreatefromjpeg($img_file);

$src_width = imagesx($src_img); //original width
$src_height = imagesy($src_img); //original height

$src_ratio = $src_height / $src_width; //get aspect ratio of image e.g. 800/600 = 1.333333....
         
$dest_height = 120; //pre set the height to 120px
$dest_width = $dest_height / $src_ratio; //calculate proportional width based on height
                   
$resize_img = imagecreatetruecolor($dest_width, $dest_height);
imagecopyresized($resize_img, $src_img, 0,0,0,0, $dest_width, $dest_height, $src_width, $src_height);
Problem 1)

$h_1 = 600;
$w_1 = 800;

$h_2 = 120;
$w_2 = $w_1 * ($h_2 / $h_1);


Problem 2 a)

Assuming you're wanting the central portion of the original image
(which
I think you are from what you've written)

$h_1 = 600;
$w_1 = 800;

$h_2 = 120;
$w_2 = 90;

$x = ($h_1 - $h_2) / 2;
$y = ($w_1 - $w_2) / 2;

Problem 2 b)

If you're in fact trying to get the central portion of the new
thumbnail

$h_1 = 600;
$w_1 = 800;

$h_2 = 120;
$w_2 = $w_1 * ($h_2 / $h_1);  // need to have the correct thumbnail
width to start from

$x = ($w_2 - 90) / 2;
$y = 0;  // because the image is only 120px high


Hope this helps

Cheers
Mag
dont quite understand your question 2.... can you provide more detail.... thanks...
Have you tried problem 1 possible solution above?
Avatar of anna27

ASKER

Hi Minichicken/Mag,
Thank you for replying.

Minichcken, your way was a bit too complicated and I just pretty much needed the maths so I took Mag's answer (and its working for me), but since you took the time to help me out I will be splitting the points between you guys.

Thanks!
Anna