9

I've got still my problem described here Image style, reuse image in field collection item but i gave up to get a solution.

A workaround which came up in my mind is to force the generation of imagestyles on nodesave. are there any possibilites to do so?

12

Yes - you can implement hook_node_insert() and hook_node_update() in a custom module and create the images there using image API functions. For example

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

The two answers with blocks of code are mostly correct except they are overlooking one major thing:

The first argument of image_style_create_derivative is expected to be an image style array.

What they're passing is just the name of the style. In the foreach if you add:

$style = image_style_load($style_name);

then change $style_name to $style in the image_style_create_derivative function it should work as expected and generate the styled image.

image_style_create_derivative($style, $file->uri, $derivative_uri);

Hope that helps anyone else having this issue.

4

Thanks for your help Clive, my whole function for the field collection items: (another helpful post from you: Accessing a field collection)

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

There seems to be a module for that problem: https://www.drupal.org/project/imageinfo_cache

Have a look at the "Related Modules"-Section on the page, too.

0

It's recommended to use both hook_node_insert() and hook_node_update() and to check if the needed image derivatives are not generated, then generate them, otherwise, do nothing.

/**
 * 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);
  }
}

Note: If your image field take multiple images then you should loop through them like that:

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']);
  }
}

The image styles generation is taken from here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.