Scale imported image to picture box html - html

I have a page where users can upload there pictures that will act as their profile pictures. I imported it using this code in php and html:
$data = mysqli_fetch_assoc;
$image = $data['image'];
and the html
<img src = "./imgs/<?php echo $image; ?>" style = "height:200px; width:200px;>
the only problem is that, some pictures who are not actually a "square-in-size" will look greatly stretched in the image box. How can I make the image box displays the original "orientation" of the image for example, when showing a 5x10 image (100px x 200px), how can I not make it automatically stretch to a 10x10 orientation?

You've used inline styles to explicitly set the height and width of the image to 200px.
I would suggest you let the browser calculate the height automatically for you and only set the width to 200px (this will make the height auto).
If you want to maintain a square image without stretching / squishing the image, you should use a container with a background image, something like this:
HTML:
<div class="profile-image"></div>
CSS:
.profile-image {
background-image: url('image/url/image.png');
background-size: cover;
background-position: center;
}
Of course, you would do well to ensure some kind of standardisation between images, for example, by enforcing a max width, height or aspect ratio when the user uploads it.

You don't have to specify image size, so if you want all images take all the width you can just apply a width: 100% and dont specify height, it will be automaticly calculated

You are using the term "orientation" wrong. Orientation suggests either the picture is "landscape" (the x value is bigger than the y value), "portrait" (the y value of bigger than the x value) or square (where both axis are the same in size).
What you are looking for is a container for the image, which is always a square (1:1), and the image inside will not stretch.
You can achieve such behavior by applying the image as a background-image via CSS, and give it a background-size: cover. This will fit the image inside the container, cropping the extra pixels from either side.
HTML Example:
<div class="container"></div>
CSS:
.container {
width: 250px;
height: 250px;
background-image: url('/path-to-image/image.png');
background-size: cover;
}
You can set the CSS form the PHP code if imported inline inside a <style> element and use the $image variable that way:
<?php
$image = $data['image'];
?>
<style>
.container {
width: 250px;
height: 250px;
background-image: url('<?= $image ?>');
background-size: cover;
}
</style>

Related

Background image scaled to html document height

I am having trouble scaling a repeating image to the height of the document in html.
I can clearly see how large the html element is using the inspector.
I have set the div's height to 100rem, which should scale it to 100% of the height of the root element (which as far as I understand is the html element).
CSS:
#left-buildings{
left: 0;
float:left;
}
.buildings{
height: 100rem;
position: absolute;
width: 5%;
top: 0;
background-image: url(../media/images/city-side-seamless.png);
background-repeat: repeat-y;
background-size: contain;
}
HTML:
<div class="buildings" id="left-buildings"> </div>
<div class="buildings" id="right-buildings"> </div>
The problem is, it does not scale to the correct size.
screenshot showing that the element is not spanning the height of the page
Try changing the height to 100vh.
rem is the relative to font-size of the root element
I would put the images as an img tag inside the divs, and then you can just style them to 100% height.
I managed to get around the problem but setting the height of the buildings to a measurement in pixels that fits length of the page. This prevents the buildings form scaling when one zooms in on the page. Quite conveniently, it also lets the ends "overflow" and doesn't extend the page height when you zoom. Not too sure why that happens, but I'm not complaining XD.

Responsive image resize and selectively crop it's width with a set of rules

I want a background image to appear at the top part and fully cover the width of a page. As you can see, the image is quite wide and short - https://i.imgur.com/aJb6eBr.jpg. This should be the header image of a page, with the contents of the page appearing below it.
If the browser's width is bigger than the image's original width, the image's width and height should be enlarged proportionally (together with its container - thus pushing downwards the page's contents that appear under the image).
If the browser's width is smaller than the image's original width, the image should retain its original size without shrinking, and be cropped from both sides until a 15% crop is reached from each side (You can see that the image has quite wide green areas on both sides which are safe for cropping).
The tricky part is that once 15% of the crop has been reached from each side, I want the image to start shrinking proportionally to the browser's width, thus the middle 70% of the image will always be seen, and the image will never be cropped more than 15% from each side.
The height of the image (and it's container) should rescale automatically in proportion with the image's width. If the image's height (together with its container) shrinks to be smaller than it's original size, the page's contents are pushed up so the distance between the page's contents and the image is always kept the same.
I'm looking for a clean solution (preferably with CSS only) similar to this:
https://demodern.de/projekte/mediengruppe-rtl
Any ideas guys?
In terms of using CSS it is pretty simple to make everything work as you need. In order to do this you might use the image as it is via and the same image on a parent element's background. But you will have to adjust your CSS to work with this image ONLY. In case if you will try to use another image - you will have to adjust paddings or mediaqueries. Solution that works a kind of ONE time for a specific image, but still, it doesn't use JS at all, which is great. And regarding referencing the image twice - it is not a problem for a browser. It will make only one http request for a single unique media asset so no performance problems from this perspective.
Here is a way how you might do what you want:
.wrapper {
background: url(/images/_m1NuVvd.jpeg) 50% 50% no-repeat;
background-size: cover;
overflow: hidden;
position: relative;
padding-top: 38%;
}
.wrapper img {
transform: translateX(-50%);
left: 50%;
position: relative;
min-width: 100%;
display:none;
}
#media screen and (min-width: 1338px) {
.wrapper {
padding-top: 0;
}
.wrapper img {
display: inline-block;
vertical-align: top;
}
}
<div class="wrapper">
<img src="/images/_m1NuVvd.jpeg" />
</div>
Make sure to use a proper path to your image instead of /images/_m1NuVvd.jpeg.
BTW, in future it will be better to probide links to the images in a way, so those might be reused in jsfiddle. Dropbox doesn't allow to use the image via that link.
Best wishes

Avoid image stretching

so I have an image tag on my page that is set to 300px height. When a bigger image is used for it, the image is stretched so it fits and it gets really ugly. Is there a way to just get a part of the image instead, preferably the top 300pxs of it?
Hope I made myself clear, I'm new to this. Thank you!
I believe
overflow:hidden;
is what you're looking for. You put that property on a div that surrounds the image, not the image itself. Here's a good resource: http://css-tricks.com/almanac/properties/o/overflow/
Here's a code sample:
<html>
<head>
<style>
.overflow{
height:100px;
width:200px;
overflow:hidden;
}
img{
height: 200px;
}
</style>
</head>
<body>
<div class="overflow">
<img src="http://funmozar.com/wp-content/uploads/2014/06/white-cat.jpg">
</div>
</body>
</html>
You could also want to consider using the max-height and max-width properties. Make sure not to set both height AND width on the image or it will still stretch it to match those parameters.
probably the best solution in order to avoid image stretching is to use a div with fixed size (width and height), background-image and use background-size propriety.
example:
html:
<div id="yourDiv"></div>
css:
#yourDiv {
width: 200px;
height: 200px;
background-image: url('yourimage.jpg');
background-size: cover;
}
Please have a look here for other information about background-size: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
A possibility is to use the object-fit CSS property on the image. For instance, the value contain will scale down the image so that its original ratio is maintained; while the value cover will crop parts of the image. You can then use the object-position property to properly place the scaled down or the croped image.
For more information on these CSS properties and their differents values:
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
https://developer.mozilla.org/en-US/docs/Web/CSS/object-position
You can wrap the image in an element, set fixed dimensions for the container and no dimension settings for the image but overflow: hidden on the image. Here’s an example of using small dimensions:
An image:<br>
<img src="http://www.lorempixel.com/100/100" alt="foo"><br>
The same image with just the upper half taken:<br>
<div style="width: 100px; height: 50px; overflow: hidden"><img
src="http://www.lorempixel.com/100/100" alt="foo"></div>

Strange CSS issue - background attribute is not working without image height

I need to set the image height everytime I'm using background: url('images/something.jpg')[..];
Fe.
HTML:
<div class="someImage"></div>
CSS:
.someImage {
background: url('images/something.jpg') no-repeat top;
}
The above example should work... but image won't display until I add an image height attribute to the CSS style class:
.someImage {
background: url('images/something.jpg') no-repeat top;
height: 25px;
}
And then my image appear on the website...
Why does it happend?
Because without content, a div has no height, background image or not.
Since your div is empty it has no height..
The image you use is applied as a background, so it does not affect the size.. it just fits whatever space is available at the div.
When you explicitly set the height, you create room for the image to appear..

How to set image width and height 100% with CSS?

My code:
background:url(images/menu_edu.jpg) no-repeat;
But only half of the image is getting displayed.
The element which has the background needs to be the size of the image.
i.e. flower.jpg = 255px x 55px
<div class="flower">
Some text
</div>
.flower {
background: url(flower.jpg) no-repeat;
width: 255px;
height: 55px;
}
The size of the element cannot be set to the dimensions of the image if you're using a background. You could use javascript to calculate the dimensions though.
Or if you need to repeat the image, you can use repeat, repeat-x or repeat-y on the background tag instead.
If you just want to display an image, the IMG-tag is much more useful and effective... (and it could be set to width(/&)height = 100%).
If you want to display your image in full size, no need to use CSS for this
Dont give height and width attribute to the <img> tag like
<img src="this.jpg" /> it will display in full size
But if you want your <div> to show its background in full size, then is no other option than assigning the exact image dimensions
You are not showing enough code, but if the background image is in the body element, it is probably not stretching across the whole viewport.
Try
html, body { min-height: 100% }