Search code examples
javascriptjqueryhtmlcssscale

Change the dimensions of a image in jQuery


Is there a way to change (making bigger) the size of image without compromising the quality of the image in jQuery?


Solution

  • This is just resize, but you can't resize them without losing quality.

    var max_size = 200;
    
    $("img").each(function(i) {
      if ($(this).height() > $(this).width()) {
        var h = max_size;
        var w = Math.ceil($(this).width() / $(this).height() * max_size);
      } 
      else {
        var w = max_size;
        var h = Math.ceil($(this).height() / $(this).width() * max_size);
      }
    
      $(this).css({ height: h, width: w });
    
    });
    

    More examples: