Summary: Automatically resize uploaded images using ImageMagick
Prerequisites: PmWiki 2.2.0-beta series, untested on 2.1.27 and earlier. Also requires ImageMagick
Questions answered by this recipe
- How can I generate smaller / thumbnail images from files uploaded to the wiki?
AutoThumber allows you to process uploaded images using ImageMagick. Primarily it's meant to allow you to automatically scale images to a maximum resolution and to generate a thumbnail image, but it also provides a way to give any commands that you wish to ImageMagick's convert command.
To install this recipe
- install ImageMagick and make sure its commands are accessible from your wiki folder
- download autothumber.phpΔ to your cookbook directory
- add the following line to your configuration file:
Upload an image file, say
image.png. If all is well and you haven't overwritten the defaults, you'll find the actual file you uploaded at
image.png is a version that's scaled to at most 300x600 pixels, and
image.small.png is a version scaled down to fit inside 100x100 pixels.
$AutoThumberSet may be set in your config.php file to configure the way AutoThumber works.
Each entry in the array defines an image, and needs to have
'ext' set to something and at least one of
'opt' set to a non-zero value (except for the
'default' entry). The name of the original uploaded image is determined by
$AutoThumberSet['default']['ext']. If a value isn't set, the
'default' value is used.
'default' => array( 'ext'=>'.orig', 'opt'=>'', 'resize-opt'=>'>', 'maxsize'=>0.9 ),
'big' => array( 'ext'=>'', 'w'=>300, 'h'=>600 ),
'small' => array( 'ext'=>'.small', 'w'=>100, 'h'=>100 )
'ext' - What to add at the end of the file's name but before its extension, eg.
'h' - Limits for resizing; set to
0 or an empty string to not limit image size in that direction.
'default' values are ignored.
'resize-opt' - The kind of resize operation to use;
'>' means that an image won't be enlarged but will be scaled to fit inside a box defined by the given limits. See the ImageMagick documentation for more.
'opt' - Any string of commands that you wish to pass to
'maxsize' - A fraction of the original filesize; if the size on disk of the resized file is greater than this and the pixel size of both is the same, the original is copied over the "resized" version. Set to
0 or an empty string to disable.
The order of these fields doesn't matter. If you wish to change any of the settings, you'll need to declare the complete array.
By default, AutoThumber only processes images (as defined by $ImgExtPattern, by default includes GIF, JPEG and PNG). Since ImageMagick can read a much wider variety of files and it could be used to generate thumbnails for eg. AVI files.
It should be possible to extend this code base to work with PHP's GD library or other utilities. However, I wrote this primarily for my own needs, and hence haven't a good reason (at least yet) to extend the code.
Uploading a file that includes the same string as an
'ext' field value isn't a problem. For example for
'small' version is
- 2008-04-05 - first release
- Cookbook.ImagesAutoResizing Creates a small image on the page, with a link to a larger image
- Cookbook.ThumbList A thumbnail picture gallery for PmWiki
- Cookbook.Attachtable Actions to rename, delete & restore deleted attachments, as well as an attachlist replacement to use those actions, show file types and list attachment references.
- YAG -- "Yet Another Gallery tool" - Create thumbnail gallery of large number of images distributed over multiple pages.
See discussion at AutoThumber-Talk
User notes +1: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.