您需要涵盖几个步骤。
从MetaBox embed获取ID根据ID提取可用大小的列表,如果图像不存在,请将图像导入到库中,并附加新文件以发布
STEP 2 | YouTube
从YouTube生成一个大小数组以供以后使用:
function getYoutTubeImagesFromID($yt_id, $prefix = \'https:\') {
$yt_url = \'img.youtube.com\'; // can be \'i3.ytimg.com\'
$sizes = array(0, 1, 2, 3, "default", "hqdefault", "mqdefault", "sddefault", "maxresdefault");
$result = array();
foreach($sizes as $size) {
$result [ $size ] = "{$prefix}//img.youtube.com/vi/{$yt_id}/{$size}.jpg";
}
return array(
\'id\' => $yt_id,
\'link\' => "{$prefix}//youtu.be/{$yt_id}",
\'sizes\' => $result,
);
}
要测试这一点,请循环查看ID列表。
$yt_videos = array(\'cV5yxrZpuXg\', \'LtBTHqi8Gmw\', \'VEAY-nRlP5c\', \'IWeasYydL8Y\', \'fCVXpcR9KDY\');
foreach($yt_videos as $yt_id) {
$ytImages = getYoutTubeImagesFromID($yt_id);
$src = $ytImages[ \'sizes\' ][ \'hqdefault\' ];
$link = $ytImages[ \'link\' ];
echo "<a href=\\"{$link}\\" target=\\"_blank\\" ><img src=\\"{$src}\\" ></a>";
print_r($ytImages);
}
它们应各自生成可用于导入的大小信息。
Array
(
[id] => cV5yxrZpuXg
[link] => https://youtu.be/cV5yxrZpuXg
[sizes] => Array
(
[0] => https://img.youtube.com/vi/cV5yxrZpuXg/0.jpg
[1] => https://img.youtube.com/vi/cV5yxrZpuXg/1.jpg
[2] => https://img.youtube.com/vi/cV5yxrZpuXg/2.jpg
[3] => https://img.youtube.com/vi/cV5yxrZpuXg/3.jpg
[default] => https://img.youtube.com/vi/cV5yxrZpuXg/default.jpg
[hqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/hqdefault.jpg
[mqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/mqdefault.jpg
[sddefault] => https://img.youtube.com/vi/cV5yxrZpuXg/sddefault.jpg
[maxresdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/maxresdefault.jpg
)
)
<小时>
STEP 2 | Vimeo
收集Vimeo视频信息
function getVimeoImagesFromID($vimeo_id, $prefix = \'https:\') {
$vimeo_url = "{$prefix}//vimeo.com/api/v2/video/{$vimeo_id}.php";
if( ! $vimeo_hash = @file_get_contents($vimeo_url)) {
return false;
}
$result = unserialize($vimeo_hash);
return ( count($result) === 1 ) ? array_shift($result) : $result;
}
// your vimeo id
$vimeo_id = 71472926;
// get available information for video
if($vimeo_hash = getVimeoImagesFromID($vimeo_id)) {
// grab the largest image possible
$vimeo_src = $vimeo_hash [ \'thumbnail_large\' ];
echo $vimeo_src; // https://i.vimeocdn.com/video/445043557_640.jpg
}
<小时>
STEP 3 | Sideload Image
media_sideload_image 从YT到服务器的图像。一定要抓住
thumbnail_id
从产生的
src
.
// your youtube id
$yt_id = \'cSXlOeKusWs\';
// size information for id
$ytImages = getYoutTubeImagesFromID($yt_id);
// max res image
$url = $ytImages[ \'sizes\' ][ \'maxresdefault\' ];
// sideload image
$src = media_sideload_image(esc_url($url), NULL, \'\', \'src\');
// convert src to id
$thumbnail_id = attachment_url_to_postid($src);
<小时>一
Array
(
[id] => cV5yxrZpuXg
[link] => https://youtu.be/cV5yxrZpuXg
[sizes] => Array
(
[0] => https://img.youtube.com/vi/cV5yxrZpuXg/0.jpg
[1] => https://img.youtube.com/vi/cV5yxrZpuXg/1.jpg
[2] => https://img.youtube.com/vi/cV5yxrZpuXg/2.jpg
[3] => https://img.youtube.com/vi/cV5yxrZpuXg/3.jpg
[default] => https://img.youtube.com/vi/cV5yxrZpuXg/default.jpg
[hqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/hqdefault.jpg
[mqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/mqdefault.jpg
[sddefault] => https://img.youtube.com/vi/cV5yxrZpuXg/sddefault.jpg
[maxresdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/maxresdefault.jpg
)
)
thumbnail_id.set_post_thumbnail( $post, $thumbnail_id );