Posted in site bugs
14894
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
524
This Month
1985
This Year
4259

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
The problem with hoarding is you end up living off your reserves. Eventually, you’ll become stale. If you give away everything you have, you are left with nothing. This forces you to look, to be aware, to replenish. . . . Somehow the more you give away, the more comes back to you.
Paul Arden
Random CSS Property

inset-inline-end

The inset-inline-end CSS property defines the logical inline end inset of an element, which maps to a physical offset depending on the element's writing mode, directionality, and text orientation. It corresponds to the top, right, bottom, or left property depending on the values defined for writing-mode, direction, and text-orientation.
inset-inline-end css reference