Reputation: 38795
Hi
Suppose I have several image with different width and height, I want all of them to fit in the img tag with 200 width and 200 height and direct proportion(image won't be distortion).
How can I do it in PHP or Jquery?
Thanks
Upvotes: 0
Views: 672
Reputation: 359966
var max = 200;
$('img').each(function ()
{
var $this = $(this);
if ($this.height() > $this.width())
{
$this.height(max);
}
else
{
$this.width(max);
}
});
http://jsfiddle.net/mattball/qtVkT/
Upvotes: 1
Reputation:
This will constrain all images with class 'image' to be <= 200x200px
If you want it to be in a perfect 200x200 box, wrap it in a 200x200px div
$('.image').each(function(){
var $this = $(this);
$this[$this.width() > $this.height() ? 'width' : 'height'](200);
});
Upvotes: 1
Reputation: 14365
From this tutorial:
IMO resizing images with jQuery is a bad practice, i advise you shouldn't do that if you can use GD library.
Upvotes: 0