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

  • Home
  • SEARCH
  • 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 6776489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:02:08+00:00 2026-05-26T16:02:08+00:00

I am trying to write a javascript function to resize an image based on

  • 0

I am trying to write a javascript function to resize an image based on a given area (or in my case (somewhat inaccurate) ‘average dimension’ since that’s easier to think in terms of. Rather than feeding in maximum height and width, I want to feed in maximum area so that long or narrow images will appear visually to be roughly the same size.

enter image description here

I’m getting really caught on the math aspect of it, though… just how to logic it, as I haven’t done much math of late.

Basically, given an aspect ratio I want to determine the maximum size within an area.

Something like this:

function resizeImgByArea(img, avgDimension){
    var w = $(img).width();
    var h = $(img).height();
    var ratio = w/h;
    var area = avgDimension * avgDimension;
    var targetHeight //something involving ratio and area
    var targetWidth //something involving ratio and area
    $(img).width(targetWidth);
    $(img).height(targetHeight);
}

Not sure if this is on topic here, but I’m not able to brain it.

  • 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-05-26T16:02:09+00:00Added an answer on May 26, 2026 at 4:02 pm

    Here is the function I came up with that’s simpler than some mentioned and does what I need. It constrains to a set maxWidth, but not height because of the particular requirements I was using.. it would probly be appropriate to throw on a maxHeight as well as well as some cleanup, but it gets ‘er done.

    function resizeImgByArea(imgId, avgDimension){
       var node, w, h, oldArea, oldAvgDimension, multiplicator, targetHeight, targetWidth, defAvgDimension;
       node = $('#' + imgId);
       node.css('width', '').css('height', '');
       var maxW = $('#' + imgId).css('maxWidth');
       if (maxW){
           defAvgDimension = maxW;
       } else {
           defAvgDimension = 200;
       }
       avgDimension = (typeof avgDimension == "undefined")?'defAvgDimension':avgDimension;
       w = node.width();
       h = node.height();
       oldArea = w*h;
       oldAvgDimension = Math.sqrt(oldArea);
       if (oldAvgDimension > avgDimension){
           multiplicator = avgDimension / oldAvgDimension;
           targetHeight = h * multiplicator;
           targetWidth = w * multiplicator;
           node.width(targetWidth);
           node.height(targetHeight);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a JavaScript function that will expand an image to
I'm trying to write a javascript function that calculates the time since Oct 11th,
I am trying to write a JavaScript function that will return its first argument(function)
I'm trying to write a javascript function that adds some DOM nodes to the
I am trying to write a javascript function with an if statement that will
I'm trying to write javascript to find page elements relative to a given element
I'm trying to write a Javascript function to edit content from clipboard before pasting.
I am trying to write a JavaScript function to get the current browser width.
I am trying to write a javascript function to parse a bit of the
I am new to programming and trying to write a javascript function to set

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.