tags:

views:

61

answers:

2

I was wondering if a user uploads an image how can I get the aspect ratio of that image when creating the thumb. I know my width will be 180px but how can I get the height.

Here is the code I got so far listed below.

list($width, $height) = getimagesize($file) ;

if ($width >= 180){
 $modwidth = 180;
 $modheight = ;
} else {
 $modwidth = $width;
 $modheight = $height;
}
+1  A: 

you want to keep the same aspect ratio

so something like that $modheight = ((180.0/$width) * $height);

it would give you a picture with 180 wide and whatever height but with same ratio as the original one.

RageZ
A: 

Are you trying to do something like this?

$targetsize = $x = $y = 180;
list($width, $height) = getimagesize($file);

if($width > $targetsize || $height > $targetsize) {
    $aspect = $width / $height;

    if($aspect < 1) $x *= $aspect; // portrait
    else $y /= $aspect; // landscape

    resizeImageFunctionHere($file, $x, $y);
}

But if you always want 180 wide regardless of whether the photo is portrait or not:

$targetwidth = $x = $y = 180;
list($width, $height) = getimagesize($file);

if($width > $targetsize) {
    $aspect = $width / $height;
    $y *= $aspect;

    resizeImageFunctionHere($file, $x, $y);
}
Rob
I'm trying to resize images that have a width larger then 180px if the images width is not larger then 180px keep the image as how it is. In-order to make thumb. Does that make sense?
ImAGe
looks good to me, just be sure to check for x/zero !
echo
Got it, added in the size check. The first example checks the orientation and sizes to not more than $targetsize, while the second sizes to $targetsize wide regardless of orientation.
Rob