Thumbnailer documentation

Class Thumbnailer

Thumbnailer

public class Thumbnailer

Thumbnailer premium class.

Copyright (C) 2009/2011 by Michal Zukowski Permission is hereby granted, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction excluding the rights to copy, modify, merge, publish or distribute and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
1.3.5
Author:
Michal Zukowski
Copyright:
Michal Zukowski

Constant Summary
final static int

ALL

Constant used in round() method.

final static int

BOTTOM_LEFT

Constant used in round(), logoText() and logoPhoto() methods.

final static int

BOTTOM_RIGHT

Constant used in round(), logoText() and logoPhoto() methods.

final static int

CENTER

Constant used in logoText() and logoPhoto() methods.

final static int

TOP_LEFT

Constant used in round(), logoText() and logoPhoto() methods.

final static int

TOP_RIGHT

Constant used in round(), logoText() and logoPhoto() methods.

Field Summary
static array

$allowed

List of allowed mime types.

string

$file

Full path to the source image.

string

$filename

Image file name.

int

$logoAlign

Align setting for text/image watermarks.

string

$mime

Image mime type.

callback

$openfunc

Function used to open the image.

resource

$resource

Image resource.

resource

$thumb

Thumb resource.

int

$thumb_height

Thumb height

int

$thumb_width

Thumb width

Method Summary
void

__construct(string file)

Creates Thumbnailer object using prepare()

static array

batch(function callback, string in)

Batch mode helper.

static array

colorHex(string hex)

Hex color helper.

static array

colorRGB(int r, int g, int b)

Color helper.

static Thumbnailer

create(string file)

Thumbnailer creator.

protected resource

createImage(int width, int height)

Creates internal php image (preserves png/gif transparency).

Thumbnailer

effectCustom(array matrix, float offset)

Apply custom effect on the image.

Thumbnailer

effectGray()

Grayscale helper.

resource

getResource()

Resource getter.

Thumbnailer

header()

Helper sends headers to display thumb directly into the browser.

void

im(resource dim, resource sim, int dx, int dy, int sx, int sy, int dw, int dh, int sw, int sh, int q)

Resample image more quickly.

Thumbnailer

logoAlign(int align)

Set text align for logoText() or logoPhoto() watermark.

Thumbnailer

logoPhoto(string photo, int x, int y, int alpha)

Puts image watermark on the thumb at $x and $y position from the corner set by logoAlign().

Thumbnailer

logoText(string text, string font, int size, int x, int y, array color)

Puts text based watermark on the thumb at $x and $y position from the corner set by logoAlign().

void

prepare(string file)

Prepares the image to be opened.

string

randomName(int length)

Helper generates random name for the thumbnail.

array

readExif(string sections)

Retrieve EXIF data from the image.

Thumbnailer

round(int corner_radius, array color, int corners)

Round thumb corners using antialiasing.

mixed

save(string file, int quality)

Saves created the result.

static void

setAllowed(array allowed)

Sets allowed mime types.

Thumbnailer

thumbFixed(int width, int height)

Create a fixed size thumbnail.

Thumbnailer

thumbSquare(int size)

Create a square thumbnail.

Thumbnailer

thumbSymmetric(int width, int height)

Creates scaled thumbnail of maximum width $width and maximum height $height.

Thumbnailer

thumbSymmetricHeight(int height)

Create a thumbnail with fixed height and scaled width.

Thumbnailer

thumbSymmetricWidth(int width)

Create a thumbnail with fixed width and scaled height.

static mixed

upload(string fieldName, callback callback)

Upload helper.

Constant Detail

Thumbnailer.premium.php at line 61

ALL

public final static int ALL = 15

Constant used in round() method.


Thumbnailer.premium.php at line 51

BOTTOM_LEFT

public final static int BOTTOM_LEFT = 4

Constant used in round(), logoText() and logoPhoto() methods.


Thumbnailer.premium.php at line 56

BOTTOM_RIGHT

public final static int BOTTOM_RIGHT = 8

Constant used in round(), logoText() and logoPhoto() methods.


Thumbnailer.premium.php at line 66

CENTER

public final static int CENTER = 0

Constant used in logoText() and logoPhoto() methods.


Thumbnailer.premium.php at line 41

TOP_LEFT

public final static int TOP_LEFT = 1

Constant used in round(), logoText() and logoPhoto() methods.


Thumbnailer.premium.php at line 46

TOP_RIGHT

public final static int TOP_RIGHT = 2

Constant used in round(), logoText() and logoPhoto() methods.


Field Detail

Thumbnailer.premium.php at line 75

allowed

public static array $allowed = array(...)

List of allowed mime types. By default only allowed mime types are JPEG, GIF and PNG.

Since:
1.2.5

Thumbnailer.premium.php at line 83

file

public string $file

Full path to the source image.

Since:
1.0.0

Thumbnailer.premium.php at line 91

filename

public string $filename

Image file name.

Since:
1.3.1

Thumbnailer.premium.php at line 147

logoAlign

public int $logoAlign = self::BOTTOM_LEFT

Align setting for text/image watermarks.

Since:
1.3.3

Thumbnailer.premium.php at line 139

mime

public string $mime

Image mime type.

Since:
1.3.1

Thumbnailer.premium.php at line 107

openfunc

public callback $openfunc

Function used to open the image.

Since:
1.0.0

Thumbnailer.premium.php at line 99

resource

public resource $resource = null

Image resource. Opened image source in prepare() will be held here.

Since:
1.0.0

Thumbnailer.premium.php at line 115

thumb

public resource $thumb

Thumb resource.

Since:
1.0.0

Thumbnailer.premium.php at line 131

thumb_height

public int $thumb_height

Thumb height

Since:
1.0.0

Thumbnailer.premium.php at line 123

thumb_width

public int $thumb_width

Thumb width

Since:
1.0.0

Method Detail

Thumbnailer.premium.php at line 173

__construct

public void __construct(string file)

Creates Thumbnailer object using prepare()

Parameters:
file - Image path
Since:
1.0.0

Thumbnailer.premium.php at line 924

batch

public static array batch(function callback, string in)

Batch mode helper. For more information about the input mask please refer to php's glob function manual.

Example usage:

function callb(Thumbnailer $th) {
$filename=$th->randomName();
$th->thumbFixed(120,90)->round()->save('/save/path'.$filename);
return $filename;
}

Thumbnailer::batch('callb', '/read/path/*.jpg');

Parameters:
in - Read from directory.
callback - Callback function
Throws:
ThumbnailerException
Returns:
User output
Magic:
premium
Since:
1.2.1

Thumbnailer.premium.php at line 545

colorHex

public static array colorHex(string hex)

Hex color helper. For example black is Thumbnailer::colorHex('#000000')

Parameters:
hex - Color in hex value
See Also:
colorRGB()
Since:
1.1.1
Magic:
premium

Thumbnailer.premium.php at line 529

colorRGB

public static array colorRGB(int r, int g, int b)

Color helper. For example black is Thumbnailer::colorRGB(0,0,0)

Parameters:
r - Red color 0-255
g - Green color 0-255
b - Blue color 0-255
Since:
1.1.1
Magic:
premium

Thumbnailer.premium.php at line 161

create

public static Thumbnailer create(string file)

Thumbnailer creator. Creates thumbnailer object for inline scripting, i.e.:

Thumbnailer::create('photo.jpg')->thumFixed(120,90)->header()->save();

Parameters:
file - File path or url
Since:
1.3.3

Thumbnailer.premium.php at line 613

createImage

protected resource createImage(int width, int height)

Creates internal php image (preserves png/gif transparency).

Parameters:
width - Image width
height - Image height
Since:
1.3.0

Thumbnailer.premium.php at line 730

effectCustom

public Thumbnailer effectCustom(array matrix, float offset)

Apply custom effect on the image.

Example usage:

// this code will apply gaussian blur on the thumb
$thumbObject
->thumbSquare(200,200)
->effectCustom(array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0)))
->save('output_with_blur.jpg');

Magic:
premium
Throws:
ThumbnailerException
See Also:
http://www.php.net/imageconvolution
Parameters:
matrix - A 3x3 matrix: an array of three arrays of three floats
offset - Color offset
Since:
1.3.5

Thumbnailer.premium.php at line 688

effectGray

public Thumbnailer effectGray()

Grayscale helper. Turns image grayscale. This method can be chained.

Throws:
ThumbnailerException
Since:
1.2.5
Magic:
premium

Thumbnailer.premium.php at line 599

getResource

public resource getResource()

Resource getter.

Since:
1.3.3

Thumbnailer.premium.php at line 749

header

public Thumbnailer header()

Helper sends headers to display thumb directly into the browser. This method can be chained.

Since:
1.3.2
Magic:
premium

Thumbnailer.premium.php at line 647

im

public void im(resource dim, resource sim, int dx, int dy, int sx, int sy, int dw, int dh, int sw, int sh, int q)

Resample image more quickly.

See Also:
imagecopyresampled()
Since:
1.0.0

Thumbnailer.premium.php at line 793

logoAlign

public Thumbnailer logoAlign(int align)

Set text align for logoText() or logoPhoto() watermark. Correct values are Thumbnailer::TOP_LEFT, Thumbnailer::TOP_RIGHT, Thumbnailer::BOTTOM_LEFT, Thumbnailer::BOTTOM_RIGHT and Thumbnailer::CENTER. This method can be chained.

Magic:
premium
Since:
1.3.3

Thumbnailer.premium.php at line 868

logoPhoto

public Thumbnailer logoPhoto(string photo, int x, int y, int alpha)

Puts image watermark on the thumb at $x and $y position from the corner set by logoAlign(). Default corner is bottom left. Alpha should be between 0 and 100. Use it only with PNG images though. This method can be chained.

Throws:
ThumbnailerException
Since:
1.2.4
Magic:
premium

Thumbnailer.premium.php at line 817

logoText

public Thumbnailer logoText(string text, string font, int size, int x, int y, array color)

Puts text based watermark on the thumb at $x and $y position from the corner set by logoAlign(). Default corner is bottom left. $color should be an array of (R,G,B) or use colorHex(), colorRGB() helpers. $font - path to the TrueType font This method can be chained.

Throws:
ThumbnailerException
Since:
1.2.4
Magic:
premium

Thumbnailer.premium.php at line 207

prepare

public void prepare(string file)

Prepares the image to be opened.

Method checks if file actually exists. If not it checks if file is remotely based. If it is, Thumbnailer creates a temporary file and tries to download the file. When image is ready it tries to get its mime type to set save function and open function to be able to work and save the results. In the end Thumbnailer opens the image and is ready to operate.

Parameters:
file - Path
Since:
1.3.0
Throws:
ThumbnailerException

Thumbnailer.premium.php at line 775

randomName

public string randomName(int length)

Helper generates random name for the thumbnail.

Example usage:

$th=Thumbnailer::create('image.jpg');

$fileName=$th->randomName(); $th->round()->save($fileName); echo 'File was saved as '.$filename;

Since:
1.3.2
Magic:
premium
Parameters:
length - Name length
Returns:
Random name

Thumbnailer.premium.php at line 998

readExif

public array readExif(string sections)

Retrieve EXIF data from the image.

Example usage:

print_r($thumbnailerObject->readExif());

Magic:
premium
Throws:
ThumbnailerException
See Also:
http://www.php.net/exif_read_data
Parameters:
sections - Comma separated list of sections that need to be present in file to produce a result array
Since:
1.3.5

Thumbnailer.premium.php at line 478

round

public Thumbnailer round(int corner_radius, array color, int corners)

Round thumb corners using antialiasing.

Parameters:
corner_radius - Corner radius
color - RGB color of background
corners - Corners to round
Since:
1.1.0
Magic:
premium
Throws:
ThumbnailerException

Thumbnailer.premium.php at line 561

save

public mixed save(string file, int quality)

Saves created the result. If $file is not specified file will be flushed into the browser. If so use header() helper to send proper headers. Quality setting is used only for JPEG images. PNG ignores this completely.

Parameters:
file - Destination file for the thumbnail
quality - Quality of JPEG image (default 85)
Returns:
False on error, filename on success
Since:
1.0.0

Thumbnailer.premium.php at line 668

setAllowed

public static void setAllowed(array allowed)

Sets allowed mime types.

Parameters:
allowed - Allowed mime types
Since:
1.0.0

Thumbnailer.premium.php at line 431

thumbFixed

public Thumbnailer thumbFixed(int width, int height)

Create a fixed size thumbnail.

Parameters:
width - Thumb width
height - Thumb height
Since:
1.2.0

Thumbnailer.premium.php at line 381

thumbSquare

public Thumbnailer thumbSquare(int size)

Create a square thumbnail.

Parameters:
size - Width and height size
Since:
1.0.0

Thumbnailer.premium.php at line 286

thumbSymmetric

public Thumbnailer thumbSymmetric(int width, int height)

Creates scaled thumbnail of maximum width $width and maximum height $height.

Parameters:
width - Maximum width
height - Maximum height
Since:
1.0.0
Throws:
ThumbnailerException

Thumbnailer.premium.php at line 357

thumbSymmetricHeight

public Thumbnailer thumbSymmetricHeight(int height)

Create a thumbnail with fixed height and scaled width.

Since:
1.2.6

Thumbnailer.premium.php at line 332

thumbSymmetricWidth

public Thumbnailer thumbSymmetricWidth(int width)

Create a thumbnail with fixed width and scaled height.

Since:
1.2.6

Thumbnailer.premium.php at line 959

upload

public static mixed upload(string fieldName, callback callback)

Upload helper. Example usage:

function callb(Thumbnailer $th) {
return $th->round()->save('/save/path/'.$th->randonName());
}

echo Thumbnailer::upload('file', 'callb');

Parameters:
fieldName - Field name in $_FILES superglobal.
callback - Callback function name
Returns:
Output
Throws:
ThumbnailerException
Magic:
premium