How To Get Youtube Video Thumbnails From URL In PHP - Laravel; In this post, I will give you a custom-made PHP function for getting a youtube thumbnail image from the video youtube URL. With the help of the given PHP Function pass a youtube URL and the function will return a thumbnail image URL. You can use this function in laravel also.
To get Youtube thumbnail image URL from the video you need to take the help of the below-suggested function in your PHP or laravel application. In this function, you can still make manually changes according to your requirement.
Thumbnail image URL from youtube video URL this function will find out and return image URL. This function is easy and very simple to understand or make customization according to requirements.
How To Validate Youtube Url In laravel Best Practical Example
Just copy this PHP function for getting the thumbnail image URL from the youtube video. In this function, we will pass an argument. The argument is Youtube video URL and this function will make a youtube thumbnail image URL from the video URL and return it.
<?php
function parseVideos($videoString = null){
// RETURN DATA
$videos = array();
if (!empty($videoString)) {
// SPLIT ON LINE BREAKS
$videoString = stripslashes(trim($videoString));
$videoString = explode("\n", $videoString);
$videoString = array_filter($videoString, 'trim');
// CHECK EACH VIDEO FOR PROPER FORMATTING
foreach ($videoString as $video) {
// CHECK FOR IFRAME TO GET THE VIDEO URL
if (strpos($video, 'iframe') !== FALSE) {
// RETRIEVE THE VIDEO URL
$anchorRegex = '/src="(.*)?"/isU';
$results = array();
if (preg_match($anchorRegex, $video, $results)) {
$link = trim($results[1]);
}
} else {
// WE ALREADY HAVE A URL
$link = $video;
}
// IF WE HAVE A URL, PARSE IT DOWN
if (!empty($link)) {
// INITIAL VALUES
$video_id = NULL;
$videoIdRegex = NULL;
$results = array();
// CHECK FOR TYPE OF YOUTUBE LINK
if (strpos($link, 'youtu') !== FALSE) {
if (strpos($link, 'youtube.com') !== FALSE) {
if (strpos($link, 'youtube.com/watch') !== FALSE) {
$videoIdRegex = '/[\?\&]v=([^\?\&]+)/';
}else{
// works on:
// http://www.youtube.com/embed/VIDEOID
// http://www.youtube.com/embed/VIDEOID?modestbranding=1&rel=0
// http://www.youtube.com/v/VIDEO-ID?fs=1&hl=en_US
///[\?\&]v=([^\?\&]+)/
$videoIdRegex = '/youtube.com\/(?:embed|v){1}\/([a-zA-Z0-9_]+)\??/i';
}
} else if (strpos($link, 'youtu.be') !== FALSE) {
// works on:
// http://youtu.be/daro6K6mym8
$videoIdRegex = '/youtu.be\/([a-zA-Z0-9_]+)\??/i';
}
if ($videoIdRegex !== NULL) {
if (preg_match($videoIdRegex, $link, $results)) {
$video_str = 'https://www.youtube.com/embed/%s?autoplay=0';
$thumbnail_str = 'http://img.youtube.com/vi/%s/2.jpg';
$fullsize_str = 'http://img.youtube.com/vi/%s/0.jpg';
$video_id = $results[1];
$video_type = 'youtube';
}
}
}
// CHECK IF WE HAVE A VIDEO ID, IF SO, ADD THE VIDEO METADATA
if (!empty($video_id)) {
// add to return
$videos = array(
'url' => sprintf($video_str, $video_id),
'thumbnail' => sprintf($fullsize_str, $video_id),
'video_type' => sprintf($video_type, $video_id),
);
}else{
$videos = array(
'url' => null,
'thumbnail' => null,
'video_type' => '0',
);
}
}
}
}
// RETURN ARRAY OF PARSED VIDEOS
return $videos;
}
$response = parseVideos('YOUTUBE URL');
// RESPONS OUTPUT
[
"url" => "" // VIDEO URL
"thumbnail" => "" // VIDEO THUMBNAIL
"video_type" => "youtube"
]
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- Laravel 10 Handle Image Upload Example Tutorial
- Laravel Email Send
- Microsoft Authentication Implement on Laravel App
- combine() Method | Laravel Collection Example
- [Fixed] 429: Too Many Attempts When Call API In Laravel 8 9
- PhonePe Payment Gateway Example PHP
- Get Zipcode/Pincode From Latitude and Longitude PHP and Google Maps API
- intervention/image change image format laravel tutorial
- Laravel Debugging Methods With Example
- Laravel Custom Command Make Example