How do I automatically create the image styles (imagecache) when I import a feed in Drupal?How do I automatically create the image styles (imagecache) when I import a feed in Drupal?

So you have setup your feed importers using the excellent Drupal Feeds module, figured out how to upload multiple images in a field using the Fields Tamper module, scheduled the imports, perhaps even got round the annoying Drupal core issue of only keeping temporary files for 6 hours thus stalling your large feeds...

...but what about the images it's importing? Having to regenerate the images styles causes excessive waiting for visitors. But help is at hand with the Rules Image Styles module.

Step 1.

Ensure you have the Rules, Rules UI and Rules image styles modules installed and activated.

Step 2.

Head to the rules UI (Configuration -> Workflow -> Rules).

Step 3.

Add a new rule, calling it what you wish, e.g. 'Build image styles on node add/update'.

Step 4.

Add events, for example 'Node -> After saving new content' and 'Node -> After updating existing content'

Step 5.

Add an action for 'Pre generate image styles'. You can select here which image styles are created upon this rule being invoked. Be aware it will create these styles for ALL images so can create a file overhead if you are only wanting the first of each uploaded. You will want to set the file schema appropriately (in most cases this will be 'public'.

Step 6.

Save and then try creating/updating a node. Did it create the images? Check in the directory sites/default/files/styles/-your-style-name-/public to see. If it did, great, you're good to go, if not, read on...

Optional Step 7.

The module is in it's infacy and as such, it's setup pretty basic. Depending on how your image field is setup, you may need to alter the code. Have a look at the database table 'field_config_instance' for the particular node/image combo you are working with. Then examine the data blob to see what module your image field is using. For example, it may, instead of the default 'image' field be using the 'multiupload_imagefield_widget'.

You will then need to edit the file rules_image_styles/rules_image_styles.rules.inc.

Look for the rules_image_styles_action_get_node_image_fields function and alter accordingly, for example, below I have added the appropriate case for the multi_upload_imagefield_widget :-

function rules_image_styles_action_get_node_image_fields($node) 
{
  $image_fields = array();

  $node_fields = field_info_instances('node', $node->type);

  foreach ($node_fields as $name => $field_data) {
    //we check the widget here, is there a better solution? May this cause problems?
    if (($field_data['widget']['module'] == 'image') ||
        ($field_data['widget']['module'] == 'multiupload_imagefield_widget')){
      $image_fields[$name] = $field_data;
    }
  }
  return $image_fields;
}

Once saved and loaded, rerun the test.

Optional Step 8.

Still not creating them? Okay, the final step is that sometimes, for whatever reason, the Feeds module seems not to add the default 'styles' subdirectory, a quick code change will solve that. In the function rules_image_styles_action_generate just before the call to xx I added :-

if(strpos($destination, 'ublic://styles/') > 0)
{

}else{
            $destination = str_replace('public://', 'public://styles/', $destination);
}

$result = image_style_create_derivative($data, $filedata['uri'], $destination);   

You may also be able to accomplish this through the use of the subfolders field within the action of the rule.

And there we go, no more waiting times for image generation on feed imports!

Comments

Post new comment