title: Full Bleed Image Resize Calculation
body: <p>I am trying to write a JavaScript function that will expand an image to fill a div always (so crop top or sides as needed). It is the JavaScript equivalent of the CSS3 code background-size: cover.</p>
  <p>I can't for the life of me figure it out. This is what I have so far:</p>
  <pre><code>    function full_bleed(box_width, box_height, new_width, new_height) 
      {
          var aspect_ratio=new_width/new_height;
                  
          if(new_height&lt;box_height) {
                          
              new_height=box_height;
              new_width=Math.round(new_height*aspect_ratio);            
              
          }
          
          if(new_width&lt;box_width) {
  
              new_width=box_width;
              new_height=Math.round(new_width/aspect_ratio);
          }
          
          return {
              width: new_width, 
              height: new_height
          };
      
      }
  </code></pre>
  <p>I figured one of you guys might have the equation lying around.</p>