我仍然在这里描述我的问题图像样式,在字段收集项中重复使用图像,但是我放弃了以获得解决方案。
我想到的一种解决方法是强制在nodesave上生成图像样式。有没有可能这样做?
我仍然在这里描述我的问题图像样式,在字段收集项中重复使用图像,但是我放弃了以获得解决方案。
我想到的一种解决方法是强制在nodesave上生成图像样式。有没有可能这样做?
Answers:
是的-您可以在自定义模块中实现hook_node_insert()
并hook_node_update()
使用图像API函数在其中创建图像。例如
function MYMODULE_node_insert($node) {
// Get some field items from a field called 'field_photo.
if ($image_items = field_get_items('node', $node, 'field_photo')) {
$image_item = array_shift($image_items);
// Load the associated file.
$file = file_load($image_item['fid']);
// An array of image styles to create.
$image_styles = array('style_1', 'style_2');
foreach ($image_styles as $style_name) {
// Get the location of the new image.
$derivative_uri = image_style_path($style_name, $file->uri);
// Create the image.
image_style_create_derivative($style_name, $file->uri, $derivative_uri);
}
}
}
带有代码块的两个答案大多数都是正确的,除了它们忽略了一件主要事情:
image_style_create_derivative的第一个参数应为图像样式数组。
他们传递的只是样式的名称。在foreach中,如果您添加:
$style = image_style_load($style_name);
然后在image_style_create_derivative函数中将$ style_name更改为$ style,它将按预期工作并生成样式化的图像。
image_style_create_derivative($style, $file->uri, $derivative_uri);
希望对其他有此问题的人有所帮助。
感谢您的帮助,Clive是我对字段收集项的全部功能:(另一篇有用的帖子:访问字段收集)
function channelportal_gallery_node_update($node) {
//get all the id's from the field collection values
$galleryimages = field_get_items('node', $node, 'field_gallery_and_caption');
$fieldcollectionids = array();
foreach ($galleryimages as $key => $value) {
$fieldcollectionids[] = $value['value'];
}
// Load up the field collection items
$items = field_collection_item_load_multiple($fieldcollectionids);
foreach ($items as $item) {
$image_item = field_get_items('field_collection_item', $item, 'field_gallery_image');
// Load the associated file.
$file = file_load($image_item[0]['fid']);
// An array of image styles to create.
$image_styles = array('gallery_big', 'gallery_thumbnail');
foreach ($image_styles as $style_name) {
// Get the location of the new image.
$derivative_uri = image_style_path($style_name, $file->uri);
// Create the image.
image_style_create_derivative($style_name, $file->uri, $derivative_uri);
}
}
}
似乎有一个针对该问题的模块:https : //www.drupal.org/project/imageinfo_cache
也可以查看页面上的“相关模块”部分。
建议同时使用hook_node_insert()和hook_node_update()并检查是否未生成所需的图像派生类,然后生成它们,否则,什么也不做。
/**
* Implements hook_node_insert to generate derivative images for the new inserted node in
* case they are not generated
* @param object $node
*/
function YOUR_MODULE_node_insert($node) {
//REPLACE field_YOUR_IMAGE_FIELD WITH YOUR FIELD IMAGE NAME
if(isset($node->field_YOUR_IMAGE_FIELD['und'][0]['uri'])) {
_generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][0]['uri']);
}
}
/**
* Implements hook_node_update to generate derivative images for the new updated node in
* case they are not generated
* @param object $node
*/
function YOUR_MODULE_node_update($node) {
//REPLACE field_YOUR_IMAGE_FIELD WITH YOUR FIELD IMAGE NAME
if(isset($node->field_YOUR_IMAGE_FIELD['und'][0]['uri'])) {
_generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][0]['uri']);
}
}
/**
* Generates the needed image styles by the image uri if they are not already generated
* @param string $image_uri
*/
function _generate_image_style($image_uri) {
//This should be changed to your image styles names.
$image_styles = array('image_style_name1', 'large_image', 'promo_image');
foreach ($image_styles as $style) {
$derivative_uri = image_style_path($style, $image_uri);
file_exists($derivative_uri) || image_style_create_derivative(image_style_load($style), $image_uri, $derivative_uri);
}
}
注意:如果您的图片字段拍摄多张图片,则应像这样遍历它们:
if(isset($node->field_main_image['und']) && is_array($node->field_main_image['und'])) {
foreach($node->field_main_image['und'] as $delta => $image_field) {
_generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][$delta]['uri']);
}
}
图像样式生成是从这里获取的