Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

AutoThumber

Summary: Automatically resize uploaded images using ImageMagick
Version: 2008-04-05
Prerequisites: PmWiki 2.2.0-beta series, untested on 2.1.27 and earlier. Also requires ImageMagick.
Status: beta
Maintainer: EemeliAro

Questions answered by this recipe

  • How can I generate smaller / thumbnail images from files uploaded to the wiki?

Description

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 yout wiki folder
  • download autothumber.phpΔ to your cookbook directory
  • add the following line to your configuration file:
include_once("$FarmD/cookbook/autothumber.php");

Usage

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.orig.png; 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.

Configuration

The variable $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 'w', 'h' or '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(
	'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. '.small' turns image.png to image.small.png
  • 'w' and '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 convert.
  • '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.

Notes

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 image.small.png the 'small' version is image.small.small.png.

Release Notes

  • 2008-04-05 - first release

See Also

  • Cookbook.ImagesAutoResizing Creates a small image on the page, with a link to a larger image
  • Cookbook.ThumbList A feature-rich thumbnail picture gallery for PmWiki
  • Cookbook.Attachtable Actions to rename, delete & restore deleted attachments, as well as an attachlist replacement to use those actions and show file MIME types.

Contributors

Comments

2008-07-06 - Great Recipe ... when I tried to declare a maximum w or h for the default (original), it would not resize. That would be a handy feature as many images that will be uploaded for my application will come directly from digital cameras, and don't need a version larger than (i.e.) 1280 pixels wide on the server. Any suggestions? RussJackman

2008-09-05 - It tryed to use the recipe but when I tryed to upload an image the following error was shown:

PmWiki can't process your request

?no such file

We are sorry for any inconvenience.

When I disable resizing it "works", which means the upload skript doesn't produce this error:

$AutoThumberSet = array(
	'default' => array(),
);

But When I try to use the resize feature it doesn't work anymore.

Edit - History - Print - Recent Changes - Search
Page last modified on September 04, 2008, at 05:30 PM