1

I would like to call a Ruby script when a user uploads an image to my Drupal content-type. I have a CCK image field that serves as the main image, and ImageCache takes care of resizing and creating thumbnails for me.

I have a Ruby script that does some transformations to the image, however I don't exactly know how to call it (no experience with Ruby really). Basically the script applies some image transforms and then generates a new image.

My question is how to call this script from Drupal...is there some sort of hook regarding the CCK image upload that I would hijack?

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
erik
  • 3,810
  • 6
  • 32
  • 63
  • @rogerdpack: good point...that's probably all i need to do to call the ruby script itself...but i'm really not sure where to put this call in my drupal implementation :) – erik Aug 25 '10 at 14:45
  • I've edited your title to clarify your question. The question is more about how to call a script (of any sort) from Drupal, than it is a question about Ruby. – Brian Campbell Aug 25 '10 at 20:23
  • @Brian Campbell: thanks...that's a very good point – erik Aug 26 '10 at 08:26

1 Answers1

1

ImageField uses the same API as FileField. Therefore, you could add a custom validator for your upload field, which would do some checks on the image (like calling a ruby script).

I described that some time ago on Drupal.org, see: http://drupal.org/node/546146

However, for your convenience, here the code.

First, define a validator for the upload form:

function example_form_alter(&$form, $form_state, $form_id) {
  if ($form_id != 'ID_OF_YOUR_FORM')
    return;

  $form['FIELDNAME'][0]['#upload_validators']['example_FUNCTIONNAME'] = array();

  return $form;
}

Second, implement the validator function:

function example_FUNCTIONNAME($field) {
  // variable for error messages
  $errors = array();

  // do some processing on the field
  $filepath = $field->filepath;

  ...

  // in case of error, add error message
  $errors[] = t('Validation failed, because...');

  return $errors;
}

Put this code in a custom module, but make sure that your module is called after FileField and ImageField (adjust weight in system table).

Update: There is another way to hook into it. You can use hook_nodeapi to react on operation "presave". This also allows you to call a script on the content of the uploaded field.

Sebi
  • 8,323
  • 6
  • 48
  • 76
  • @Sebi: could you expand on this presave a little bit? – erik Aug 26 '10 at 13:03
  • 1
    Check hook_nodeapi http://api.drupal.org/api/function/hook_nodeapi/6 I assume that your node containing the cck imagefield is gets saved (basically creating a new or updating an existing node). Here, hook_nodeapi gets called and you can do whatever you want with the uploaded image like forwarding it to an external script. The image is already stored in the upload folder and also added to the systems table. hook_nodeapi is called several times during node save, but you are probably most interested in "presave", because that's after form validation but before the node gets finally saved. – Sebi Aug 26 '10 at 18:24
  • @Sebi: Ok, I like how this method is sounding. Sorry to be a pain...but how would I go about implementing a hook_nodeapi for an upload? Do I add a call to hook_nodeapi in my theme's template.php file? – erik Aug 27 '10 at 09:19
  • No, I would do it in a custom module. It is not really about theming, but more about processing something. – Sebi Aug 31 '10 at 14:20
  • @Sebi: ok, thanks...i think i will ask for a bit more detail in a separate question – erik Sep 03 '10 at 11:13
  • @Sebi: http://stackoverflow.com/questions/3635206/how-to-work-with-hook-nodeapi-after-image-thumbnail-creation-with-imagecache if you are interested :) – erik Sep 03 '10 at 11:18