Not sure if this has been discussed other places (I took a quick look and didn't see anything), but I think it would be handy to have a way to use the power of the image module when coding new node types. A few times, I have found myself wishing for an easy way to attach an image node to another new custom node type. For example, when building a form in a new module, it would be really nice to be able to use a function similar to:

$output .= form_image_file($title, $name, $size, $max_image_size = NULL, $description = NULL, $required = FALSE);

I'm not sure how it would work exactly, but when a form containing the above code is submitted (or previewed), calls would be made automatically to the image.module to upload, resize, and handle the details of processing the image, and the node id of the new image would be returned.
Any thoughts?...

Comments

zach harkey’s picture

This exact issue has come up over at flexinode. It looks like what we need is for the image module to provide some api-like functions. Other modules really need to be able to take advantage of the image module instead of having to implement their own mechanism for creating images.

Cvbge wrote:

For example I'd like to have a function for creating new image nodes, for example
image_api_create_image($name_of_uploaded_file, $title, $array_of_preffered_sizes_for_thumbs_and_previes). Such function would take care of checking if upload was successfull, copying file, creating thumbnails and should probably return node id. Next there should be functions that would return url for thumbnail/preview/full size, functions for updating image, deleting it etc.

walkah’s picture

yes robrecthtj and I have had some discussions about this...

the problem is this : a form_* function wouldn't be of any use.. because right now our form functions are just glorified theme functions (i.e. they only handle rendering the form) - thus your image upload function is just a form_file(). the big thing you want here is handling the file *after* upload. perhaps adrian's form api will help us here.

until then...

what do you want from such an API? because you already have things like image_scale and image_resize , that's all part of image.inc. the only real thing that image.module does special (besides some gallery features) is to keep a set of derivatives for each upload. however, if you just want an image file, you may (or may not) want a bunch of derivatives for that file - and you likely want a different set of derivatives, etc. there currently isn't a great way to handle all the settings for that... this is the one place where i think CCK is the answer - i.e. image as field type (rather than node).

moshe weitzman’s picture

i do this already in some modules. as mentioned you have to add code for the form. in addition , you need to call image_validate() in yoiur validate function and image_load in your load.

i found that images will be added with no further code but changes to those images required some code in my hook_update(). album.module uses image.module in this manner.

seanbfuller’s picture

I was playing around with this and posted my results here:

http://drupal.org/node/30944

It's a bit jumbled, but I was trying to walk through the process of adding all the necessary code.

guardian’s picture

Status: Active » Closed (fixed)

Since Drupal 5.0, it is possible to add any field to any node, and "an easy way to add images to other node types" would be the use of the http://drupal.org/project/imagefield module.