I saw the "Resize uploaded images to a maximum size" patch at http://drupal.org/node/143828 but I was working on my own solution in the meantime and am attaching here as a much more full-featured alternative to it. The attached patch accomplishes three things:
- Allows admin to select whether to resize the original uploaded image on the Image configuration page.
- Also adds a "Create Web-Friendly Filenames?" option to the Image configuration page allowing admins to decide whether to automatically strip nasty characters from uploaded-image filenames
- Lastly, implements a small bugfix I suggested in http://drupal.org/node/148807.
So it's kind of a triple-threat enhancement/bugfix package. More detail:
Resizing uploaded images
You can check or uncheck this option in Site configuration > Image. In keeping with the form fields for the multiple image sizes ("preview", "thumbnail", etc.) there are two separate boxes to specify max width and max height (so you can specify one but not the other if desired). The checkbox allows you to enable or disable this feature with one click.
I'm guessing some of you reading this might wonder whether you can resize images that have already been uploaded to the server, and the answer is YES. I think patches like this are really deficient if they don't provide a path to implement themselves across existing content. If you go into the Content management > Content and do a mass "Rebuild image thumbnails," it will ALSO rebuild the original images to the specified size. So be sure you've set it to the right size before you do this, because it's irreversible!
By default the checkbox is unchecked to preserve existing behavior.
Technical note: Because it's rolled into the _image_build_derivatives function, each image is resized after the "image_alter" hook is invoked. I thought this was good, so other modules could operate on the image full-size before it is downsampled; if you disagree you could move the code out of the _image_build_derivatives function and plop it up into the image_prepare function before the hook is invoked.
Create Web-Friendly Filenames
I have always been completely baffled why this isn't already built into Drupal by default (or, since it isn't, the image module). Seriously, it's always bothered me that nobody created this already; it's a constant pain in my ass, since the people who use these sites are not usually technically inclined.
Anyway, this patch adds a checkbox for whether to use this. It adds the following function:
function _image_clean_uploaded_filename($filename, $space_replacement='') {
$clean_filename = preg_replace('/[^a-zA-Z0-9\.\$\%\'\`\-\@\{\}\~\!\#\(\)\&\_\^]/','',str_replace(array(' ','%20'),array($space_replacement,$space_replacement),$filename));
return $clean_filename;
}
and, of course, calls it from the _image_filename function. If you want to hack it a little you can have it pass an underscore or hyphen as the $space_replacement parameter instead of having it just remove all spaces.
This option is also unchecked by default to preserve existing behavior unless specified otherwise.
Little bugfix
There's just a little bug in image.module, which I mentioned in another post (http://drupal.org/node/148807), where the IMAGE_ORIGINAL constant wasn't properly used. This fixes it is all. If you've already fixed it on your own, this part of the patch will probably just be discarded so no worries.
So, anyway
I'm kinda hoping I'll get some comments on this as to whether it works for you. I'd really like to see the module maintainers roll this into the module itself as soon as they can. Any chance? Regardless, I hope this helps some of you! I'm not very experienced at making actual patch files, so please forgive me if for any reason it doesn't apply correctly; it seems to apply correctly for me.
| Comment | File | Size | Author |
|---|---|---|---|
| image.module.resizeoriginal.patch | 54.17 KB | Vallenwood |
Comments
Comment #1
drewish commentedI really like what you've done here but it's too many changes in one patch. Post a patch for the IMAGE_ORIGINAL on that issue, and split the Web-Friendly Filenames into it's own issue and this functionality will be committed a whole lot sooner.
Comment #2
drewish commentedHumm, just looked at the patch. It looks like you've got some line ending issues that have caused the patch to delete the entire file and add your updated file. Make sure that your patch is generated using the same line endings as the original file.
Comment #3
Vallenwood commentedk, I'll take a look. I made everything UNIX line endings since I thought that was probably the convention. As difficult as it is to get a good patch-maker for Windows, I was starting to think I was the only sucker using it to code Drupal patches on.
Comment #4
drewish commentedhehe, i do my development on windows and freebsd, the line endings are by far the biggest problem on windows. grab TortoiseCVS and use it to checkout stuff from CVS and generate patches.
Comment #5
2houseplague commentedThese are welcome additions to the Image Module.
Comment #6
esadot commentedI second. Would love to see these enhancements hit the module in the near future.
Comment #7
drewish commentedwell, i'd love to see a usable patch ;)
Comment #8
drewish commentedsince the other features have been committed i guess this is what's left of the original issue.
Comment #9
drewish commentedsince this has a patch i'm marking http://drupal.org/node/135438 as a duplicate of the file naming part of this.
Comment #10
sunThis is a Drupal core issue. You definitely should test Transliterate filenames module. Please report back if that module works for you (and mark this issue fixed if it does).
Comment #11
sunUse http://drupal.org/project/transliteration to solve this issue.