Posted in site bugs
14896
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
526
This Month
1987
This Year
4261

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 here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
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
Random CSS Property

font-variant-numeric

The font-variant-numeric CSS property controls the usage of alternate glyphs for numbers, fractions, and ordinal markers.
font-variant-numeric css reference