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. :- /
Moderator: General Moderators
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;
}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);
}