如何从嵌入到Metabox中的视频生成特色图像

时间:2015-12-31 作者:Madeirense

我有一个metabox,可以将Youtube和Vimeo中的视频添加到视频帖子格式的帖子中。我现在正在研究自动生成缩略图,这些文章与视频后格式。这是我的meatabox代码,允许我嵌入视频:

function my_theme_show_post_video_metabox( $post ) {

  wp_nonce_field( \'my_theme_post_video_metabox\', \'_my_theme_post_video_metabox\' );
  $video = get_post_meta( $post->ID, \'_my_theme_post_video\', true );

  echo \'<h4 style="margin: 10px 0 0 0;">URL - YouTube or Vimeo</h4>\';
  echo \'<input type="text" id="_my_theme_post_video" name="_my_theme_post_video" value="\' . $video . \'" style="width: 100%;" />\';

}
现在,我如何从嵌入此元数据库的视频中生成缩略图,以用作后期特色图像?

1 个回复
SO网友:jgraup

您需要涵盖几个步骤。

从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 );

相关推荐

保存Metabox内容无效

我正在尝试保存一个metabox输入,但它似乎不起作用。我使用的是数组(因为我需要我的metabox有60行),所以我假设问题就出在数组中。这是我为管理员提供的metabox函数(它可以正确显示我要显示的信息):function mock_metabox() { global $post; // Nonce field wp_nonce_field( basename( __FILE__ ), \'mock_fields\' ); // in