How to resize images proportionally / keeping the aspect ratio?

前端 未结 18 1671
野趣味
野趣味 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:08

    This issue can be solved by CSS.

    .image{
     max-width:*px;
    }
    
    0 讨论(0)
  • 2020-11-22 14:09

    The resize can be achieved(maintaining aspect ratio) using CSS. This is a further simplified answer inspired by Dan Dascalescu's post.

    http://jsbin.com/viqare

    img{
         max-width:200px;
     /*Or define max-height*/
      }
    <img src="http://e1.365dm.com/13/07/4-3/20/alastair-cook-ashes-profile_2967773.jpg"  alt="Alastair Cook" />
    
    <img src="http://e1.365dm.com/13/07/4-3/20/usman-khawaja-australia-profile_2974601.jpg" alt="Usman Khawaja"/>

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

    I think this is a really cool method:

     /**
      * Conserve aspect ratio of the original region. Useful when shrinking/enlarging
      * images to fit into a certain area.
      *
      * @param {Number} srcWidth width of source image
      * @param {Number} srcHeight height of source image
      * @param {Number} maxWidth maximum available width
      * @param {Number} maxHeight maximum available height
      * @return {Object} { width, height }
      */
    function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {
    
        var ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
    
        return { width: srcWidth*ratio, height: srcHeight*ratio };
     }
    
    0 讨论(0)
  • 2020-11-22 14:12

    There are 4 parameters to this problem

    1. current image width iX
    2. current image height iY
    3. target viewport width cX
    4. target viewport height cY

    And there are 3 different conditional parameters

    1. cX > cY ?
    2. iX > cX ?
    3. iY > cY ?

    solution

    1. Find the smaller side of the target view port F
    2. Find the larger side of the current view port L
    3. Find the factor of both F/L = factor
    4. Multiply both sides of the current port with the factor ie, fX = iX * factor; fY = iY * factor

    that's all you need to do.

    //Pseudo code
    
    
    iX;//current width of image in the client
    iY;//current height of image in the client
    cX;//configured width
    cY;//configured height
    fX;//final width
    fY;//final height
    
    1. check if iX,iY,cX,cY values are >0 and all values are not empty or not junk
    
    2. lE = iX > iY ? iX: iY; //long edge
    
    3. if ( cX < cY )
       then
    4.      factor = cX/lE;     
       else
    5.      factor = cY/lE;
    
    6. fX = iX * factor ; fY = iY * factor ; 
    

    This is a mature forum, I am not giving you code for that :)

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

    Does <img src="/path/to/pic.jpg" style="max-width:XXXpx; max-height:YYYpx;" > help?

    Browser will take care of keeping aspect ratio intact.

    i.e max-width kicks in when image width is greater than height and its height will be calculated proportionally. Similarly max-height will be in effect when height is greater than width.

    You don't need any jQuery or javascript for this.

    Supported by ie7+ and other browsers (http://caniuse.com/minmaxwh).

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

    If I understand the question correctly, you don't even need jQuery for this. Shrinking the image proportionally on the client can be done with CSS alone: just set its max-width and max-height to 100%.

    <div style="height: 100px">
    <img src="http://www.getdigital.de/images/produkte/t4/t4_css_sucks2.jpg"
        style="max-height: 100%; max-width: 100%">
    </div>​
    

    Here's the fiddle: http://jsfiddle.net/9EQ5c/

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