开发者

How to get the images aspect ratio using PHP?

开发者 https://www.devze.com 2022-12-11 03:13 出处:网络
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.

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

精彩评论

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