esedic
2/25/2016 - 2:19 PM

Get Youtube thumbnail from URL

Get Youtube thumbnail from URL

<?php 

 $_text = str_replace(
		array(
  			'//www.youtube.com',
  			'//youtube.com',
  			'https://www.youtube.com',
  			'https://youtube.com',
  			'https://www.youtu.be',
  			'https://youtu.be'
  		),
  		'http://www.youtube.com',
  		$_text
	);
	
	// Example URL
	 $_text = 'https://www.youtube.com/watch?v=Uph5MuE_CJg';

  // Change maxresdefault if it's not a HD video
	if (preg_match_all('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^>"&?/ ]{11})%i', $_text, $match)) {
      $imgsrc = 'http://img.youtube.com/vi/' . $match[1][0] . '/maxresdefault.jpg';
	}
?>

<?php echo '<img src="'.$imgsrc.'" alt="">'; ?>