在保存节点上强制生成图像样式


Answers:


12

是的-您可以在自定义模块中实现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);
    }
  }
}

看起来很酷,谢谢!但是您是否知道如何获取与我的节点类型相关的字段收集项的值?
sinini 2013年


我现在注意到,生成的图像没有调整大小,只是以原始大小存储。您有没有遇到过这种情况,您可能对解决方法有任何想法吗?
sinini 2013年


D7的寿命有限,但以防万一有人像我今天一样遇到这种情况,请进行纠正。image_style_create_derivative()需要第一个参数的数组,因此该行应为:image_style_create_derivative(image_style_load($ style_name),$ file-> uri,$ derivative_uri);
Mark

9

带有代码块的两个答案大多数都是正确的,除了它们忽略了一件主要事情:

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);

希望对其他有此问题的人有所帮助。


4

感谢您的帮助,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);
    }
  }
}


0

建议同时使用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']);
  }
}

图像样式生成是从这里获取的

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.