I'm struggling with a issue with my images. i have a series of images and i want to resize the m to fit is container based on the biggest one. preferably handled in jquery, javascript. there is an example below:
http://jsbin.com/bicupana/1/edit?html,css,js,output
So you see the images. The need to be resized proportionally, based on the largest image. i want the larges image to be filling 80% of it's parent, the body in this case. The reason why i'm struggling is that sometimes the width is bigger than the height and sometimes the height is bigger than the width.
The second reason i'm worried about is performance. If there are a lot of images, will people see the images flickering from big the the final size?
Related
I've seen quite a few post on here with similar questions and they seemed to have been answered great. However my problem is I can't understand the logic of whats going on and therefore I'm finding looking at the code pointless. Also I really want to be able to understand the code I'm writing. So really I'm not for any complete code as I think I can get that from other posts but if possible could someone explain to me the theory behind the code.
So, if I have a div 500px wide and 400px high and then two photos:
photo 1: 1000px wide 800px high
photo 2 : 250px wide 700px high
How do I re size these images to fit inside the div without them becoming stretched or distorted.
Like I said in the beginning I'm not looking for code. Could someone just explain the formula that fits the photos inside the div perfectly.
The (simple) asnwer is that the browser knows the aspect ratio of your image.
If you simply use an <img> tag, the image will be displayed in the original size (and aspect ratio).
now, if you specify the width, but not the height, the aspect ration will be kept, so your image will not be distorted.
If you use a CSS attribute like max-width and/or max-height that is pretty much what happens: your browser takes the image, and if necessary, scales both the with and the height by the same factor to match your specifications.
Does that answer your question?
The browser knows your images aspect ratio. If you want a 250px wide image to fit into a 500px wide container you'll need to scale it and lose quality or keep it as is.
Setting the width to 100% and the height left to auto will scale the image to fit it's width, without assuring that your height will match your desired output. You can scale it based on height, but the same problem applies. This is especially difficult when it comes to making the images responsive.
Using object-fit might help when you're container is the same, but the image vary widely in ratio, as seen here https://css-tricks.com/almanac/properties/o/object-fit/
Or having your images set as a background-image with background-size: cover.
when i work with flash i getting trouble when working with images
now my present project i'm uploading images dynamically, but here main problem is all the images sizes are different when i put the images into flash canvas every image looking different size means exact image size, but i need all the images should look same size in the canvas
check the image
if i change both of the height and width values that is not effecting any where, that is automatically taking fixed images size but i need all the images looks exact size, i didn't get any thing
I think I have the solution to your problem.
Basically, you need to create a container (if you haven't already done so) to 'hold' the images as they come up OR know the maximum height and width you want for the images. Then you need to make sure the aspect ratio is correct (so your picture won't be skewed. )
Code Example:
var wHRatio:Number=image.width/image.height; //this will give you the width to height ratio to make sure aspect ratio stays the same.
if (image.width>mcContainer.width){ //if not using container, enter max width
image.width=mcContainer.width;
image.height=image.width/wHRatio;
// at this point, if the height is still taller than the container, you want to shrink the image again so it's no taller than the container.
if (image.height>mcContainer.height){ //if not using container, enter max height
image.height=mcContainer.height;
image.width=image.height*wHRatio;
}
}
**** Don't forget to 'addChild' after you've completed the re-sizing.
This may not be the most efficient way of doing things, but I think it will work for your purposes.
I'm trying to fit 3 pictures in one HTML page, such that maximum screen area will be used, without scrolling. When screen size changes the pictures should be re-arranged, covering maximum screen arena. Each picture has different dimensions. For easier fitting each pic could be cropped (2:3 proportion max). The layout should be fluid and responsive. See example below
Two frameworks I'm considering for this problem are Twitter Bootstrap and jQuery Masonry, but none of these solves the problem of fitting.
Currently I solve this problem making square thumbnails out of each with css and overlapping those pictures, but users want the see the entire picture without overlapping and as big as possible:
!
you can see it in action at vash.co
You can get pretty close using pure CSS. Take this example, try it in Google Chrome: http://jsbin.com/ixomav/1 (source)
It's pretty crude but it should get the idea across. Going furthur you could add js code to resize images with a proper aspect ratio or keep trying with a css only approach. I used random images from wikipedia for my example but you may have an easier time if all your photos are the same dimensions.
If you remove the height you'll get properly aspected images but the problem with that is the white space, example: http://jsbin.com/ixomav/2
Check out Isotope: http://isotope.metafizzy.co/demos/images.html
I would like to create a gallery like Google+ "Photos From Your Circles" in which the thumbnails looks like a collage.
Thumbnails keeps the aspect ratio of the photo.
Gallery rows are close in height (not equal but very close).
Spacing between thumbnails is equal everywhere.
The gallery fills in a rectangle.
Photos are not cropped. They're just resized to fill in the space.
Please see the screenshot as example:
http://ansonalex.com/wp-content/uploads/2011/07/google-plus-gallery-large.jpg
i would like to learn how can i use css/javascript/jquery to style gallery thumbnails like this at the client side programatically.
Thank you for answers!
You may find this write-up helpful: Building a Google Plus Inspired Image Gallery
He uses a technique that does not require sorting the images, and it's pretty simple. Basically, once you know the width W of your rows of thumbnails, keep placing thumbnails until the total width exceeds W. Let's say you exceed W by 40px. Now, crop each thumbnail in the row (via css) to remove 40 pixels total.
If, say, you want to crop 10px from an image, you can do it with something like this:
<div style="width:[cropped width]; height:[height]; margin-left:-5px; overflow:hidden;">
<img style="width:[true width]; height:[height];" src="path/to/thumbnail.jpg" />
</div>
The overflow:hidden crops the image, and the negative margin centers it horizontally, basically.
This is classic cutting stock problem. You can solve it using branch and bound method or dynamic programming.
Generally Google has huge base of pictures thus finding proper combination of widths is easier for them.
For you I would suggest having limited set of aspect ratios (ex. 16) which creates finite number of combinations (256). Every picture before conversion should be scaled or cropped to closest aspect ratio.
Keep in mind that there may be very wide pictures which need some workaround: cropped or width of them should be width of row and height variable. Also spacing needs to be solved somehow. I would join all pictures in one row and put spacings as white overlays over pictures.
I'm going to simplify what I see (e.g. the numbers I provide won't be 100%, down-to-the-pixel accurate), but it will give you one method for accomplishing this.
When I look at the screenshot, what I see is a list of images that are approximately 4x3 aspect ratio (or 3x4 in reverse). This aspect ratio is at the heart of the layout. The overall rectangle that is filled can be any aspect ratio, but it should be a multiple of some widths and heights. For example, you'll see that each row contains some portrait, and some landscape photos. The overall effect, however, is that G+ can pull from a large pool of images, and therefore, can choose a combination of images that meet the needs of the individual aspect ratio (landscape or portrait aspect of the given image), as well as overall aspect ratio of the containing rectangle.
I would take images that are available in the pool, and calculate their aspect ratio (a simple width divided by height). Then group the images by their aspect ratio.
Finally, the part that's tricky about the layout is that you have to figure out which combinations of aspect ratios will result in a row that is completely full, from left to right. From the screenshot we see three such examples:
1st row = 4 landscape photos
2nd row = 2 landscape photos, 2 portrait photos, and 1 square photo
3rd row = 3 landscape photos, 1 portrait photo, and 1 square photo
The result is that, since all thumbnails have the same height, their combined widths in these particular combinations give you the desired resulting width for the layout rectangle.
So, I think solving this particular problem is basically a matter of solving 4 sub-problems:
Compute the aspect ratios of all photos in the available "photo pool"
Make a list of all combinations of photos' aspect ratios that result in the desired width (to make a single row)
From the pool of photos available, figure out which photos you can combine into which valid combinations, resulting in a single, composed row of images
Finally, steps 1-3 create a single row of images. In order to get the overall rectangle, you just use steps 1-3 to create as many rows of images as you like, and then stack them all on top of one another.
im using a image preview that allows me to add css styles to the previews that pops up.
now, some images are larger than the screen so you will just see a portion of them, while other images are very small so you don't have to resize them.
is there a way to only resize the larger images and not touching the smaller ones, eg. only resize images with width or height larger than 400 px?
any other approaches to make the larger ones fit into the screen without affecting the smaller ones would be appreciated cause if i just use width=400px and height=400px all images will be at that size and the proportion will be wrong. and for smaller images you will se very large pixels.
thanks in advance
Seam carving javascript implementation is what I call smart resizing of images :)
Anyway, in Javascript, you can implement your logic depending on the width and height properties of the Image object.
Also have a look at this page, the author uses jQuery to resize images larger than specified dimensions.