All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Thumbnail From Youtube Video Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Riya

In this tutorial we will show you how to get thumbnail from youtube video using PHP. You can get any video thumbnail by entering youtube video url in text box and choose quality of image like high quality, medium quality, low quality etc.

You may also like youtube style rating system.

Get Thumbnail From Youtube Video Using PHP

To Get Thumbnail From Youtube Video It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to get thumbnail from youtube video
  3. Make a CSS file and define styling

Step 1. Make a HTML file and define markup

We make a HTML file and save it with a name thumbnail.html

<html>
<head>
<link href="thumbnail_style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="wrapper">

<div class="thumbnail_div">
<form method="post" action="get_thumbnail.php">
<input type="text" name="url" placeholder="Enter URL">
<input type="submit" name="get_thumbnail" value="GET THUMBNAIL">
</form>
</div>

</div>
</body>
</html>

In this step we create a form to enter youtube video url and send it to get_thumbnail.php page where we extract thumbnail from url.

You may also like extract url data like facebook using PHP and jQuery.

Step 2. Make a PHP file to get thumbnail from youtube video

We make a PHP file and save it with a name get_thumbnail.php

<?php
if(isset($_POST['get_thumbnail']))
{
 $url=$_POST['url'];
 $fetch=explode("v=", $url);
 $videoid=$fetch[1];
 echo '<img src="http://img.youtube.com/vi/'.$videoid.'/0.jpg" width="250"/>';
}
?>

// For Thumbnail Quality Type
http://img.youtube.com/vi/'.$videoid.'/default.jpg
http://img.youtube.com/vi/'.$videoid.'/hqdefault.jpg
http://img.youtube.com/vi/'.$videoid.'/mqdefault.jpg
http://img.youtube.com/vi/'.$videoid.'/sddefault.jpg
http://img.youtube.com/vi/'.$videoid.'/maxresdefault.jpg

In this step we get the youtube video url and get value video id by using explode() function which is after the 'v' variable in url.

We use youtube image storing subdomain 'img.youtube.com' and write full path where youtube store its video thumbnail and write '0' which is the default video thumbnail.

You can use 0, 1, 2 and 3 for different thumbnails of a particular video.You may also like upload image from url.

Step 3. Make a CSS file and define styling

We make a CSS file and save it with a name thumbnail_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#2C3E50;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:white;
}
#wrapper h1 p
{
font-size:18px;
}
.form_div input[type="text"]
{
 width:380px;
 height:50px;
 border-radius:2px;
 font-size:17px;
 padding-left:5px;
 border:1px solid silver;
}
.form_div input[type="submit"]
{
 width:150px;
 height:50px;
 border:none;
 border-radius:2px;
 font-size:15px;
 background-color:#7F8C8D;
 border-bottom:3px solid #616A6B;
 color:white;
 font-weight:bold;
}

That's all, this is how to get thumbnail from youtube video using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial. I hope this tutorial on how to get thumbnail from youtube video using PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪