How to resize images proportionally / keeping the aspect ratio?

前端 未结 18 1673
野趣味
野趣味 2020-11-22 13:58

I have images that will be quite big in dimension and I want to shrink them down with jQuery while keeping the proportions constrained, i.e. the same aspect ratio.

C

相关标签:
18条回答
  • 2020-11-22 14:15

    2 Steps:

    Step 1) calculate the ratio of the original width / original height of Image.

    Step 2) multiply the original_width/original_height ratio by the new desired height to get the new width corresponding to the new height.

    0 讨论(0)
  • 2020-11-22 14:19

    If the image is proportionate then this code will fill the wrapper with image. If image is not in proportion then extra width/height will get cropped.

        <script type="text/javascript">
            $(function(){
                $('#slider img').each(function(){
                    var ReqWidth = 1000; // Max width for the image
                    var ReqHeight = 300; // Max height for the image
                    var width = $(this).width(); // Current image width
                    var height = $(this).height(); // Current image height
                    // Check if the current width is larger than the max
                    if (width > height && height < ReqHeight) {
    
                        $(this).css("min-height", ReqHeight); // Set new height
                    }
                    else 
                        if (width > height && width < ReqWidth) {
    
                            $(this).css("min-width", ReqWidth); // Set new width
                        }
                        else 
                            if (width > height && width > ReqWidth) {
    
                                $(this).css("max-width", ReqWidth); // Set new width
                            }
                            else 
                                (height > width && width < ReqWidth)
                    {
    
                        $(this).css("min-width", ReqWidth); // Set new width
                    }
                });
            });
        </script>
    
    0 讨论(0)
  • 2020-11-22 14:20

    Here's a correction to Mehdiway's answer. The new width and/or height were not being set to the max value. A good test case is the following (1768 x 1075 pixels): http://spacecoastsports.com/wp-content/uploads/2014/06/sportsballs1.png. (I wasn't able to comment on it above due to lack of reputation points.)

      // Make sure image doesn't exceed 100x100 pixels
      // note: takes jQuery img object not HTML: so width is a function
      // not a property.
      function resize_image (image) {
          var maxWidth = 100;           // Max width for the image
          var maxHeight = 100;          // Max height for the image
          var ratio = 0;                // Used for aspect ratio
    
          // Get current dimensions
          var width = image.width()
          var height = image.height(); 
          console.log("dimensions: " + width + "x" + height);
    
          // If the current width is larger than the max, scale height
          // to ratio of max width to current and then set width to max.
          if (width > maxWidth) {
              console.log("Shrinking width (and scaling height)")
              ratio = maxWidth / width;
              height = height * ratio;
              width = maxWidth;
              image.css("width", width);
              image.css("height", height);
              console.log("new dimensions: " + width + "x" + height);
          }
    
          // If the current height is larger than the max, scale width
          // to ratio of max height to current and then set height to max.
          if (height > maxHeight) {
              console.log("Shrinking height (and scaling width)")
              ratio = maxHeight / height;
              width = width * ratio;
              height = maxHeight;
              image.css("width", width);
              image.css("height", height);
              console.log("new dimensions: " + width + "x" + height);
          }
      }
    
    0 讨论(0)
  • 2020-11-22 14:24

    Resize to fit the container, get scale factor, scale down percentage control

     $(function () {
                let ParentHeight = 200;
                let ParentWidth = 300;
                $("#Parent").width(ParentWidth).height(ParentHeight);
                $("#ParentHeight").html(ParentHeight);
                $("#ParentWidth").html(ParentWidth);
    
                var RatioOfParent = ParentHeight / ParentWidth;
                $("#ParentAspectRatio").html(RatioOfParent);
    
                let ChildHeight = 2000;
                let ChildWidth = 4000;
                var RatioOfChild = ChildHeight / ChildWidth;
                $("#ChildAspectRatio").html(RatioOfChild);
    
                let ScaleHeight = ParentHeight / ChildHeight;
                let ScaleWidth = ParentWidth / ChildWidth;
                let Scale = Math.min(ScaleHeight, ScaleWidth);
    
                $("#ScaleFactor").html(Scale);
                // old scale
                //ChildHeight = ChildHeight * Scale;
                //ChildWidth = ChildWidth * Scale;
    
                // reduce scale by 10%, you can change the percentage
                let ScaleDownPercentage = 10;
                let CalculatedScaleValue = Scale * (ScaleDownPercentage / 100);
                $("#CalculatedScaleValue").html(CalculatedScaleValue);
    
                // new scale
                let NewScale = (Scale - CalculatedScaleValue);
                ChildHeight = ChildHeight * NewScale;
                ChildWidth = ChildWidth * NewScale;
    
                $("#Child").width(ChildWidth).height(ChildHeight);
                $("#ChildHeight").html(ChildHeight);
                $("#ChildWidth").html(ChildWidth);
    
            });
            #Parent {
                background-color: grey;
            }
    
            #Child {
                background-color: red;
            }
     
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="Parent">
        <div id="Child"></div>
    </div>
    
    <table>
        <tr>
            <td>Parent Aspect Ratio</td>
            <td id="ParentAspectRatio"></td>
        </tr>
        <tr>
            <td>Child Aspect Ratio</td>
            <td id="ChildAspectRatio"></td>
        </tr>
        <tr>
            <td>Scale Factor</td>
            <td id="ScaleFactor"></td>
        </tr>
        <tr>
            <td>Calculated Scale Value</td>
            <td id="CalculatedScaleValue"></td>
        </tr>
        <tr>
            <td>Parent Height</td>
            <td id="ParentHeight"></td>
        </tr>
        <tr>
            <td>Parent Width</td>
            <td id="ParentWidth"></td>
        </tr>
        <tr>
            <td>Child Height</td>
            <td id="ChildHeight"></td>
        </tr>
        <tr>
            <td>Child Width</td>
            <td id="ChildWidth"></td>
        </tr>
    </table>

    0 讨论(0)
  • 2020-11-22 14:24

    This totally worked for me for a draggable item - aspectRatio:true

    .appendTo(divwrapper).resizable({
        aspectRatio: true,
        handles: 'se',
        stop: resizestop 
    })
    
    0 讨论(0)
  • 2020-11-22 14:25

    actually i have just run into this problem and the solution I found was strangely simple and weird

    $("#someimage").css({height:<some new height>})
    

    and miraculously the image is resized to the new height and conserving the same ratio!

    0 讨论(0)
提交回复
热议问题