我正在尝试使用PHP从Youtube频道中检索最近上传的视频列表,但缩略图都是4:3格式,顶部和底部有黑色边框.我使用以下内容来检索缩略图:
$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
Run Code Online (Sandbox Code Playgroud)
这导入它们:
<img src="<?php echo $thumbnail;?>"/>
Run Code Online (Sandbox Code Playgroud)
youtube API网站和其他各种问答网站都说你可以使用这个变量
yt:name='mqdefault'
Run Code Online (Sandbox Code Playgroud)
检索没有边框的16:9缩略图,但是我不清楚在哪里将它合并到PHP中?
http://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
Run Code Online (Sandbox Code Playgroud)
你需要你的网址看起来类似于其中一个.不确定:
(string)$media->group->thumbnail[0]->attributes()->url;返回但我会查看该URL并确保它类似于上面的内容.
对于下面的示例,您希望URL与此类似.
http://img.youtube.com/vi/0GQPoyMr30o/mqdefault.jpg
http://img.youtube.com/vi/0GQPoyMr30o/maxresdefault.jpg
Run Code Online (Sandbox Code Playgroud)
尝试做这样的事情:
<?php
$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
$thumbnail = str_replace('0.jpg', 'mqdefault.jpg', $thumbnail);
?>
<img src="<?php echo $thumbnail; ?>" />
Run Code Online (Sandbox Code Playgroud)