Image Resize % different ratios - php

I am adding images into a page that are all different aspect ratios (some wide, some tall etc).
what is the best way to get all the images to display the more or less the same size but not be squashed/stretched?
I have tried
<img src='admin/userpics/$prodID.jpg' height='50%'>
This doesn't seem to make images the same?

I'd use CSS.
img {
max-height: 200px;
max-width: 200px;
}

simply set the width or height to a fixed value, and the other value to auto
<img src='admin/userpics/$prodID.jpg' style='width:100px;height:auto;'>
or
<img src='admin/userpics/$prodID.jpg' style='height:100px;width:auto;'>
what ever you prefer, the aspect ratio with be always correct

If you don't like img tags (as me), you could use this (if you won't target <= IE8 as Brad pointed out):
div.image {
background-size:cover;
background-position:center;
display:block;
width:30%;
height:30%;
}
<div class="image" style="background-image:url(admin/userpics/$prodID.jpg)"></div>
Proof: http://jsbin.com/ekogaz/1/edit See how the image always stays in center, but still is cropped. You can add as many of these as possible. Also, you can use % (or like 100px). Try to resize the window and you'll see that it works then too.

<img src='admin/userpics/$prodID.jpg' style='height: 200px; width: auto;'>

Use values in pixels, instead of a percentage. Like this:
<img src="smiley.gif" alt="Smiley face" height="42" width="42">

You already got many useful answers for displaying resized images with html/css, but i would recommend you create thumbnails and use them instead. That way you will avoid loading the full resolution images even if you just need small resolution thumbs, which is important especially if the images are in high resolution or you are building some kind of gallery. Also you will have control over the quality of downsampling done on your image, which is much better then some browsers do. Since you use PHP check out this library for example: phpThumb
You can define the maximum width and height you want to use, and resize image keeping the aspect ratio.

http://jsfiddle.net/7TDCA/
HTML:
<div class="img-wrapper">
<img src="http://i.imgur.com/Himba.png" alt="" title="" />
</div>
<div class="img-wrapper">
<img src="http://cdn.fotocommunity.com/Natur/Tiere/Pfau-Hochformat-a18613762.jpg" alt="" title="" />
</div>
CSS:
.img-wrapper {
margin: 50px auto;
width: 50%;
height: 250px;
overflow: hidden;
}
.img-wrapper img {
width: 100%;
height: auto;
}

Related

Align Main Slider image to center

I'm working on a small project, and have one little problem that I don't know how to resolve myself. I have an image gallery with many images, but I want the active image to be centered, without changing the resolution or width/height ratio. Link for the issue here. Login is user / password. I tried to manipulate with this CSS:
.img {
margin-left: 200px;
}
However, it seems to ruin the bottom slider. What should I do to center the main image without changing the image ratio? This example image shows what I want to do.
Try this
.imgs {
margin-left: auto;
margin-right auto;
}
or you could do this
<div class="imgs" align="center">
<img src="https://source.unsplash.com/random/200x200" />
</div>
Try adding:
.flex-active-slide {
text-align: center;
}

Keep Div size same for every different image sizes?

I'm working on a Gallery Web page and I have different size images stored in local server. What I want is to make my gallery look like this. Simply saying I want to keep Division size same for all image sizes. Also image should cover whole area.
What I tried
I suppose to use PHP to get image locations and do this task. So I'll paste PHP file.
<?php
$itemNum =$_POST["itemNum"] ;
$folderName = $_POST["folderName"];
echo '<div class="col-md-4 mix category-a" style="margin:10px;height:300px;width:400px">
<div class="single-portfolio" style="height:300px;width:400px;overflow: hidden;">
<a class="gallery-item" href="gallery/'.$folderName.'/'.$itemNum.'.jpg"><img class="img-responsive" src="gallery/'.$folderName.'/'.$itemNum.'.jpg" alt="One" /></a>
</div>
</div>';
die();
?>?>
My Problem
This code doesn't do what I want. I tried every way I know to make this possible. Can someone help me to get what I want? Thanks.
EDIT:
This is what I got. Can see that size changes according to image size
Set max-height and max-width of div and also fix height of image :
<div class="single-portfolio" style="max-height:300px;max-width:400px;overflow: hidden;">
<img style="height:300px;width:400px;" class="img-responsive" src="gallery/'.$folderName.'/'.$itemNum.'.jpg" alt="One" />
All you have to do is playing with css and its so easy to apply. You have fixed height of image and that is 400px.
So css for the div will be
.category-a {
height: 400px;
}
.img-responsive {
display: block;
max-width: 90%;
max-height: 400px;
margin: 0 auto;
}
And your HTML will be like this. Please don't apply inline css as its always bad practice to use like this. instead use class.
<div class="col-md-4 mix category-a">
<div class="single-portfolio">
<a class="gallery-item" href="gallery/'.$folderName.'/'.$itemNum.'.jpg"><img class="img-responsive" src="gallery/'.$folderName.'/'.$itemNum.'.jpg" alt="One" /></a>
</div>
</div>

How to make an image not stretch

On my front page I want the post thumbnail to be the full width of the page. But I only want it to be the full width of the page up until the image width it was uploaded at. So when the page gets bigger and bigger I want the image to stop being 100% once it gets to its actual image size and then just stay that size. Right now I have figured out how to make the post thumbnail full width, however as the page gets bigger the image just stretches to fit 100%. How could I fix this?
<?php the_post_thumbnail('thumbnail', array('class' => 'large-front-thumbnail')); ?>
.large-front-thumbnail {
position: relative;
width: 100%;
height: auto;
}
You need to make 2 changes.
Right now you're setting the image width to 100%. When you set width to 100%, no matter what size the image was uploaded at, it's going to stretch to the width of the container. You need to set width to auto.
You then want to set a max-width of 100%. Those 2 properties combined will mean your image will scale responsively yet never exceed the original upload size.
.large-front-thumbnail {
height: auto;
max-width: 100%;
position: relative;
width: auto;
}
As long as you haven't altered any of your WordPress defaults, the image size will always be 150px by 150px. How do you know? Because you are passing the 'thumbnail' argument into the_post_thumbnail.
Therefore, as #pol said, setting a max-width rule of 150px will work.
See more here about the behavior of the_post_thumbnail.
The following CSS should be what you require:
.large-front-thumbnail {
position: relative;
width: auto;
height: auto;
max-width: 100%;
}
Actually you just need use "max-height" & "max-width", preferable add div container outside constraint outer image size.
<div style="width: 100%;">
<img src="https://www.royalcanin.com/~/media/Royal-Canin/Product-Categories/cat-adult-landing-hero.ashx" alt="anything" style="position: absolute; max-height: 100%; max-width: 100%;"/>
</div>

wordpress crop thumbnail

I need to crop wordpress thumbnail like in the image below, i.e., crop piece of image from left top corner, without scaling of image.
Can I do this without plugins?
Here's an example of how you'd crop to the top-left of a much larger <img> using a parent container.
.crop {
width: 200px;
height: 200px;
overflow: hidden;
}
<div class="crop">
<img src="http://i.imgur.com/pRSJBDI.jpg" />
</div>

html image not fitting in image-wrapper

I need to fit and crop the image into it's wrapper.
<div class="box desktop-3 tablet-3 tablet-ls-3 mobile-3">
<div class="inner-box fullbox">
<a href='#module'>
<div class="image-wrap" >
<img src="../img/placeholder.png" />
</div>
</a>
</div>
</div>
css
.box {
width: 282px;
min-height: 282px;
padding: 10px;
float: left;
}
.inner-box {
width: 100%;
min-height: 282px;
top: 10px;
right: 10px;
bottom: 10px;
left: 10px;
padding: 20px;
}
.fullbox {
padding: 0px;
}
.image-wrap {
...
}
i've tried to put the image as the background like you see below, but that didn't worked for me. I want to have a image section from the image that it fits into the box.
.image-warpper {
background-image: url(...);
background-repeat: no-repeat;
background-size: contain;
}
Do i have to crop the image via php or is it possible to scale or crop it in css?
Thanks
"Do i have to crop the image via php..."
Depending on the Image file-size its strongly recommendable to use PHP for this purpose.
Remember the clients browser will always load the complete image to resize it to the css given values.
So even if you got a style telling the image shall never exceed 100x100px the client's browser will load the full size image.
That could take "very long" if its a giant image (referring to the file size).
There are pretty nice classes/libs you can use with PHP to get a comfortable and easy way to play with images. And your page will be much faster then.
For example i recently found:
http://wideimage.sourceforge.net/
Super sweet thing. Supports chaining and stuff.
You should be able to do this:
.image-wrap img { max-width:100%; height:auto; }
This will constrain, and scale down the image, and set it to be 100% wide, according to however wide the parent element is.
Having in mind that you'll use an img html tag, make the image wrap div in position:relative and overflow:hidden and the image with position:absolute and height:100%, width:auto (or width:100% and height:auto). This way the image will be cropped in the parent container and keep its ratio.
See this demo and resize the frame to see how the image is cropped and resized in various dimensions.

Categories