Created
March 31, 2016 08:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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