E.g. I have a link
http://img.youtube.com/vi/aOPGepdbfpo/0.jpg
for a youtube video thumbnail:
And I would like to remove the black top and bottom border so I get a picture like this:
Could it be done using PHP function javascript/jQuery or maybe youtube api itself?
YouTube offers images that don't have the 4:3 ratio black strips. To get a 16:9 video thumbnail with no black strips, try one of these:
http://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
The first one mqdefault comes as a 320x180 pixel image.
The second one maxresdefault comes as a 1500x900 pixel image, so it would need resizing to use as a thumbnail. This is a nice image but it isn't always available. If the video is of a low quality (less than 720p I'd imagine, not exactly sure) then this 'maxresdefault' becomes unavailable. So never rely on it.
Use it as a background image, center it and change height.
http://dabblet.com/gist/4012604
.youtubePreview {
background:url('http://img.youtube.com/vi/aOPGepdbfpo/0.jpg') center no-repeat;
height:204px;
width:480px;
}
If you want it with a flexible width, use this:
HTML
<div class="thumb">
<img src="...">
</div>
CSS
.thumb {
overflow: hidden;
}
.thumb img {
margin: -10% 0;
width: 100%;
}
To remove the black borders from the Youtube thumbnail we need not have to write a seperate code or CSS. Simply use the ytimg.com site, that stands for YouTube image, which fetches the images from YouTube, like thumbnails and icons as required which come from that domain.
Example shown below -
Original Image [With borders]
http://img.youtube.com/vi/JQ7a_8psCn0/hqdefault.jpg
With No borders/Strips
http://img.youtube.com/vi/JQ7a_8psCn0/mqdefault.jpg
OR
http://i.ytimg.com/vi/JQ7a_8psCn0/mqdefault.jpg
This is answer I gave for similar question, but it will solve your problem completely, just cut everything you don't want to be shown from the video with the wrapper div, this is done with html and css.
After searching the net a while for fix of this issue I came up with nothing, I think I have tried everything and nothing solved my problem. Then driven by my logic I just wrapped the iframe of the embedded youtube video in one div set overflow: hidden; to this div and made it's height 2px smaller then the iframe height (on my video there was black border in the bottom).
So the wrapper div is smaller then the iframe and with positioning it over the video you can hide the black borders you don't want.
I think that this is the best solution from everything I have tried so far.
From this example below try embedding just the iframe and you will see small black border at the bottom, and when you wrap the iframe in the div the border is gone, because the div is overlapping the iframe and it's smaller then the video, and it has overflow: hidden so everything that goes out of the div dimensions is hidden.
<div id="video_cont" style="width: 560px;
height: 313px;
overflow: hidden;">
<iframe id="the-video" class="extplayer" width="560" height="315" src="https://www.youtube.com/embed/EErx017kDHQ?&autoplay=0&rel=0&fs=0&showinfo=0&controls=0&hd=1&wmode=transparent&version=2;yt:quality=high;" frameborder="0" allowfullscreen></iframe>
</div>
In my case the border was with about 2px height so I made the wrapper div 2px smaller in height to hide the border, in your scenario if the border is on the top of the video or on the sides and/or with different dimensions, you have to make different dimensions for the wrapper div and position it good so it overlaps the video where the borders are and with overflow:hidden; the borders are hidden.
Hope this will help.
How youtube do it. There's lot of parameter in the image url.
https://i.ytimg.com/vi/XkOpbLBzPsY/hqdefault.jpg?custom=true&w=196&h=110&stc=true&jpg444=true&jpgq=90&sp=68&sigh=Gv-oyTIgA39e7UG01pZ2RiGbwSo
I'm not an expert, i was looking for a solution to remove the black bars of youtube video thumbnails, found a few solutions but didn't worked for me. Started experimenting with the solutions i found and came up with this.
https://jsfiddle.net/1fL2ubwy/
.row, .col, [class*="col-"] {
margin-left: 0;
margin-right: 0;
padding-left: 0;
padding-right: 0;
}
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
padding: 10px;
}
.yt-thumb {
width: 100%;
height: 74%;
overflow: hidden;
}
.yt-thumb > img {
margin: -10% 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap-theme.min.css">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-6 card vd-block mb-3">
<a class="yt-thumb" href="https://www.youtube.com/embed/wb49-oV0F78" data-rel="lightcase">
<img class="aligncenter" src="https://img.youtube.com/vi/wb49-oV0F78/hqdefault.jpg" alt="thumbnail" width="100%" height="auto">
</a>
</div>
<div class="col-md-6 col-sm-6 col-6 card vd-block mb-3">
<a href="https://www.youtube.com/embed/wb49-oV0F78" data-rel="lightcase">
<img class="aligncenter" src="https://img.youtube.com/vi/wb49-oV0F78/hqdefault.jpg" alt="thumbnail" width="100%" height="auto">
</a>
</div>
</div>
</div>
I needed a responsive way of doing it, so let's consider this code is running on the window.addEventListener("resize");
We basically want to convert the 4:3 ratio to 16:9
<div id="video-item">
<img src="https://i.ytimg.com/vi/{videoId}/hqdefault.jpg" />
</div>
const videoItem = document.getElementById("video-item");
const img = videoItem.querySelector("img");
resize()
{
img.style.top = `${-(img.offsetHeight - (img.offsetWidth * 9 / 16)) / 2}px`;
videoItem.style.height = `${9 / 16 * videoItem.offsetWidth}px`;
}
#video-item
{
position: relative;
overflow: hidden;
}
#video-item img
{
width: 100%;
position: absolute;
}
BTW, you can also fallback to the calculated height for the image (in case the image wasn't fully loaded)
img.style.top = `${-((img.offsetHeight || (3 / 4 * img.offsetWidth)) - (img.offsetWidth * 9 / 16)) / 2}px`;
Related
I am working on a 4x3 image gallery with pictures from a database.
All pictures have different sizes.
Using Bootstrap and CodeIgniter I make rows and columns for every image.
Now I want wide pictures to fit the height of the row, center horizontally and hide the horizontal overflow.
I also want that tall pictures fit the width of the column, center vertically and hide the vertical overflow. Just like Facebook.
margin-left: auto;
margin-right: auto; don't center the image.
My index page:
<h1><?=$title?></h1>
<?php $i = 1; foreach ($photos as $photo) {
if ( $i % 3 == 1 ) { ?>
<div class="row gallery-row">
<?php } ?>
<div class="col-md-4">
<img class="gallery-image" src="<?php echo site_url(); ?>assets/images/<?php echo $photo['photo_name'];?>">
</div>
<?php if ( $i % 3 == 0 ) { ?>
</div>
<?php }
$i++;
} ?>
My css:
.row.gallery-row{
height: 25%;
}
.gallery-image{
height : 100%;
overflow:hidden;
display: table-cell;
text-align: center;
}
The closest I get is with the CSS above: The images fit the height of the row and overflow is hidden.
I still want the images to center and if the images are taller then wide they should prioritize fitting the width.
Here is what the page looks like
The bottom left picture has a woman on it but it doesn't show because it isn't centered.
Solution: I changed my css to :
.gallery-image{
height : 100%;
width : 100%;
object-fit: cover;
}
My page now looks like this:
Adding some padding in between the rows will make it look just like facebook.
Maybe adding object-fit in your .gallery-image css is what you want.
have background images in nested divs as follows
<div id="templatemo_content" style="padding: 30px 30px 0 0; background: #fff url(images/foot_bg.png) no-repeat 0 bottom; z-index:10">
this div has grey colored background image
<div style="background:url(images/job.png) no-repeat 0 0; height:131px; z-index:5">
this is a nested div with another background image having right bottom overlapping grey colored image
</div>
</div>
below image is what i have achieved so far, however, expected image is the 2nd one below
used z-index to both images however browsing through internet found that z-index does not work on background images. please suggest a solution
You also need to set
position: relative;
Thus, the z-index will - should - be effective.
z-index is relative to the parent container from which z-index is set. As such, a child cannot have a lower z-index than its parent in terms of displaying below it.
You may want to change your HTML
<div>
<div>this is a nested div with another background image having right bottom overlapping grey colored image</div>
<div>this div has grey colored background image</div>
</div>
CSS (will require some alteration)
div {
position:relative;
background:#fff;
height:131px;
}
div:first-child {
background:url(images/job.png) no-repeat 0 0;
margin-top:30px;
height:100%;
width:100%;
}
div:last-child {
background:url(images/foot_bg.png) no-repeat 0 bottom;
position:absolute;
bottom:0px;
height:100%;
width:100%;
}
I am looking to handle files that were uploaded by users. The main issue in that situation is that they differ in size.
How can I center an image of unknown size, both vertically and horizontally, into a div?
Thanks a lot
EDIT: I am making a thumbnail for an image. Basically, I want to keep the div to the same size, and I want the image inside that div to fit the div, but without changing the scale. I am using overflow:hidden
EDIT:My code is
<div class='pic'><img id='theimage' src='image.png'></div>
and my CSS is
#theimage {
vertical-align: middle;
display: inline;
}
You can do this using the background CSS property. Give your <div> these properties:
div.whatever {
background-position: center center;
background-repeat: no-repeat;
}
Then in your HTML (because it's being generated dynamically), add a style="" attribute to the <div> with the URL of your image in it:
<div style="background-image: url('/path/to/image.png');"></div>
You could do this with an <img> tag inside the <div> too:
<div>
<img src="image.png">
</div>
With this CSS (untested, should work):
div {
text-align: center;
}
div img {
vertical-align: middle;
display: inline;
}
I'm assuming you've given your <div> a fixed width and height elsewhere.
Force the container to behave as a table cell.
#container {
display: table-cell;
vertical-align: middle;
text-align: center;
}
Demo
i got a suggestion though this thread is old
try
< div style='text-align:center;overflow:hidden;width:200px;height:200px;' >
< img src='anypath' width='190'style='vertical-align:middle;display:inline;' />
works just fine ! goodluck
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.
I'm a little stuck, it's due to my inexperience with html/css and iframes.
Basically, I've got a LH column, which is my menu. and a RH column which house's my content. the RH column is a iframe.
The problem is i can't get the height of the iframe to equal 100%, it's constrained by the height of the LH column.
See below;
http://www.therussianfrostfarmers.com/
it's driving me nuts, its seems so simple to fix.
Here's a portion of the relevant html;
<div id='wrapper'>
<div id='header'>
<img src="images/titleBart.gif" alt="rff"/>
</div>
<div id='menu'>
<div class='container'>
<%obj_itop%>
<plug:front_index />
<%obj_ibot%>
</div>
</div>
<div id='content'>
<!-- text and image -->
<plug:front_exhibit />
<!-- end text and image -->
</div>
<div class='clear-both'>
<!-- -->
</div>
</div>
and the corrosponding CSS;
#wrapper {
margin: 0 auto;
width:950px;
text-align: left;
background: #fff;
height:100% !important;
}
.clear-both { clear: both; }
#content {
width: 760px;
margin: 20px 0 0 190px;
padding:0;
height: 100% !important;
overflow: auto;
}
#menu {
width: 170px;
position:absolute;
margin:0;
padding:0;
overflow: auto;
}
.container {
margin:0;
padding:0;
}
Any help would be much appreciated,
thanks cam
I'm not 100% sure on this, but I'm fairly certain that there is no way to do this without using JavaScript to dynamically size the iFrame. And if there is, it probably isn't easy.
It's happening because '100%' in CSS terms only takes up as much space as it can of what is already on the page. Since you have a left-hand column already, 100% will only go to the size of that column.
The iFrame will need to resized using javascript - you can do it fairly easily using something like the below:
<body onload="resizeFrame(document.getElementById('myframe'))">
<iframe id="myframe" src="http://www.example.com/foo.html" />
<script type=”text/javascript”>
function resizeFrame(f)
{
// Get height of iframe once it has loaded content and then
// use this for the iframe height in parent doc
f.style.height = f.contentWindow.document.body.scrollHeight + “px”;
}
</script>
The above will evaluate the height of the rendered content in the iframe, take that height and then use it as the height of the iframe container.
Note, the above will work if your iframed content is on the same domain as the containing page. If it isn't, you'll run into security restrictions due to the same origin policy, which is a little trickier to get around!
ok, i believe the iframe has already got this sort of JS function applied to it;
<script type='text/javascript'>
function iframer()
{
// get width of #content
frame_x = $('#content').width();
// get height of #menu
frame_y = $('#menu').height();
// apply height and width
$('#iframed').css('width', frame_x);
$('#iframed').css('height', frame_y);
}
$(document).ready( function() { iframer(); } );
$(window).resize( function() { iframer(); } );
</script>
<iframe src='$url' frameborder='0' id='iframed'></iframe>
EOH;
return $iframe;
if i change the #menu height to 50% and grey the BG then its easier to see how the #menu div is effecting the height of the #content div.
http://www.therussianfrostfarmers.com/