我正在更新Drupal 7网站以使用图片模块(https://www.drupal.org/project/picture)并且有许多php模板覆盖当前使用图像样式URL来打印图像URL。
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);
谢谢!
这个解决方案适用于图片模块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,
),
),
),
);
您可以看到这是如何在我的全变量变量中应用的。我从代码中删除了大小数组,因为我对每个断点都使用图像样式。
您可以检索断点并调用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。
以下是适用于我的图片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_MAPPING
和IMAGE_URI
。