【问题标题】:How to render a Youtube thumbnail in a template in Drupal 7如何在 Drupal 7 的模板中渲染 Youtube 缩略图
【发布时间】:2011-11-19 20:46:11
【问题描述】:

我正在使用以下模块:

  • 媒体
  • media_youtube
  • 样式

并希望在模板 (.tpl) 中呈现 Youtube 视频的缩略图。我应该使用哪个主题功能以及使用什么参数?

我最好的问题是:

$my_media['style_name'] = 'unlinked_thumbnail';
print theme('file_styles',$my_media);

其中 $my_media 是一个包含 fid、uri、filename、filemime 等的数组。

由于我是 Drupal 的新手,我所有试图理解模块源代码的尝试都失败了。我觉得我已经尝试了在 youtube 和样式模块中定义的所有可能的样式名称组合,但没有得到任何输出。

渲染视频本身就可以正常使用

print theme('media_youtube_video',$my_media);

你们是怎么做到的?

【问题讨论】:

    标签: php drupal drupal-7


    【解决方案1】:

    在media_youtube 模块代码中挖掘,有一个函数可以在includes/media_youtube.formatters.inc 中为您构建它,称为media_youtube_file_formatter_image_view()。您可以使用如下代码来渲染缩略图:

    // Make sure the correct include file is loaded
    module_load_include('inc', 'media_youtube', '/includes/media_youtube.formatters.inc');
    
    // Load the file
    $file = file_load($my_media['fid']);
    
    // Set up the settings array with your image style
    $display['settings'] = array('image_style' => 'unlinked_thumbnail');
    
    // Get the render array for the thumbnail image
    $image_render_array = media_youtube_file_formatter_image_view($file, $display, LANGUAGE_NONE);
    
    // Render it
    print render($image_render_array);
    

    【讨论】:

    • 你能告诉我在 vide tpl 或其他地方添加这段代码吗?
    【解决方案2】:

    还有一个示例,它可以使用您的自定义图像样式呈现视频缩略图(vimeo、youtube、qbrick 等)。

    $file - 可以从媒体字段轻松获取的文件对象。

    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
    $image_uri = $wrapper->getLocalThumbnailPath();
    
    $image_rendered = theme('image_style', array(
      'style_name' => 'YOUR_IMAGE_STYLE_HERE',
      'path' => $image_uri
    ));
    

    请注意,如果您还想渲染图像,则需要检查$file->type 是video 还是image,因为它将使用不同的包装对象和不同的方法。

    【讨论】:

      【解决方案3】:

      如果你想同时打印拇指+视频,你可以这样做:

      <?php print render($content['field_your_field']); ?>
      

      如果您在 tpl 中并想使用 $variables - 它会是这样的:

      <?php print render($variables['content']['field_url']['#object']->content['field_video']);?>
      

      厚朴你可以用它...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-17
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多