0

In Drupal 7 I wish to create several images defined by image styles when saving a node. There is only one image field, and it is just several styles of that image that I need to generate.

None of these images are displayed when viewing the node, they will be accessed through XML and Services.

I think mabye the hook_preprocess_HOOK and theme_image_style can be used as pointed out in this question: Multiple image styles. Is this correct? And if so, can anyone point me to some examples where this is done? In the link above, images are echo'ed out, but is this how it should be done when saving a node?

  • The problem here is that to build the image one has to request the URL of the image. But this shouldn't be done when saving a node, should it? Am I completely off track here? – pusle Aug 16 '11 at 11:38
1

I ended up creating a separate content type and a theme template file that solved this specific problem I had. I will update this if I ever find a way to do this in a module or other.

EDIT:

I ended up creating a module that solves this.

in hook_menu:

function mymodule_menu () {
  $items['node/%/generateimages'] = array (
    'title' => 'Generate images',
    'page callback' => 'mymodule_generate_images',
    'page arguments' => array(0, 1),
    'access callback' => TRUE,
  );
}

In the callback function:

function mymodule_generate_images ($arg1, $arg2) {
  $retval = '';
  $path   = drupal_lookup_path ("source", $arg1 . '/' . $arg2);
  $node   = menu_get_object ("node", 1, $path);

  drupal_set_title ('Generate images ' . $node->title);

  global $language;
  $lang = $language->language;
  $files = field_get_items ('node', $node, 'field_image');

  foreach ($files as $key => $media) : 
    $url   = image_style_url ('related_thumbnail', $media['uri']);
    $thumb = theme ('image_style', array('style_name' => 'related_thumbnail', 'path' => $media['uri']));
    $retval .= $thumb;
  endforeach;

  return $retval;
}

After node creation, I manually have to go to thenodeurl/generateimages and images defined in the callback function is created.

I wish someone could tell me a better way to solve this :)

| improve this answer | |
0

I had the same need so that a certain image style was available for an email digest.

I got the answer here https://drupal.stackexchange.com/a/54584/38200 making sure to add the image_style_load call as explained here https://drupal.stackexchange.com/a/82089/38200 to make sure the correct image style is appied.

| improve this answer | |
  • Welcome to Drupal Answers! Whilst these links may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – Mołot Oct 16 '14 at 14:29

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.