Removing black borders 4:3 on youtube thumbnails

PhpJavascriptJqueryYoutube Api

Php Problem Overview


E.g. I have a link

http://img.youtube.com/vi/aOPGepdbfpo/0.jpg

for a youtube video thumbnail:

enter image description here

And I would like to remove the black top and bottom border so I get a picture like this:

enter image description here

Could it be done using PHP function javascript/jQuery or maybe youtube api itself?

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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;
}

Solution 3 - Php

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%;
}

Solution 4 - Php

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

  1. http://img.youtube.com/vi/JQ7a_8psCn0/mqdefault.jpg

OR

  1. http://i.ytimg.com/vi/JQ7a_8psCn0/mqdefault.jpg

Solution 5 - Php

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?&amp;autoplay=0&amp;rel=0&amp;fs=0&amp;showinfo=0&amp;controls=0&amp;hd=1&amp;wmode=transparent&amp;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.

Solution 6 - Php

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

Solution 7 - Php

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>

Solution 8 - Php

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`;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerfderView Question on Stackoverflow
Solution 1 - PhpTheCarverView Answer on Stackoverflow
Solution 2 - PhpRich BradshawView Answer on Stackoverflow
Solution 3 - PhpSergi RamónView Answer on Stackoverflow
Solution 4 - PhpPPBView Answer on Stackoverflow
Solution 5 - PhpdekissView Answer on Stackoverflow
Solution 6 - PhpGinoView Answer on Stackoverflow
Solution 7 - PhpSampath KumarView Answer on Stackoverflow
Solution 8 - PhpIdoView Answer on Stackoverflow