I'm not too savvy with CCK fields yet but the attached patch works for my needs so far. I figure there are better ways to do several of these things but wanted to contribute and see if any of it is useful.

1) max number of images. This settings allows a cap on the number of images that can be added to the node. It works by counting both the images already attached to the node and the images in the current session that are not yet part of the node.

2) max image size. Checks the uploaded filesize against the defined max images size in Kb.

3) rewrite filename. I wanted to be assured of unique filenames and also to discard user supplied filenames. Right now the resulting filename is a bit long and ugly in the display area but that can be cleaned up. Uses md5_file function on the uploaded file.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Status: Needs review » Needs work

These all sound like valuable features to me and I'd like to see them in imagefield. Some notes on your patch:

- Use t() to translate string:
Rather than 'The file you uploaded has a filesize greater than the maximum allowed filesize of '.$field['widget']['max_filesize'].'kb' use
t('The file you uploaded has a filesize greater than the maximum allowed filesize of @sizekb', array('@size' => $field['widget']['max_filesize']))

- Avoid duplicate code. Rather than including

        // add all new images saved in the session to the node_field
        if (is_array($_SESSION['imagefield'][$fieldname])) {
          foreach ($_SESSION['imagefield'][$fieldname] as $delta => $file) {
            $node_field[] = $file;
          }
        }

in two places in the same function, use an IF condition to skip the necessary code to get down to where this code already exists.

I put in a separate issue that adds mime-type validation (http://drupal.org/node/132054) which includes a seperate _validate function which you might consider as a cleaner way to accomplish the validation.

quicksketch’s picture

One more small thing, after uploading several images, if I upload a large one over the filesize it causes all images in the SESSION to not be displayed. Attempting to upload another image makes them all come back.

jaydub’s picture

FileSize
4.27 KB

revsied patch taking into account quicksketch's comments. moved the validation routines for max images and max filesize to functions.

quicksketch’s picture

Status: Needs work » Needs review

Oh dear I'm sorry I wasn't quite clear. Make one validation function and include all of the various validates in that one function. In the other patch I'm using _imagefield_widget_upload_validate().

Anyway, I don't think it matters much because both patches are going to need to be re-factored a little bit if they both go in. The rest of the patch looks okay.

@dopry Should this be an imagefield 2.0 feature or make it into the 1.2 release?

jaydub’s picture

FileSize
4.49 KB

better patch that actually works!

@quicksketch - just saw your update about rolling into one validate function. well for now just got this updated patch. I'm open to further fixing but as you said there are a number of other features such as your image type validation that need to be worked into the main project. I just wanted to put out there an attempt to add some of the features that I had seen in the issue queue and that I had needed as well. I'm good with letting the module author(s) take over and use as appropriate if that's the best course of action.

yched’s picture

The 'max number of images' part seems a possible workaround for CCK's current inability to control the number of multiple values in a generic / all-field-types way.
One small remark, though : the error message ("You are only allowed to upload up to @maximages images") is misleading - you'd think this is a site-wide limit,
And I think the convention is to end error messages with a period.

Nitpicking, really...

dopry’s picture

Jaydub,

if you're up to a quick round of revision...

1) reroll for drupal-5--2, I'm not putting any new features into older branches.

2) move validation into _imagefield_widget_upload_validate

3) you can probably refector the max images to be something like count($node_field) + count($_SESSION['imagefield'][$fieldname])... if those aren't arrays in that validate function
something is probably wrong and we should figure out why they're not initialized instead of
testing if they're init'd before acting on them.

4) if you're using node_field, use items instead...

dopry’s picture

Version: 4.7.x-1.x-dev » 5.x-1.x-dev
Status: Needs review » Needs work

oops I forgot about the status update.

jaydub’s picture

I've been laid out by a virus as of late so sorry but no chance to reroll this patch with suggested changes. will keep you posted.

m3avrck’s picture

Subscribing...

dopry’s picture

Version: 5.x-1.x-dev » 5.x-2.x-dev

I'd still love to see this land in imagefield after the 2.0 release. Any chance of re-rolling the patch jaydub?

jaydub’s picture

Status: Needs work » Needs review
FileSize
2.51 KB

See attached patch for max filesize and max number of images. Rolled against the current dev version. This patch only includes 1) and 2) of the original post.

TODO:
- add in text regarding constraints to the form created in _imagefield_widget_form

bobdalob’s picture

Excellent, with one bug if editing a published node.

UPDATE: My mistake - just Excellent! Bug was not related to this patch (it was due to another fix elsewhere that I had applied shortly beforehand and didn't notice)

dopry’s picture

Status: Needs review » Needs work

This patch looks great so far, but the for issue really needs to be addressed before I can commit it. I think we could set #disabled on the upload field and update the description text if items >= max images.

jaydub’s picture

Version: 5.x-2.x-dev » 5.x-2.0-rc2
Status: Needs work » Needs review
FileSize
2.51 KB

If we set #disabled when max images is triggered then would the user not be able to set an existing image to be deleted? If the user decides that with a say 3 image limit that the first image is not as useful as the 4th image then they should be able to delete image 1, submit and be able to upload image 4...

This of course would work a lot better if image delete was also javascript as is upload but I see that the patch for that issue is not ready at this point.

For now I've added the patch reroll for the RC2 release.

dopry’s picture

Status: Needs review » Needs work

I wasn't talking about setting #disabled on any of the image rows for uploaded images... only on the new file upload element. This would force users to select an image to delete before uploading a new one.

The last patch you posted looks like it is reversed.

jaydub’s picture

How to go about modifying the form to disable the file element? The validation function only returns whether the current upload is valid or not and none of the function parameters are references (node, field, or items) so I can't modify a variable that would be available in _imagefield_widget_form.

jaydub’s picture

FileSize
2.74 KB

ok patch re-rolled against HEAD release from Jan 5 with the proper ordering of the files.

jaydub’s picture

Version: 5.x-2.0-rc2 » 5.x-2.0-rc4
FileSize
2.74 KB

re-rolled against 5.x-2.0-rc4

jaydub’s picture

Title: patch to support max number of images, max image size, md5 rewrite of filename » patch to support max number of images, max image size
dopry’s picture

Status: Needs work » Fixed

committed to DRUPAL-5--2 with modifications.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.