Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:55:33+00:00 2026-06-06T00:55:33+00:00

So after extensive research and tons of jQuery and Javascript solutions I simply could

  • 0

So after extensive research and tons of jQuery and Javascript solutions I simply could not find a way in which to dynamically scale images to a specified size both horizontally and vertically, I found tons of information on scaling to fit width wise and keep the aspect ratio, or scaling to fit height wise and keep the aspect ratio, but couldn’t figure out whether the image was too tall or too short and adjust accordingly.

So in my example, I had a <div> with a set width of 460px and a set height of 280px, and i need the image to fit, all of itself into that area without stretching (maintaining its aspect ratio)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:55:34+00:00Added an answer on June 6, 2026 at 12:55 am

    Now after fiddling around with some width examples my classic algebra skills kicked in.

    If I took the width and divided it by the height, so in this case, 460/280 you in return get 1.642… which is the aspect ratio of that area, now if I looked at the aspect ratio of the image, I knew that if it was greater than 1.642… that that meant it was wider than the area, and if the aspect ratio of the image was less than, that it was taller.

    So I came up with the following,

    // Set the Image in question
    $image = 'img/gif/b47rz.gif';
    
    // Set the width of the area and height of the area
    $inputwidth = 460;
    $inputheight = 280;
    
    // Get the width and height of the Image
    list($width,$height) = getimagesize($image);
    
    // So then if the image is wider rather than taller, set the width and figure out the height
    if (($width/$height) > ($inputwidth/$inputheight)) {
                $outputwidth = $inputwidth;
                $outputheight = ($inputwidth * $height)/ $width;
            }
    // And if the image is taller rather than wider, then set the height and figure out the width
            elseif (($width/$height) < ($inputwidth/$inputheight)) {
                $outputwidth = ($inputheight * $width)/ $height;
                $outputheight = $inputheight;
            }
    // And because it is entirely possible that the image could be the exact same size/aspect ratio of the desired area, so we have that covered as well
            elseif (($width/$height) == ($inputwidth/$inputheight)) {
                $outputwidth = $inputwidth;
                $outputheight = $inputheight;
                }
    // Echo out the results and done
    echo '<img src="'.$image.'" width="'.$outputwidth.'" height="'.$outputheight.'">';
    

    And it worked perfectly, so I thought I would share, hopefully this helps some people

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After an extensive search I have been unable to find any information on this
After extensive research and fun learning about Google's mapping api, I am building a
This should have been a simple thing to sort but even after extensive research
Extensive research did not lead me to understanding why the below happens - I
So after extensive research on the subject, I've come up with the following bit
after doing some research i'm still not sure if using mysql with .net desktop
In Android (after building), we find in the bin folder a file of extension
After discovering about Javascript namespaces, I tried to implement them but I run into
I do not have extensive experience with gdb, so I am not sure if
[I split the original question into two after more testing and research] I defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.