Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Get Thumbnail From Youtube Video Using PHP

Friday, Jan 27, 2017

Tags:- PHP HTML


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.


See Demo
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.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Are You Starting A New Blog
Do You Want Best Web Hosting?

Try World's Best Web Hosting Provider BlueHost
At Just $3.95/mo

Hurry Up! Limited Time Offer

Yes, I Want To Check Out BlueHost

Check The World's Best SEO And All-In-One Marketing Toolkit
For Digital Marketing Professionals SEMRUSH

Check Out SEMRUSH