Welcome Guest, Not a member yet? Register   Sign In
Image cropping challenge
#8

[eluser]Colin Williams[/eluser]
Cross-multiply and divide. Very simple algebra... like, 5th or 6th grade.

Code:
$ratio_x = 240;
$ratio_y = 145;
list($image_x, $image_y) = getimagesize($image_filepath);

if ($ratio_x > $ratio_y)
{
   $image_y = ($image_x * $ratio_y) / $ratio_x;
}
else
{
   $image_x = ($image_y * $ratio_x) / $ratio_y;
}

// Now $image_x and $image_y are the desired proportions for your final photo (one won't have changed)

Actually, there's a bit more to it. You have to make sure the new dimensions aren't larger than the original (you could even check before you started the calculation.)

Do it on paper first, then turn it into code.


Messages In This Thread
Image cropping challenge - by El Forum - 02-14-2009, 08:40 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:11 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:11 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:14 AM
Image cropping challenge - by El Forum - 02-14-2009, 10:21 AM
Image cropping challenge - by El Forum - 02-15-2009, 10:09 AM
Image cropping challenge - by El Forum - 02-15-2009, 10:26 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:22 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:27 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:32 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:44 AM
Image cropping challenge - by El Forum - 02-16-2009, 12:46 AM
Image cropping challenge - by El Forum - 02-22-2009, 08:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB