In questo tutorial creeremo una funzione in grado di restituire una thumbnail di un video YouTube.
Il funzionamento è molto semplice, è necessario estrarre l’ID (in formato alfanumerico) ed aggiungerlo a “http://img.youtube.com/vi/IDALFANUMERICO.0.jpg”. Basta con la teoria! Vediamo di mettere in pratica quanto detto.

 
function youtube_video_thumbnail($url,$index=0){
if(preg_match(‘/^[^v]+v.(.{11}).*/’,$url,$matches)){ //url video
return ‘http://img.youtube.com/vi/’.$matches[1].‘/’.$index.‘.jpg’;
}elseif(preg_match(‘/youtube.com\/user\/(.*)\/(.*)$/’,$url,$matches)){ //url video visualizzato dal canale
return ‘http://img.youtube.com/vi/’.$matches[2].‘/’.$index.‘.jpg’;
}else{
return false;
}
}
 

ESEMPIO APPLICATO

 
//restituisce la thumbnail del video
echo youtube_video_thumbnail("http://www.youtube.com/watch?v=IDALFANUMERICO");
 

Share on FacebookCondividi su facebook

fonte: www.sastgroup.com » Vai al post originale