Skip to content

Instantly share code, notes, and snippets.

@amilabandara
Last active February 10, 2021 10:28
Show Gist options
  • Save amilabandara/bca39c9fbfffd5caf810be5892cf0940 to your computer and use it in GitHub Desktop.
Save amilabandara/bca39c9fbfffd5caf810be5892cf0940 to your computer and use it in GitHub Desktop.
Get youtube image url from video url
<?php
$url = 'https://www.youtube.com/watch?v=DkxMhdtOnKM';
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
$image_url = 'https://img.youtube.com/vi/'.$youtube_id.'/maxresdefault.jpg';
echo $image_url;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment