add a youtube video preloader function, lazy load youtube [fixed]

I noticed that this list page: https://kruxor.com/list/rs3_efficient_skill/

Loads about 29 (well exactly 29) youtube video embeds and was wondering if there was an easy way to replace these iframes with the image thumbnail to make the page load time a bit better.

I think this is also known as lazy loading.

Here is a method i found on css tricks: https://css-tricks.com/lazy-load-embedded-youtube-videos/ 

This does not actually use any additional javascript to achieve this but rather the srcdoc feature of the iframe element. Very clever. 

I already have a general function that can create an iframe from a youtube video url, i will try and add this new code into this function so i can just generate it for any video!

File: functions.class.php

Class: functions

Function: youtube_thumbnail();

Function Options: $url = "", $ttype = "hqthumb", $height = "420", $autoplay = false

Notes: it needs to have the links in srcdoc to not have the ' or " of this seems to break it. 

HTML

<iframe
  width="560"
  height="315"
  src="https://www.youtube.com/embed/Y8Wp3dafaMQ"
  srcdoc="<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href=https://www.youtube.com/embed/Y8Wp3dafaMQ?autoplay=1><img src=https://img.youtube.com/vi/Y8Wp3dafaMQ/hqdefault.jpg alt='Video The Dark Knight Rises: What Went Wrong? – Wisecrack Edition'><span>▶</span></a>"
  frameborder="0"
  allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
  title="The Dark Knight Rises: What Went Wrong? – Wisecrack Edition"
></iframe>

PHP

/* Here is the original embed function */
function youtube_thumbnail($url = "", $ttype = "hqthumb", $height = "420", $autoplay = false) {
	$url_array = parse_url($url);
	if(!is_array($url_array)) {
		return false;
	}
	if(!isset($url_array["query"])) {
		return false;
	}
	parse_str($url_array["query"],$query_string_array); // pass into array rather than variables.
	// var_dump($query_string_array);
	if(!isset($query_string_array['v'])) { return false; }
	$v = $query_string_array['v'];

	$thumb = "";
	if($ttype == "default") {
		$thumb = "https://img.youtube.com/vi/$v/default.jpg";
	}
	if($ttype == "hqthumb") {
		$thumb = "https://img.youtube.com/vi/$v/hqdefault.jpg";
	}
	if($ttype == "iframe") {
		$thumb = "<iframe width='100%' height='$height' src='https://www.youtube.com/embed/$v?rel=0' frameborder='0' allowfullscreen></iframe>";
		if($autoplay) {
			$thumb = "<iframe width='100%' height='$height' src='https://www.youtube.com/embed/$v?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe>";
		}
	}
	return $thumb;
}


// added an iframe_lazy option to this function
    if($ttype == "iframe_lazy") {
      $thumb = '<iframe width="560" height="'.$height.'" src="https://www.youtube.com/embed/'. $v .'?rel=0" srcdoc="<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href=https://www.youtube.com/embed/'. $v .'?autoplay=1><img src=https://img.youtube.com/vi/'. $v .'/hqdefault.jpg><span>▶</span></a>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
  	}

// new function
function youtube_thumbnail($url = "", $ttype = "hqthumb", $height = "420", $autoplay = false) {
	$url_array = parse_url($url);
	if(!is_array($url_array)) {
		return false;
	}
	if(!isset($url_array["query"])) {
		return false;
	}
	parse_str($url_array["query"],$query_string_array); // pass into array rather than variables.
	// var_dump($query_string_array);
	if(!isset($query_string_array['v'])) { return false; }
	$v = $query_string_array['v'];

	$thumb = "";
	if($ttype == "default") {
		$thumb = "https://img.youtube.com/vi/$v/default.jpg";
	}
	if($ttype == "hqthumb") {
		$thumb = "https://img.youtube.com/vi/$v/hqdefault.jpg";
	}
	if($ttype == "iframe") {
		$thumb = "<iframe width='100%' height='$height' src='https://www.youtube.com/embed/$v?rel=0' frameborder='0' allowfullscreen></iframe>";
		if($autoplay) {
			$thumb = "<iframe width='100%' height='$height' src='https://www.youtube.com/embed/$v?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe>";
		}
	}
    if($ttype == "iframe_lazy") {
      $thumb = '<iframe width="560" height="'.$height.'" src="https://www.youtube.com/embed/'. $v .'?rel=0" srcdoc="<style>*{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black}</style><a href=https://www.youtube.com/embed/'. $v .'?autoplay=1><img src=https://img.youtube.com/vi/'. $v .'/hqdefault.jpg><span>▶</span></a>" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
  	}
	return $thumb;
}

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 367 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
There is a qualitative and quantitative difference between a day that begins with a little exercise, a book, meditation, a good meal, a thoughtful walk, and the start of a day that begins with a smartphone in bed.
Unknown