1

I was trying to figure out what is the best, if it can be done, and desired method to take an image that is uploaded and trim the image for a thumb in CSS. If it cannot be done in pure CSS what is the method to do it in JavaScript/jQuery? The images may be different sizes but I am looking for a way that an image will square on center, and then reduce to fit. Example below:

This image is 413 x 300.

enter image description here

If this image was trimmed from the left and right for the portfolio thumb it would be 300 X 300:

enter image description here

Then the image needs to be reduced for the thumb 200 x 200 or what ever value the thumb is set to display:

enter image description here

EDIT my understanding if #img_preview{width:200px;} is applied it would result in this:

enter image description here

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

4 Answers4

5

example here: http://jsfiddle.net/cnWqQ/5/

css like this:

#img-wrap{
    height:200px;
    width: 200px;
    background-image: url('https://i.stack.imgur.com/yQ1j8.jpg');
    background-size: cover;
    background-position:center; 
}

html like so:

<div id="img-wrap"></div>

Works by putting the images as the background in a div, works for all image shapes and sizes consistently.

it involves some css3.

3

You can do it in CSS, but it will only work with modern browsers :

You'll use background-image property :

<div id="myImageTrimed">
</div>

and the css :

#myImageTrimed {
    background-image: url('img/youImage.jpg');
    background-position: center; /* to make sure it trims the borders */
    background-size: cover; /* As large as possible */
    height: 200px; /* But only 200x200px are shown */
    width: 200px;
} 

Please comment if you have more browser constraints.

Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
0

Just set the CSS width to the value you need, the height will be automatically adjusted to maintain the aspect ratio.

#img_preview{
    width:200px;
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

You can mask the image with a div:

Your div: height:200px; width:200px; overflow: hidden;

Your image: position:absolute; height:inherit; margin-left:-15%;

see this demo below: http://jsfiddle.net/jRCgP/

otinanai
  • 3,987
  • 3
  • 25
  • 43