maxlk
maxlk

Reputation: 1057

Get a image height according to width using php

I want to get a image size according to the width and also I'm resizing the width if more then 500px

Here is my code so far

list($width, $height) = getimagesize("MyImage.jpg");

if ($width<300){
    $NewWidth = $width;

}else{ 
    $NewWidth = '500';

}

Just like to know how to get new height according to the width. Really appreciate your help or any advice you can give.

Upvotes: 0

Views: 1028

Answers (2)

pratik nagariya
pratik nagariya

Reputation: 574

Try this formula to maintain 4:3 aspect ratio.

new height = ((width X 3) / 4)

Upvotes: 0

Nathan Dawson
Nathan Dawson

Reputation: 19308

You need to get current image scale and then use that to calculate the new height from the new width. Divide the width by the height to get the scale. Then divide the new width by the scale to get the new (proportional) height.

list($width, $height) = getimagesize("MyImage.jpg");

// Get the image scale.
$scale = $width / $height;

// Existing width modification.
if ($width<300){
    $NewWidth = $width;

}else{ 
    $NewWidth = '500';

}

// Get new height from new width.
$NewHeight = $NewWidth / $scale;

Upvotes: 3

Related Questions