开发者

How to get the images aspect ratio using PHP?

I was wondering if a user uploads an image how can I get the aspect r开发者_开发技巧atio 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;
}


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);
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜