How can I access thumbnail collection of a YouTube video using the link of the video from the YouTube API. I want thumbnails to be displayed on website using PHP using the video id stored in a variable for example $link

Đang xem: Show image thumbnail by youtube video url using php

YouTube stores many different types of thumbnails on its server for different devices. You can access it by using the video id which every YouTube video has. You can display the images on your website using a variable $link which holds the id of the video and substituting itin the place for video_ID in the link.

Low quality thumbnail:

http://img.youtube.com/vi//sddefault.jpgMedium quality thumbnail:

http://img.youtube.com/vi//mqdefault.jpgHigh quality thumbnail:

http://img.youtube.com/vi//hqdefault.jpgMaximum quality thumbnail:

http://img.youtube.com/vi//maxresdefault.jpgExample:

If you want to access the thumbnail of the following video:

https://www.youtube.com/watch?v=Q-GYwhqDo6oVideo ID : Q-GYwhqDo6o

So, this is how video thumbnail link looks like:

http://img.youtube.com/vi/Q-GYwhqDo6o/mqdefault.jpgHope it helps. Enjoy coding.

Share
Improve this answer
Follow
edited Feb 14 “19 at 10:20

*

Ivijan Stefan Stipić
4,59355 gold badges3232 silver badges6868 bronze badges
answered Sep 2 “15 at 6:51

*

RonRon
1,6001616 silver badges2525 bronze badges
0
Add a comment |
12
To get high-quality image you can use the following URL which is fetched from youtube API

$video_id = explode(“?v=”, $link);$video_id = $video_id<1>;$thumbnail=”http://img.youtube.com/vi/”.$video_id.”/maxresdefault.jpg”;
Share
Improve this answer

Xem thêm:

Follow
answered Nov 29 “17 at 6:04

*

RishiRishi
40844 silver badges1313 bronze badges
4
Add a comment |
4
You can use the below code. It is work for me. Choose the image quality as per your requirement.

Share
Improve this answer
Follow
answered Jan 11 “19 at 9:59

*

Kamal SanghaviKamal Sanghavi
9977 bronze badges
1
Add a comment |

Xem thêm:

2
here is my handy function to download the Youtube thumbnail image

function downloadYouTubeThubnailImage($youTubeLink=””,$thumbNamilQuality=””,$fileNameWithExt=””,$fileDownLoadPath=””) { $videoIdExploded = explode(“?v=”, $youTubeLink); if ( sizeof($videoIdExploded) == 1) { $videoIdExploded = explode(“&v=”, $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode(“&”,$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); }else{ $videoIdExploded = explode(“?v=”, $youTubeLink); $videoIdEnd = end($videoIdExploded); $removeOtherInVideoIdExploded = explode(“&”,$videoIdEnd); $youTubeVideoId = current($removeOtherInVideoIdExploded); } switch ($thumbNamilQuality) { case “LOW”: $imageUrl = “http://img.youtube.com/vi/”.$youTubeVideoId.”/sddefault.jpg”; break; case “MEDIUM”: $imageUrl = “http://img.youtube.com/vi/”.$youTubeVideoId.”/mqdefault.jpg”; break; case “HIGH”: $imageUrl = “http://img.youtube.com/vi/”.$youTubeVideoId.”/hqdefault.jpg”; break; case “MAXIMUM”: $imageUrl = “http://img.youtube.com/vi/”.$youTubeVideoId.”/maxresdefault.jpg”; break; default: return “Choose The Quality Between < LOW (or) MEDIUM (or) HIGH (or) MAXIMUM>“; break; } if( empty($fileNameWithExt) || is_null($fileNameWithExt) || $fileNameWithExt === “”) { $toArray = explode(“/”,$imageUrl); $fileNameWithExt = md5( time().mt_rand( 1,10 ) ).”.”.substr(strrchr(end($toArray),”.”),1); } if (! is_dir($fileDownLoadPath)) { mkdir($fileDownLoadPath,0777,true); } file_put_contents($fileDownLoadPath.$fileNameWithExt, file_get_contents($imageUrl)); return $fileNameWithExt; }Function Description

Argumemts

$youTubeLink Youtube url for example https://www.youtube.com/watch?v=a3ICNMQW7Ok

$thumbNamilQuality It has Many Quality Such as LOW ,MEDIUM, HIGH, MAXIMUM

Thumbnail Quality list Taken from https://darkedeneurope.com/a/32346348/8487424

&& https://darkedeneurope.com/a/47546113/8487424

$fileNameWithExt File Name with Extension**for example** myfavouriteimage.png

NOTE $fileNameWithExt is not mandatory it will generate the uuid based file name for Example 91b2a30d0682058ebda8d71606f5e327.jpg

if you want to put the file to the custom directory use this argument

NOTE $fileDownLoadPath is not mandatory it will generate the image file where the script is executing

Some of the sample examples

$folderpath = “c:”.DIRECTORY_SEPARATOR.”xampp”.DIRECTORY_SEPARATOR.”htdocs”.DIRECTORY_SEPARATOR.”youtube”.DIRECTORY_SEPARATOR;$imageName = “mybeautfulpic.jpg”;downloadYouTubeThubnailImage(“https://www.youtube.com/watch?v=a3ICNMQW7Ok”,”MAXIMUM”,null,$folderpath );downloadYouTubeThubnailImage(“https://www.youtube.com/watch?v=a3ICNMQW7Ok”,”LOW”,$imageName ,null);Hope it is answered already but this function has some exta features

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *