Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fix image size without re-sizing

I have a div which is width:500px; and height:170px;

The div contains a jquery slideshow which gets random images from my database and displays them.

Problem is that the images are uploaded by users and can be any aspect-ratio/any size.

I need the images to fit the div without being resized using

style='height:x; width:x;

Basically, I want all images to be displayed properly and in acutal quality.

I don't want to restrict which sizes of images can be uploaded as they as displayed in other parts of the site in full size/quality.

Can any provide a solution?

like image 790
andrew anderson Avatar asked Apr 17 '12 18:04

andrew anderson


People also ask

How can I change the size of an image without losing quality?

The higher the resolution, the more pixels there are in the image, and the better the quality. If you want to resize an image without losing quality, you need to make sure that the "Resample Image" checkbox is unchecked. This checkbox tells Photoshop to change the number of pixels in the image.

How do I fix the size of an image in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How can I increase the size of an image without changing pixels?

Choose Image > Resize > Image Size. Make sure that Resample Image is deselected. If deselected, you can change the print dimensions and resolution without changing the total number of pixels in the image, but the image may not keep its current proportions.


2 Answers

You could use CSS to set the size of the images in the slideshow without necessarily changing the aspect ratio of the image.

  • set the height of the image to the height of the slideshow container

  • make the width auto so it maintains its aspect ratio

  • set a max-width so it doesn't get wider than the slideshow

slideshow.img{
    height:170px
    width:auto;/*maintain aspect ratio*/
    max-width:500px;
}

Note the images might be slimmer than the slideshow if the original size is small.

like image 109
Richard A Avatar answered Nov 03 '22 14:11

Richard A


You should be able to use CSS's max-width property. You won't want to specify the width or height if you do this.

Your HTML:

<div id="yourContainer">
    <img src="imagePath" />
</div>

And your CSS:

#yourContainer {
    width: 500px;
    height: 170px;
}

#yourContainer img {
    max-width: 100%;
    max-height: 100%;
}

This won't centre your image inside of the container, but it should get the job done for you.

like image 37
Corey Docken Avatar answered Nov 03 '22 13:11

Corey Docken