滚动
以编程方式使用图像预设(programmatically use imagecache)
与 Drupal 6 相比,Drupal 7 中的 ImageCache API(图像样式)有所变化。在 Drupal 6 中,我们可以通过以下方式以编程方式使用图像预设(preset):
<?php print theme('imagecache', 'my_preset', $file_path, $alt, $title, $attributes); ?>
例如:
theme('imagecache', '300x300crop', $first_photo["filepath"], $node->title, $node->title, array('itemprop' => 'image'));
而在 Drupal 7 中,代码写法略有不同:
print theme_image_style(array( 'style_name' => 'home_category_image', 'path' => $result['node']->field_image['und'][0]['uri'], 'alt' => $title, 'width' => '', 'height' => '', 'attributes' => array('class' => 'search-result-img'), ));
现在我们使用 theme_image_style()
函数,并向其传递一个参数数组:
- style_name — 预设名称。
- path — 图像路径,包括上传方式和文件地址,例如:public://product/421-a1 amt.jpg,其中 public 表示公共文件系统。
- alt — 图像的替代文本(alt 属性)。
- width、height — 宽度和高度。可以不指定,但这可能会触发 notice 提示。
- attributes — 其他属性,通过数组传递。