ImageUtil.php

Show: inherited
Table of Contents

Easy resize, filter and compression class for images.

All image manipulations are applied to a temporary image. Only the save() method is permanent.

Requires: PHP5 and GD2 library.

Optional: jpegtran, pngcrush.

Usage:

include 'ImageUtil.php';

// Render image without saving example:

// an absolute URL or path to file. Can be .jpg, .jpeg, .gif or .png
$img = new ImageUtil('images/large/input1.jpg');
// set width and height and then render image. Can be .jpg, .jpeg, .gif or .png
$img->resize(320, 240)->render('output.jpg');

// Save image example:

// an absolute URL or path to file. Can be .jpg, .jpeg, .gif or .png
$img = new ImageUtil('images/large/input2.jpg');
// set width, height and image resize option
$img->resize(150, 100, 'crop')
       // add watermark
       ->watermark('images/large/watermark.png')
       // save path to file. Can be .jpg, .jpeg, .gif or .png
       ->save('images/small/output.jpg');
Author
Jani Mikkonen  
License
public domain  
Link
https://github.com/janisto/ImageUtil/  
Package
ImageUtil  

\ImageUtil

Package: ImageUtil

Main class for ImageUtil

Author
Jani Mikkonen  
License
public domain  
Link
https://github.com/janisto/ImageUtil/  
\Exception invalid image

Methods

methodpublic__construct( string $file, array $options = array() ) : void

Constructor.

Parameters
Name Type Description
$file string

an absolute URL or path to file

$options array

an array of options

Throws
Exception Description
\Exception invalid image
methodpublicblur( string $type = 'gaussian' ) : object

Set blur.

This method is chainable.

Parameters
Name Type Description
$type string

blur type: gaussian or selective. Default: 'gaussian'.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpublicbrightness( int $value = -20 ) : object

Set brightness.

This method is chainable.

Parameters
Name Type Description
$value int

level of brightness between -255 to 255. Default: -20.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpubliccontrast( int $value = -10 ) : object

Set contrast.

This method is chainable.

Parameters
Name Type Description
$value int

level of contrast between -255 to 255. Default: -10.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpublicgetOption( string $key ) : mixed

Returns the value of the specified option.

Parameters
Name Type Description
$key string

the name of the option to retrieve

Returns
Type Description
mixed option or false
Details
Access
public  
methodpublicgreyscale( ) : object

Convert image to greyscale.

This method is chainable.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpublicinterlace( ) : object

Interlace image.

This method is chainable.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicmask( string $file, int $top = 0, int $left = 0, string $bgColor = '' ) : object

Add mask layer to image. You can create for example polaroid like images.

This method is chainable.

Parameters
Name Type Description
$file string

an absolute URL or path to mask .png file

$top int

top position of resized image. Default: 0.

$left int

left position of resized image. Default: 0.

$bgColor string

HEX color of the uncovered/transparent zone

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception invalid image
Details
Access
public  
methodpublicnoise( int $value = 30 ) : object

Set noise.

This method is chainable.

Parameters
Name Type Description
$value int

factor between 0 and 255. Default: 30.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicpixelate( int $value = 10, bool $native = true ) : object

Set pixelate.

This method is chainable.

Parameters
Name Type Description
$value int

size of pixels. Default: 10.

$native bool

use native GD library pixelate filter if available. Enabled by default.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicrasterbate( int $value = 6 ) : object

Set rasterbate.

This method is chainable.

Parameters
Name Type Description
$value int

maximum circle size in pixels. Default: 6.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicrender( string $file, int $quality = 80, bool $destroy = true ) : void

Output the image to the browser without saving the image.

Parameters
Name Type Description
$file string

output image file name

$quality int

image quality: 0-100. Default: 80.

$destroy bool

destroy image resource identifiers on save. Enabled by default.

Throws
Exception Description
\Exception invalid image type
Details
Access
public  
methodpublicresize( int $newWidth, int $newHeight, string $option = 'auto' ) : object

Resize the image. Must be called first if resize is required since this will reset all filters for image.

This method is chainable.

Parameters
Name Type Description
$newWidth int

new width of the image in pixels

$newHeight int

new height of the image in pixels

$option string

one of the image resize options: exact, portrait, landscape, crop or auto. Default: 'auto'.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicrotate( mixed $value = 'random', string $bgColor = 'ffffff' ) : object

Rotate image.

This method is chainable.

Parameters
Name Type Description
$value mixed

rotation angle in degrees between -360 and 360 or random. Default: 'random'.

$bgColor string

HEX color of the uncovered zone after the rotation. Default: 'ffffff'.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicsave( string $path, int $quality = 80, bool $destroy = true, int $chmod = 0644 ) : void

Save the image.

Parameters
Name Type Description
$path string

file path and output image file name. Directory must writable.

$quality int

image quality: 0-100. Default: 80.

$destroy bool

destroy image resource identifiers on save. Enabled by default.

$chmod int

permissions for saved image. Default: 0644.

Throws
Exception Description
\Exception invalid image type or directory is unwritable
Details
Access
public  
methodpublicscatter( int $value = 4 ) : object

Set scatter.

This method is chainable.

Parameters
Name Type Description
$value int

intensity. Default: 4.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicsepia( string $rgb = '90, 55, 30', int $brightness = -30 ) : object

Set sepia.

This method is chainable.

Parameters
Name Type Description
$rgb string

comma separated RGB value. Default: '90, 55, 30'.

$brightness int

level of brightness. Default: -30.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpublicsharpen( bool $value = true ) : object

Set sharpen.

Parameters
Name Type Description
$value bool

sharpen image automatically at first save or render with optimal image sharpening detection. Enabled by default.

Returns
Type Description
object the current object for fluent interface
Details
Access
public  
methodpublicsmooth( int $value = 6 ) : object

Set smooth.

This method is chainable.

Parameters
Name Type Description
$value int

level of smoothness -12 to 12. Values outside of the range -8 to 8 are usually unusable. Default: 6.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception missing imagefilter function
Details
Access
public  
methodpublicwatermark( string $file, int $transparency = 40, int $padding = 2, string $position = 'BR' ) : object

Add watermark to the image.

This method is chainable.

Parameters
Name Type Description
$file string

an absolute URL or path to file

$transparency int

watermark transparency (0-100). Default: 40.

$padding int

watermark padding in pixels. Default: 2.

$position string

position of watermark (one of: TL, TR, BL or BR). Default: 'BR'.

Returns
Type Description
object the current object for fluent interface
Throws
Exception Description
\Exception invalid image
Details
Access
public  
Documentation was generated by DocBlox 0.15.0.