Skip to content

Instantly share code, notes, and snippets.

@RonnyO
Created March 31, 2016 08:49
Show Gist options
  • Save RonnyO/878c8dc6f0a5a8372006c0859ef28cb5 to your computer and use it in GitHub Desktop.
Save RonnyO/878c8dc6f0a5a8372006c0859ef28cb5 to your computer and use it in GitHub Desktop.
getLargestYoutubeThumbnail - Because maxresdefault.jpg isn't reliable. Requires jQuery/Zepto, example here: http://jsbin.com/bamiju/edit?html,css,js,output
// getLargestYoutubeThumbnail by @ReallyGoodTeam, MIT
/* Sample youtube IDs
teYZM0Aft0A
wr1fa3ZaNYY
8e-BsJoaS20 - doesn't have maxresdefault
*/
function getLargestYoutubeThumbnail(youtubeId) {
var def = $.Deferred();
var img = new Image();
img.onload = function() {
if (this.width < 720) {
def.resolve(img.src.replace('maxres', 'hq'));
} else {
def.resolve(img.src)
}
};
img.src = 'https://img.youtube.com/vi/' + youtubeId + '/maxresdefault.jpg'
return def.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment