Page 1 of 1
I need help with image math.
Posted: Fri Jul 21, 2006 2:14 am
by daedalus__
I'm really bad with math and I need help
Let's say I have an image:
image.jpg
On my page, image.jpg can be a maximum of 760 pixels wide. Now, when I display it at 760 pixels wide, I want it to be proporational still but I can't figure out the math.
Please help, I'm hopeless. :- /
Posted: Fri Jul 21, 2006 2:35 am
by Benjamin
What was that you said earlier about reading the manual?
This should work for ya..
Code: Select all
list($width_orig, $height_orig) = getimagesize($MoveImageToPath);
if ($MaxWidthProductThumbnail && ($width_orig < $height_orig)) {
$width = ($MaxHeightProductThumbnail / $height_orig) * $width_orig;
$height = $MaxHeightProductThumbnail;
} else {
$height = ($MaxWidthProductThumbnail / $width_orig) * $height_orig;
$width = $MaxWidthProductThumbnail;
}
Posted: Fri Jul 21, 2006 2:39 am
by daedalus__
I know but I am honestly so bad at math the thought of it is more intimidating than mike tyson.
edit: i just realised that sounded stupid, ia m tired.
someone delete this topic lol
Posted: Fri Jul 21, 2006 2:41 am
by Benjamin
Keep in mind that if the image is smaller than your minimum sizes, the sizes will be invalid. You should add code that checks to make sure that either the height or width is larger than 760 pixels.
Posted: Fri Jul 21, 2006 2:44 am
by daedalus__
Fixed and done, thanks so much.
Posted: Fri Jul 21, 2006 5:26 am
by MarK (CZ)
This is what I'm using:
Code: Select all
function ImageFitDimensions($src_w, $src_h, $dst_w, $dst_h) {
// returns dimensions of an image to fit into defined rectangle
$ratio_img = $src_w / $src_h;
if ($src_w <= $dst_w && $src_h <= $dst_h) {
$w = $src_w;
$h = $src_h;
} else {
if ($src_w > $dst_w) {
$ratio_res = $dst_w / $src_w;
$w = $src_w * $ratio_res;
$h = $src_h * $ratio_res;
} else if ($src_h > $dst_h) {
$ratio_res = $dst_h / $src_h;
$w = $src_w * $ratio_res;
$h = $src_h * $ratio_res;
}
if ($w > $dst_w) {
$ratio_res = $dst_w / $src_w;
$w = $src_w * $ratio_res;
$h = $src_h * $ratio_res;
} else if ($h > $dst_h) {
$ratio_res = $dst_h / $src_h;
$w = $src_w * $ratio_res;
$h = $src_h * $ratio_res;
}
}
$w = Round($w);
$h = Round($h);
if ($w == 0) $w = 1;
if ($h == 0) $h = 1;
return Array($w, $h);
}
I hope it's not much more complicated that it should be

Probably could be simplified.