views:

39

answers:

1

Hi all,

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?

+1  A: 

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
@Sebi: could you expand on this presave a little bit?
espais
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
@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?
espais
No, I would do it in a custom module. It is not really about theming, but more about processing something.
Sebi
@Sebi: ok, thanks...i think i will ask for a bit more detail in a separate question
espais
@Sebi: http://stackoverflow.com/questions/3635206/how-to-work-with-hook-nodeapi-after-image-thumbnail-creation-with-imagecache if you are interested :)
espais