在Drupal中调用图像上传脚本 [英] Calling script on image upload in Drupal
问题描述
当用户将图像上传到我的Drupal内容类型时,我想调用Ruby脚本。我有一个CCK图像字段作为主图像,ImageCache负责调整大小并为我创建缩略图。
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.
我有一个Ruby脚本可以进行一些转换图像,但我不知道如何调用它(没有真正的Ruby经验)。基本上脚本应用了一些图像变换,然后生成一个新图像。
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.
我的问题是如何从Drupal调用这个脚本...是否有某种关于CCK图片上传,我会劫持?
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?
推荐答案
ImageField使用与FileField相同的API。因此,您可以为上载字段添加自定义验证器,这将对映像进行一些检查(如调用ruby脚本)。
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).
我前段时间在Drupal.org上描述过: http://drupal.org/node/546146
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;
}
将此代码放入自定义模块中,但要确保调用模块在FileField和ImageField之后(在系统表中调整权重)。
Put this code in a custom module, but make sure that your module is called after FileField and ImageField (adjust weight in system table).
更新:还有另一种方法可以挂钩。您可以使用hook_nodeapi对操作presave做出反应。这也允许您在上传字段的内容上调用脚本。
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.
这篇关于在Drupal中调用图像上传脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!