Image Filter Class, used to post process all images, check the documentation of the individual methods.

This class always works on existing files at this time. It supports scripting support by using the process_chain / process_command helper functions, which allow the application to let the user specify a list of filters that should be applied during upload.

todo Image type automatic best-web-format conversion
todo Add support for working on attachments
author Eero af Heurlin, Nemein Oy <eero.afheurlin@nemein.com>
author Nico Kaiser <nico@siriux.net>
author Torben Nehmer <torben@nehmer.net>
version 1.1 (2005-11-03)
package midcom.helper

 Methods

__construct (\midcom_db_attachment $input)

Parameters

$input

__destruct ()

convert (string $format)

This function converts the image to the specified image format.

It must be a suitable extension to use with the Imagemagick convert utility. The file will not be renamed, you have to do this yourself.

This call will always convert the first page only of the supplied image, otherwise, Imagemagick would spam the temp directory in case we are working on a PDF file or whatever.

Parameters

$format

stringThe format to convert to. This must be a valid conversion targed recognized by Imagemagick, it defaults to 'jpg'.

create_tmp_copy (mixed $input)

Creates a working copy to filesystem from given attachment object

Parameters

$input

mixedThe attachment object or filename to copy

Returns

stringtmp file name (or false on failure)

crop (int $x, int $y, $gravity)

Crop an image to given proportions

Parameters

$x

intWidth

$y

intHeight

$gravity

Returns

booleantrue on success.

execute_user_callback (string $command, array $args)

Executes a custom image manipulation callback.

The command name is the function that is searched. The function must use the following signature:

boolean callback (string $infile, $string outfile, array $parameters);

infile is the file to be manipulated, outfile the name where the result has to be stored in (a temporary file usually) and the parameters passed to the command originally. It should return true on a successful transformation, false otherwise.

Parameters

$command

stringThe name of the callback to execute

$args

arrayThe arguments passed to the callback

Returns

\Thereturn code of the callback.

exifrotate ()

Automatic rotation for the image using EXIF tags.

NOTE: This REQUIRES php_read_exif to be present. It will fail if it is missing.

Filter Syntax: exifrotate()

Returns

booleantrue on success.

fill (int $x, int $y, string $color, string $gravity)

Resize image and apply fill to match given size

Parameters

$x

intWidth

$y

intHeight

$color

stringColor

$gravity

stringGravity point

Returns

booleantrue on success.

gamma (float $gamma)

Gamma-Adjustment of the image.

Filter Syntax: gamma($gamma)

Where $gamma is a positive floating point number, e.g. 1.2

Parameters

$gamma

floatGamma adjustment value.

Returns

booleantrue on success.

get_file ()

imagemagick_available ()

Static

process_chain (string $chain)

This function will process a string-based filter chain.

This is a semicolon-separated list of commands, which will be evaluated using process_command each.

Processing stops as soon as one filter command fails.

Parameters

$chain

stringchain The filter chain to be processed (filter1();filter2();...)

Returns

booleantrue, if all filters have been successfully applied, false otherwise.

process_command (string $cmd)

This is the main function evaluator of the filter class.

It will take a function style command like resize(90,90), arguments are separated by commas, there is no quoting at the moment. All arguments are parsed as strings, function names need to be [a-z_]

Execution will relay to the corresponding filter function.

All filters will use defaults for missing arguments (which can result in a null operation) and will ignore excessive arguments.

Parameters

$cmd

stringcmd The command to be executed.

Returns

booleantrue, if the filter executed successfully, false otherwise.

rescale (int $x, int $y)

Image Resize: This will resize the original image.

Filter Syntax: resize ($x, $y)

The width of the image in pixels, it will be scaled in proportion. One of the two parameters can be 0, meaning that the width/height of the image is adjusted to the other, given value.

The resize will be done only if it would not increase the image as this won't gain you any benefit.

Parameters

$x

intWidth

$y

intHeight

Returns

booleantrue on success.

rotate (float $rotate)

Manual image rotation

Filter Syntax: rotate($rotate)

Where $rotate is a positive floating point number greater then 0 and less then 360; if omitted, a null operation is done.

Parameters

$rotate

floatDegrees of rotation clockwise, negative amounts possible

Returns

booleantrue on success.

set_file (string $filename)

Sets the filename of the image currently being edited.

This must be the full path to the file, it will be replaced with the modified image.

The process will check for write permissions at this point, A return value of false will indicate some problem, see the debug log for details.

todo Use ImageMagick Identify to check for a valid image.

Parameters

$filename

stringfilename The file to be edited.

Returns

booleantrue, if the file is valid, false otherwise.

squarethumb (int $x, $gravity)

Square Thumbnails: This will resize&crop the original image.

Filter Syntax: squarethumb ($x)

The filter will adapt picture to given width, point gravity to the centre and crop leftovers.

This is a one parameter shorthand for cropping, provided for backwards compatibility

Parameters

$x

intWidth

$gravity

Returns

booleantrue on success.

write (\midcom_db_attachment $target)

Parameters

$target

_get_tempfile ()

Returns the name of a temporary file to be used to write the transformed image to.

Has to be managed by the callee.

_jpegtran_available ()

_process_tempfile ($tmpname)

This will replace the original file with the processed copy of $tmpfile, deleting the temporary file afterwards.

Parameters

$tmpname

 Properties

 

string $_filename

The file currently being processed.
 

string $_quality

The quality to use for JPEG manipulation, this is not yet settable from the outside.

Stored as a valid imagemagick option, defaults to '-quality 90' right now.