Posted in site bugs
15
2:58 am, January 21, 2021

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;
}

View Statistics
This Week
75
This Month
513
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Related Search Terms
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.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote
In this case my anchor this week becomes driving almost 2hrs outside of Atlanta to one of my favorite hard core gyms in the world.. MetroFlex aka The Dungeon. The gym owners turn the heat way up so it becomes a fun sweat box and the gym members just watch from afar and leave me alone. I happily drive myself long distances to find MY ANCHOR. Our anchor allows us to have balance, focus and be as productive as possible. And if you're in the middle of a heavy set and your headphones start to fall off your head, like mine did here.. well.. f*ck the headphones. Let em break and fall. You can always get a new pair, but the iron ain't ever gonna lift itself.
Unknown