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

"Olivia, my eldest daughter, caught measles when she was seven years old. As the illness took its usual course I can remember reading to her often in bed and not feeling particularly alarmed about it. Then one morning, when she was well on the road to recovery, I was sitting on her bed showing her how to fashion little animals out of coloured pipe-cleaners, and when it came to her turn to make one herself, I noticed that her fingers and her mind were not working together and she couldn’t do anything. 'Are you feeling all right?' I asked her. 'I feel all sleepy,' she said. In an hour, she was unconscious. In twelve hours she was dead. The measles had turned into a terrible thing called measles encephalitis and there was nothing the doctors could do to save her. That was...in 1962, but even now, if a child with measles happens to develop the same deadly reaction from measles as Olivia did, there would still be nothing the doctors could do to help her. On the other hand, there is today something that parents can do to make sure that this sort of tragedy does not happen to a child of theirs. They can insist that their child is immunised against measles. ...I dedicated two of my books to Olivia, the first was ‘James and the Giant Peach’. That was when she was still alive. The second was ‘The BFG’, dedicated to her memory after she had died from measles. You will see her name at the beginning of each of these books. And I know how happy she would be if only she could know that her death had helped to save a good deal of illness and death among other children."

I just checked google books for BFG, and the dedication is there. 

https://www.google.com.au/books/edition/_/quybcXrFhCIC?hl=en&gbpv=1 


Roald Dahl, 1986