Header Information


  • Home
  • Blogs
  • Fetch Image Thumbnail by Youtube Video Url Using PHP

Fetch Image Thumbnail by Youtube Video Url Using PHP

Thumbnails are used for preview a single or group of media entities in a single explorer window.  Youtube automatically generates (at least) three thumbnails and one image preview for every uploaded video, and you can easily get to them once you know how.

You Need followings to get thumbnail from youtube :

  • Html Form Design
  • Youtube Video Link OR Url (Want to fetch the thumbnail)
  • A Small Or Easy PHP Script

Here we have A simple design of form which ask youtube link and after pressing submit button, thumbnail is shown in img tag below the form. So let's  start  first of all we design simple form for submitting youtube video URL


Now we have to write PHP code in starting of this page in PHP tag. We have to launch localhost server or live hosting server. if you don't know, how to setup and run localhost server on your own Computer. See our another article on How to setup php webserver on local system. 

  $url = $_POST["url"];
  preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
  $youtube_id = $match[1];
  $img = "https://img.youtube.com/vi/".$youtube_id."/0.jpg";
}else{ $video_id = ""; }

Here Details of some of function of code

Preg_match is used to extract video id from the given youtube url. and

Link in $img = "https://img.youtube.com/vi/"; => this link is where every video thumbnail is stored, we can access our thumbnail by sending video id in link. when you enter a link in form and submit, then video's thumnail is shows in img tag.


No Comments yet. Be the first one to comment on this article.

Leave a Comment

You've to login for posting comment on this article.