How to crop a image to have 1:1 aspect ratio CSS - php

I want to show in a page all images from a specific file. I make it but I can't crop it like to show 4 images on a row and the heigh be the same like width.
This is php code for show images from file:
<?php
$files = glob("galery/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img class="img-display" src="'.$num.'" alt="random image">';
}
?>
This is CSS:
.img-display {
width: 25%; /* This make photos to aling 4 on a row*/
border: solid 5px white; /* This is for make a white spece between photos */
}
I want to show the imeges all at the same aspect ratio. Because if I have a portrait image, the heigh from this image is biger than the others.And if I give "max-heigh" is resizing the image and doesn't look good...
I try with clip but I can't make something good.
I reserch to google but can't find anything good for me.

You can use this:
.img-cropped {
object-fit: cover;
object-position: center center;
width: 250px;
height: 250px;
}
And it's browser support table: https://caniuse.com/#feat=object-fit
(thanks #giorgio)
This will display the image in 250x250 dimensions and the image will cover the container with center as the resize origin.
If you don't know the exact dimensions of the image, and will be using percentage as the width, then you need some javascript to equalize width and height's of the images.
For example:
var cats = document.querySelectorAll(".img-cropped");
var width = cats[0].clientWidth;
console.log(width);
for (var i = 0; i < cats.length; i++) {
cats[i].style.height = width + "px";
}
.container {
width: 80%;
margin: 0 auto;
}
.img-cropped {
object-fit: cover;
object-position: center center;
width: 24%;
}
<div class="container">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
<img class="img-cropped" src="http://placekitten.com/300/350">
</div>

This works! Thanks a lot for your help!
I leave here what I maked.
HTML/PHP:
<div class="galery-cont">
<?php
$files = glob("galery/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img class="img-cropped" src="'.$num.'" alt="random image">';
}
?>
</div>
<script>
var cats = document.querySelectorAll(".img-cropped");
var width = cats[0].clientWidth;
console.log(width);
for (var i = 0; i < cats.length; i++) {
cats[i].style.height = width + "px";
}
</script>
CSS:
.galery-cont {
padding-top: 210px;
width: 100%;
height: auto;
background: white;
}
.img-cropped {
border: solid 5px white;
object-fit: cover;
object-position: center center;
width: 25%;
}
But here I have another question...:) How can I make when I click on image(random one from the list) to open a slideshow like Modal and first image is image that I clicked.

Related

How to make videos same shape and height

I have uploaded two videos to the database and Its being displayed on my website. I am trying to make them the same shape and height. The width is fine, but one of them is shaped like a square and the height is longer while the other is rectangle and the height is shorter and I know its because of the way video was taken, one was taken vertically and the other horizontally. How do I get them to be the same shape and height. Can you help me please?
I tried this
<video class="video1" id="cb" preload="auto" video="src="{{$proo->video}}#t=1"" style=" height:80%; max-height:20em; width:100%; max-width:20em; object-fit: cover;
float:left; clear:both;
padding-left:2%; margin-top:0px; cursor:pointer; "><source="video1.jpg" playsinline alt="Video Unavailable" id="" ></source>
</video>
this the javascript part to handle the onclick,onmouseleave
$(document).on('mouseover touchstart', 'video', function() {
$(this).get(0).play();
this.muted=true;
});
//pause video on mouse leave
$(document).on('mouseleave', 'video', function() {
this.currentTime = 1;
$(this).get(0).pause();
});
$(document).on('click touchstart', 'video', function() {
$(this).get(0).play();
this.muted=false;
this.currentTime = 0;
});
This a little something i came up with when i need to output YouTube videos in iframes and its very responsive meaning that it adjusts its height and width in relation with current screen size.
<style>
#mediaPlayer{
position: relative;
height: auto;
padding-bottom: 56.25%;
padding-top: 1.875em;
overflow: hidden;
border: 0.1875em double #185875;
background-image:url('../video_loading.gif');
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-position: center;
background-attachment: fixed;
}
#mediaPlayer iframe{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
max-width: 100%;
max-height: 100%;
}
</style>
/*use the div to specify the exact height and width you want*/
<div style="height: 50%; width:50%;">
<center>
<div id="mediaPlayer">
<iframe id="play_now" width="100%" height="100%" src="source here" value="value here" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
</center>
</div>
The video_loading.gif background is usually a good idea for users with slow network to have something in the background while your iframe content is loading
but you have to find your own unique gif file/image and set it's location in the background-image:url('../file-location-here.gif');

responsive image with object-fit:cover

I want to frame the image, getting just the width:300px; of the image visible and the rest of the width invisible. I have been working with object-fit:cover and overflow: hidden. But I haven't been able to do it while still having the image responsive (width: 100%),scaling in size.
Here is the html code for the image:
<div id="pgc-1397-2-0" class="panel-grid-cell">
<div id="panel-1397-2-0-0" class="so-panel widget widget_sow-image panel-first-child panel-last-child" data-index="2">
::before
<div class="so-widget-sow-image so-widget-sow-image-default-bf8d3ff3bedb">
<h3 class="widget-title">KRAGEN</h3>
<div class="sow-image-container">
<img src="http://www.ny.nordicredesigners.dk/wp-content/uploads/2018/03/unspecified-3-31.jpeg"width="800" height="450" srcset="http://www.ny.nordicredesigners.dk/wp-content/uploads/2018/03/unspecified-3-31-300x169.jpeg 300w, http://www.ny.nordicredesigners.dk/wp-content/uploads/2018/03/unspecified-3-31-768x432.jpeg 768w, http://www.ny.nordicredesigners.dk/wp-content/uploads/2018/03/unspecified-3-31.jpeg 800w" sizes="(max-width: 800px) 100vw, 800px" title="KRAGEN" class="so-widget-image"
</div>
</div>
Here is a link to the webpage:
http://www.ny.nordicredesigners.dk/
Here is a picture of how I want to frame the picture:
and this is what I want for result:
I have made fiddle here: https:www.jsfiddle.net/q2yrqvxv/5/
Control Image Aspect Ratio Using CSS object-fit and object-position properties allow you to scale images and videos, much like you could with background-size and background-position.
Moreover here you will get documentation and example
img {
width: 90vw;
height: 75vh;
border: 3px solid tomato;
object-fit: cover;
object-position: 0 0;
}
/* Demo styles only */
* {
box-sizing: border-box;
}
body {
margin: 0;
}
<div>
<img src="https://i.imgur.com/9Q9pgmR.jpg">
</div>

Resizing/Cropping Image to adjust into layout

I'm having issue with the re-sizing / scaling the thumbnails in to the desired layout. I am able to generate the thumbnails here's the code to crop/resize.
$filename = $key.$_FILES["files"]["name"][$key];
$filetmp = $_FILES["files"]["tmp_name"][$key];
$filetype = $_FILES["files"]["type"][$key];
$filesize = $_FILES["files"]["size"][$key];
$fileinfo = getimagesize($tmp_name);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
// GETS FILE EXTENSION
$fileextension = pathinfo($filename, PATHINFO_EXTENSION);
$microtime = preg_replace('/[^A-Za-z0-9]/', "", microtime());
$filepath = "../static/products/".$microtime.".".$fileextension;
$filepath_thumb = "../static/products/thumbs/".$microtime.".".$fileextension;
$filepath2 = "/static/products/".$microtime.".".$fileextension;
move_uploaded_file($filetmp,$filepath);
if ($filetype == "image/jpeg") {
$imagecreate = "imagecreatefromjpeg";
$imageformat = "imagejpeg";
}
if ($filetype == "image/png") {
$imagecreate = "imagecreatefrompng";
$imageformat = "imagepng";
}
if ($filetype == "image/gif") {
$imagecreate = "imagecreatefromgif";
$imageformat = "imagegif";
}
$ratio = $filewidth * 1.0 / $fileheight; // width/height
if( $ratio > 1) {
$new_width = 600;
$new_height = 600/$ratio;
}
else {
$new_width = 600*$ratio;
$new_height = 600;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = $imagecreate($filepath); //photo folder
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $filewidth, $fileheight);
$imageformat($image_p, $filepath_thumb);//thumb folder
The Problem:
I don't know what will ve the file size of image, but it surely be a HD/DSLR image. Now, the above script generates a thumbnail having with = 600px and height = ratio of the image (e.g 600x400)
In my layout, i want that thumbnail to be adjusted some how, that i won't get distorted, or stretched in any ways. The container which holds the thumbnail has the 200 x 200 px width/height.
When the thumbnails renders in the browser its dimension gets
600px × 401px (scaled to 200px × 200px) height is random for every image.
The HTML:
<li class="column">
<div class="post-image">
<a href="http://localhost/example/products/40/">
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
<div class="product-detail">
<h4 class="post-title">
Post title
</h4>
</div>
</li>
The CSS
.post-image{ width:100%; height:200px; }
.post-image img { width:auto; height:100%; min-height:200px; }
What could be solution to exactly scaled to 200 x 200px without having width x height specifics ...
I think the easiest way to achieve what you want, is to use background images.
You would have to change the html though to something like:
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: cover;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
And the original image: http://www.nbcnews.com/science/space/ceres-spots-shine-new-images-dwarf-planet-n357161
Note that part of the image will be cut off this way. If you want to show the whole image in the 200x200 block, you would need background-size: contain; but then you would have space around the image (above / below or left / right depending on the orientation of the image):
.post-image {
width:200px;
height:200px;
background-repeat: no-repeat;
background-position: center center;
/* the size makes sure it gets scaled correctly to cover the area */
background-size: contain;
/* just to illustrate */
background-color: yellow;
}
.post-image a {
display: block:
width: 100%;
height: 100%;
}
.post-image img {
display: none;
}
<div class="post-image" style="background-image: url(http://media1.s-nbcnews.com/j/newscms/2015_20/1018941/150511-ceres-bright-spots-nasa-yh-0106p_fbf0f0c348c8d1881df19c5e07c819d1.nbcnews-fp-800-520.jpg);">
<!-- ^^^^^ set the background image -->
<a href="http://localhost/example/products/40/">
<!-- You can leave the image for SEO if required or you can take it out -->
<img src="http://localhost/example/static/products/thumbs/0446826001431411830.JPG" alt="my photo" /></a>
</div>
There is a pretty neat solution for this! you would need a parent container for your image and then place the image with position: absolute inside this container as following:
<div class="imageParent">
<img src="http://placehold.it/600x400" alt="" class="image--fitHeight" />
</div>
And the needed css:
.imageParent {
width: 200px;
height: 200px;
position: relative;
overflow: hidden;
}
.image--fitHeight {
position: absolute;
top: -500%;
left: -500%;
right: -500%;
bottom: -500%;
margin: auto;
min-width: 100%;
height: auto;
}
It stretches the image to a min-width of 100% and since the container has overflow: hidden it will cover the image centered with margin: auto inside your parent container.
it will not be altered / stretched.
if you have an image which has a ratio like 16/9 (so the width is larger than the height) use the method above. vice versa use instead of min-width: 100%; height: auto you simply switch those two: width: auto; min-height: 100%
This method is the same as background: cover but with real images
JSFIDDLE
I don't think you can have a scaled image that isn't distorted if you don't respect its ratio. You can either crop the image or zoom it out, but you'll end up with side bands.
I would try to find whether the image is in portrait or landscape, and then apply the css accordingly, so that the image fits into your 200x200 frame (this is an example of css with the bands) :
.post-image-portrait img { width:auto; height:200px; }
.post-image-landscape img {width:200px; height:auto;}
(I haven't tested the css, you may have to correct it).

CSS onmouseover gallery with php loop

I once created an image gallery using CSS which did the following:
Created a thumbnail gallery
Created a div where in there was placed 1x1 pixel images
on mouseover of thumbnails, these 1x1 pixel images expanded to fit the div size, with height being relative to length.
My code so far:
php:
echo '<div id="thumbnails">';
$files = glob("11-09-2012/*.*");
for ($i=1; $i<count($files); $i++)
{ //creating thumbnails
$num = $files[$i];
echo '<img src="'.$num.'" height="50px" id="thumb'.$files[$i].'"></img>';
};
echo '</div><div id="gallery">';
for ($i=1; $i<count($files); $i++)
{ //creating 1x1
$num = $files[$i];
echo '<img STYLE="position:absolute" src="'.$num.'" height="1px" width="1px" id="img'.$files[$i].'"></img>';
};
echo '</div>';
CSS:
#gallery {
margin: 0 auto;
border-style: solid;
border-width: 3px;
border-color: #fff;
width: 800px;
height: 600px;
}
Now I'm not sure where to go - Any help is appreciated - alternative ways to do this, is as well.
Best regards - Jesper
A naïve programmer might recommend jQuery, or attach an event to each individual image. Here's how the big boys do it:
(function() {
var box = document.getElementById('thumbnails'),
handler = function(e) {
e = e||window.event;
var tar = e.target || e.srcElement,
type = e.type, id = tar.id, m, img;
if( (m=id.match(/^thumb(.*)$/)) && (img=document.getElementById('img'+m[1]))) {
img.style.height = img.style.width = type == "mouseover" ? "auto" : "1px";
}
};
if( typeof box.attachEvent != "undefined") {
box.attachEvent('onmouseover',handler);
box.attachEvent('onmouseout',handler);
}
else {
box.addEventListener('mouseover',handler);
box.addEventListener('mouseout',handler);
}
})();
The only change you need to make to your HTML is to remove the width="1px" height="1px" from your images and instead add width:1px;height:1px;max-width:800px;max-height:600px; to the image's style. This would be better done in the CSS file:
#gallery>img {
position: absolute;
left: 0; top: 0;
width: 1px; height: 1px;
max-width: 800px;
max-height: 600px;
}
http://jsfiddle.net/yDUMT/
The jsfiddle shows the onMouseOver being used with a javascript function to enlarge the 1px when the thumbnail is mouse overed, and the onMouseOut being used with a javascript function to shrink the enlarged 1px image back down when the mouse is moved off the thumbnail.

How do you center an image in a specified area, without resizing the image?

I want to center an image in an area, without resizing... I am using HTML.
Example:
I have an image <img src='img1.png' width='64' height='64'> - the image is actually 64x64. It displays perfectly.
Now, I have another image <img src='img2.png' width='64' height='64'> however, the image is not as big as it should be, its 32x32 - what happens here is it resizes the image to 64x64 and makes it look like $%^&.
How do I make images smaller then the desired width and height centered in the 'img' area without any resizing what so ever?
What you will need is something like this:
<div class="box">
<img src="whatever size image you'd like" />
</div>
And for the styling (in an external stylesheet, natch) you'd apply:
/* Image centering */
div.box {
border: 1px black solid;
height: 64px;
width: 64px;
background: #444;
display: table-cell;
vertical-align: middle;
}
.box img {
display:block;
margin: 0px auto;
}
This works for images with dimensions <= 64x64px, and is easily modifiable to work with larger pics. The key elements here are
set dimensions on the div
display as a table-cell (allows vertical align)
vertical align (aligns on the Y-axis w/out weird hacks)
display:block on the img element
margin: auto centers the image laterally
Solution without IE-unfriendly display:table-cell:
<!DOCTYPE html>
<style>
div {
line-height:64px; /* that's the secret sauce */
text-align:center;
width:64px; height:64px;
}
img {vertical-align:middle}
</style>
<div><img …></div>
You could try putting the image inside a DIV that is 64x64 and not specifying the image dimensions. Then you could style the div so its contents are centered and any overflow is hidden.
You can dynamically get an image size using the getimagesize() php function:
<?php
$size = getimagesize('imgX.png');
$height = $size[1];
$width = $size[0];
?>
<div style="text-align: center">
<img src="imgX.png" width="<?php print($width) ?>" height="<?php print($height) ?>" />
</div>
I've had to do something similar with 36x36 images. Users were able to upload any size but the thumbnails were only to show the center 36 square pixels.
Markup:
<li><div><span></span>
<img src="_media/objects/jessica-bowman.png" alt="Jessica Bowman" /></div>
<p>Jessica Bowman</p>
</li>
The span was just there to get rounded corners on the image, it's not necessarily needed.
CSS:
ul.recent-list li div {
position: relative;
float: left;
width: 36px;
height: 36px;
overflow: hidden;
}
ul.recent-list li div span {
position: relative;
z-index: 100;
display: block;
width: 36px;
height: 36px;
background: url("../_media/icons/icon-overlay.png") top left no-repeat;
}
ul.recent-list li div img {
position: relative;
top: -36px;
z-index: 0;
float: left;
}
JavaScript:
$(window).load(function() {
$("ul.recent-list div img").each(function() {
var moveX = ($(this).width() / 2 * -1) + 18;
var moveY = ($(this).height() / 2) * -1 - 18; // 18 is 1/2 the default offset of 36px defined in CSS
$(this).css({'top' : moveY, 'left' : moveX});
});
});
The solution is a simple bit of CSS + HMTL
<img src="transparentpixel.gif"
width="64"
height="64"
style="
background-image:url('path/to/image.jpg');
background-repeat:no-repeat;
background-position:center center;
" />
the transparentpixel.gif is a simple 1x1px transparent gif image
An img tag with width and height attributes is saying "stretch or shrink the image to this size regardless of its actual size". use something like:
<div style="text-align:center;">
<img src="x.jpg">
</div>
and no i don't know why text-align would work, but it appears to in my experience.
Use CSS to render the image using background:
<div style="background: url(img1.png) no-repeat center center; height: 64px; width: 64px;"></div>
This will show the image in the center, without scaling it.

Categories