Drupal 7图片模块PHP输出

时间:2014-11-07 16:52:53

标签: php drupal module drupal-7

我正在更新我的Drupal 7站点以使用图片模块(https://www.drupal.org/project/picture)并且当前使用图像样式URL来打印图像URL的许多php模板覆盖。

print image_style_url($image_style, $img_url);

如何通过PHP打印图片格式的图像?我已经提出了下面的代码并且它很接近,我正在为断点变量发送不正确的数据。

$variables = array(
    'width' => 1570,
    'height' => 950,
    'breakpoints' => array("breakpoints.theme.basic2.large"),
    'uri' => 'public://dev_images/map.jpg',
    'style_name' => 'blog_list_breakpoints_theme_basic2_large_1x');
print theme('picture',$variables);

谢谢!

3 个答案:

答案 0 :(得分:0)

此解决方案适用于图片模块2.x.我自己做了,并提出了以下解决方案。如果每个断点使用一个样式,并且每个样式包含1x和2x,这将是一个示例。

$variables = array(
    'width' => 1570,
    'height' => 950,
    'breakpoints' => array(
             'breakpoints.theme.basic2.large' => array(
               '1x' => array(
                 'mapping_type' => 'image_style',
                 'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'
                ),
               '2x' => array(
                  'mapping_type' => 'image_style',
                  'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'  
               ),
             ),
             'breakpoints.theme.basic2.medium' => array(
               '1x' => array(
                 'mapping_type' => 'image_style',
                 'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'
                ),
               '2x' => array(
                  'mapping_type' => 'image_style',
                  'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'  
               ),
             ),
             'breakpoints.theme.basic2.smalltablet' => array(
               '1x' => array(
                 'mapping_type' => 'image_style',
                 'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'
                ),
               '2x' => array(
                  'mapping_type' => 'image_style',
                  'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'  
               ),
             ),
             'breakpoints.theme.basic2.small' => array(
               '1x' => array(
                 'mapping_type' => 'image_style',
                 'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'
                ),
               '2x' => array(
                  'mapping_type' => 'image_style',
                  'image_style' => 'blog_list_breakpoints_theme_basic2_medium_1x'  
               ),
             ),


       ),
    'uri' => 'public://dev_images/map.jpg',
    'style_name' => 'blog_list_breakpoints_theme_basic2_large_1x'



);
  print theme('picture',$variables);

主题图片功能需要uri,样式名称(用于后备图像样式)和断点数组。图像宽度和高度似乎是可选的。断点数组应该格式化为

array(
 breakpoint_name => array(
   multiplier => array(
     mapping_type => _none|sizes|image_style,
     image_style => image_style_name,
     sizes => '(min-width: 700px) 700px, 100vw',
     sizes_image_styles => array(
       thumbnail => thumbnail,
       medium => medium,
       large => large,
     ),
   ),
 ),


  );

您可以在我的完整变量变量中看到这是如何应用的。我从我的代码中删除了sizes数组,因为我正在为每个断点使用图像样式。

答案 1 :(得分:0)

您可以检索断点并像这样调用theme_picture:

$breakpoints = picture_get_mapping_breakpoints(picture_mapping_load('megamenu_thumbnails'));

$picture = theme_picture(array(
  'uri' => 'public://dev_images/map.jpg',
  'breakpoints' => $breakpoints,
  'style_name' => 'rectangular_210_170_breakpoints_theme_ab_large_1x',
  'timestamp'   => NULL,
));

print $picture;

在此示例中,megamenu_thumbnails是图片映射名称,rectangular_210_170_breakpoints_theme_ab_large_1x是后备图片样式。

这适用于我使用图片版本7.x-2.13。

答案 2 :(得分:0)

这是图片7.x-2.13对我有用的东西:

$breakpoints = picture_get_mapping_breakpoints(picture_mapping_load('PICTURE_MAPPING'));
// Always use theme(), in case a module or theme as overridden the default theme_picture()
$picture = theme('picture', array(
    'uri'                   => 'IMAGE_URI',
    'breakpoints'           => $breakpoints,
    // Optional stuff
    'lazyload'              => true,
    'lazyload_aspect_ratio' => true,
    // You can also include 'style_name' here if you don't want automatic fallback
));

PICTURE_MAPPINGIMAGE_URI替换为相应的字符串。