0

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.

2
  • Are you talking about proportional scaling? Like in doing math? Commented May 17, 2014 at 12:44
  • @PeeHaa yes even if you can tell me the math that will be fine. Commented May 17, 2014 at 12:45

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this formula to maintain 4:3 aspect ratio.

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

6 Comments

don't you need the original height also to get the correct new height?
No, It does not required.
@pratiknagariya You're misunderstanding the OPs question. He's not asking how to get maintain a 4:3 ratio.
@Nathan Dawson For the correct height we have to maintain image resolution ratio and 4:3 is the standard for image. And that can be changed based on the requirement.
That's wrong. Please see my answer. The OP never stated the image was 4:3. You need to detect the scale not force it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.