\LinHUniX\Gfx\Componentzebra_image

A compact (one-file only) and lightweight PHP library for image manipulation providing methods for performing several types of image manipulation operations and applying filters to images.

Read more here

Summary

Methods
Properties
Constants
__construct()
apply_filter()
crop()
flip_both()
flip_horizontal()
flip_vertical()
resize()
rotate()
$chmod_value
$enlarge_smaller_images
$error
$auto_handle_exif_orientation
$jpeg_quality
$png_compression
$preserve_aspect_ratio
$preserve_time
$sharpen_images
$source_path
$target_path
No constants found
No protected methods found
No protected properties found
N/A
_create_from_source()
_flip()
_hex2rgb()
_prepare_image()
_sharpen_image()
_write_image()
No private properties found
N/A

Properties

$chmod_value

$chmod_value : integer

Indicates the file system permissions to be set for newly created images.

Better is to leave this setting as it is.

If you know what you are doing, here is how you can calculate the permission levels:

  • 400 Owner Read
  • 200 Owner Write
  • 100 Owner Execute
  • 40 Group Read
  • 20 Group Write
  • 10 Group Execute
  • 4 Global Read
  • 2 Global Write
  • 1 Global Execute

    Default is 0755

Type

integer

$enlarge_smaller_images

$enlarge_smaller_images : boolean

If set to FALSE, images having both width and height smaller than the required width and height, will be left untouched ({@link jpeg_quality} and {@link png_compression} will still apply).

Available only for the \resize() method

Default is TRUE

Type

boolean

$error

$error : integer

In case of an error read this property's value to see the error's code.

Possible error codes are:

  • 1: source file could not be found
  • 2: source file is not readable
  • 3: could not write target file
  • 4: unsupported source file format
  • 5: unsupported target file format
  • 6: GD library version does not support target file format
  • 7: GD library is not installed!
  • 8: "chmod" command is disabled via configuration
  • 9: "exif_read_data" function is not available

    Default is 0 (no error).

Type

integer

$auto_handle_exif_orientation

$auto_handle_exif_orientation : 

If set to TRUE, JPEG images will be auto-rotated according to the {@link http://keyj.emphy.de/exif-orientation-rant/ Exif Orientation Tag} so that they are always shown correctly.

If you set this to TRUE you must also enable exif-support with --enable-exif. Windows users must enable both the php_mbstring.dll and php_exif.dll DLL's in php.ini. The php_mbstring.dll DLL must be loaded before the php_exif.dll DLL so adjust your php.ini accordingly. See [the PHP manual](http://php.net/manual/en/exif.installation.php)

Default is FALSE

Type

$jpeg_quality

$jpeg_quality : integer

Indicates the quality of the output image (better quality means bigger file size).

Used only if the file at \target_path is a JPG/JPEG image.

Range is 0 - 100

Default is 85

Type

integer

$png_compression

$png_compression : 

Indicates the compression level of the output image (lower compression means bigger file size).

Available only if PHP version is 5.1.2+, and only if the file at \target_path is a PNG image. It will be ignored otherwise.

Range is 0 - 9

Default is 9

Type

$preserve_aspect_ratio

$preserve_aspect_ratio : boolean

Specifies whether, upon resizing, images should preserve their aspect ratio.

Available only for the \resize() method

Default is TRUE

Type

boolean

$preserve_time

$preserve_time : 

Indicates whether a target files should preserve the source file's date/time.

Default is TRUE

Type

$sharpen_images

$sharpen_images : 

Indicates whether the target image should have a "sharpen" filter applied to it.

Can be very useful when creating thumbnails and should be used only when creating thumbnails.

The sharpen filter relies on the "imageconvolution" PHP function which is available only for PHP version 5.1.0+, and will leave the images unaltered for older versions!

Default is FALSE

Type

$source_path

$source_path : string

Path to an image file to apply the transformations to.

Supported file types are GIF, PNG and JPEG.

Type

string

$target_path

$target_path : string

Path (including file name) to where to save the transformed image.

Can be a different than \source_path - the type of the transformed image will be as indicated by the file's extension (supported file types are GIF, PNG and JPEG).

Type

string

Methods

__construct()

__construct() 

Constructor of the class.

Initializes the class and the default properties

apply_filter()

apply_filter(string  $filter,   $arg1 = '',   $arg2 = '',   $arg3 = '',   $arg4 = '') 

Applies one or more filters to the image given as {@link source_path} and outputs it as the file specified as {@link target_path}.

This method is available only if the [imagefilter](http://php.net/manual/en/function.imagefilter.php) function is available (available from PHP 5+), and will leave images unaltered otherwise.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// apply the "grayscale" filter $img->apply_filter('grayscale');

// apply the "contrast" filter $img->apply_filter('contrast', -20);

You can also apply multiple filters at once. In this case, the method requires a single argument, an array of arrays, containing the filters and associated arguments, where applicable:

// create a sepia effect // note how we're applying multiple filters at once // each filter is in its own array $img->apply_filter(array(

 // first we apply the "grayscale" filter
 array('grayscale'),

 // then we apply the "colorize" filter with 90, 60, 40 as
 // the values for red, green and blue
 array('colorize', 90, 60, 40),

));

Parameters

string $filter

The (case-insensitive) name of the filter to apply. Can be one of the following:

                         -   <b>brightness</b>       -   changes the brightness of the image; use <b>arg1</b>
                                                         to set the level of brightness; the range of brightness
                                                         is -255 to 255;
                         -   <b>colorize</b>         -   adds (subtracts) specified RGB values to each pixel;
                                                         use <b>arg1</b>, <b>arg2</b> and <b>arg3</b> in the
                                                         form of red, green, blue and <b>arg4</b> for the alpha
                                                         channel. the range for each color is -255 to 255 and
                                                         0 to 127 for alpha; <i>alpha support is available only
                                                         for PHP 5.2.5+</i>;
                         -   <b>contrast</b>         -   changes the contrast of the image; use <b>arg1</b>
                                                         to set the level of contrast; the range of contrast
                                                         is -100 to 100;
                         -   <b>gausian_blur</b>     -   blurs the image using the Gaussian method;
                         -   <b>grayscale</b>        -   converts the image into grayscale;
                         -   <b>edgedetect</b>       -   uses edge detection to highlight the edges in the image;
                         -   <b>emboss</b>           -   embosses the image;
                         -   <b>mean_removal</b>     -   uses mean removal to achieve a "sketchy" effect;
                         -   <b>negate</b>           -   reverses all the colors of the image;
                         -   <b>pixelate</b>         -   applies pixelation effect to the image, use <b>arg1</b>
                                                         to set the block size and <b>arg2</b> to set the
                                                         pixelation effect mode; <i>this filter is available
                                                         only for PHP 5.3.0+</i>;
                         -   <b>selective_blur</b>   -   blurs the image;
                         -   <b>smooth</b>           -   makes the image smoother. Use <b>arg1</b> to set the
                                                         level of smoothness. applies a 9-cell convolution matrix
                                                         where center pixel has the weight of <b>arg1</b> and
                                                         others weight of 1.0. the result is normalized by dividing
                                                         the sum with <b>arg1</b> + 8.0 (sum of the matrix).
                                                         any float is accepted;

@param mixed $arg1 Used by the following filters:

  • brightness - sets the brightness level (-255 to 255)
  • contrast - sets the contrast level (-100 to 100)
  • colorize - sets the value of the red component (-255 to 255)
  • smooth - sets the smoothness level
  • pixelate - sets the block size, in pixels @param mixed $arg2 used by the following filters:
  • colorize - sets the value of the green component (-255 to 255)
  • pixelate - whether to use advanced pixelation effect or not (defaults to FALSE) @param mixed $arg3 Used by the following filters:
  • colorize - sets the value of the blue component (-255 to 255) @param mixed $arg4 Used by the following filters:
  • colorize - alpha channel; a value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.

    @since 2.2.2

    @return bool Returns TRUE on success or FALSE on error.

                         If {@link http://php.net/manual/en/function.imagefilter.php imagefilter} is not
                         available the method will return FALSE without setting an {@link error} code.
    
                         If the requested filter doesn't exist, or invalid arguments are passed, the method
                         will trigger a warning.
    
                         If FALSE is returned and you are sure that
                         {@link http://php.net/manual/en/function.imagefilter.php imagefilter} exists and that
                         the requested filter is valid, check the {@link error} property to see the error code.
$arg1
$arg2
$arg3
$arg4

crop()

crop(integer  $start_x,   $start_y,   $end_x,   $end_y) 

Crops a portion of the image given as {@link source_path} and outputs it as the file specified as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// crop a rectangle of 100x100 pixels, starting from the top-left corner $img->crop(0, 0, 100, 100);

Parameters

integer $start_x

x coordinate to start cropping from @param int $start_y y coordinate to start cropping from @param int $end_x x coordinate where to end the cropping @param int $end_y y coordinate where to end the cropping

@since 1.0.4

@return bool Returns TRUE on success or FALSE on error.

                 If FALSE is returned, check the {@link error} property to see the error code.
$start_y
$end_x
$end_y

flip_both()

flip_both() 

Flips both horizontally and vertically the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// flip the image both horizontally and vertically $img->flip_both();

flip_horizontal()

flip_horizontal() : boolean

Flips horizontally the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// flip the image horizontally $img->flip_horizontal();

Returns

boolean —

Returns TRUE on success or FALSE on error.

                 If FALSE is returned, check the {@link error} property to see the error code.

flip_vertical()

flip_vertical() : boolean

Flips vertically the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// flip the image vertically $img->flip_vertical();

Returns

boolean —

Returns TRUE on success or FALSE on error.

                 If FALSE is returned, check the {@link error} property to see the error code.

resize()

resize(integer  $width,   $height,   $method = ZEBRA_IMAGE_CROP_CENTER,   $background_color = -1) 

Resizes the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// apply a "sharpen" filter to the resulting images $img->sharpen_images = true;

// resize the image to exactly 150x150 pixels, without altering // aspect ratio, by using the CROP_CENTER method $img->resize(150, 150, ZEBRA_IMAGE_CROP_CENTER);

Parameters

integer $width

The width to resize the image to.

                                     If set to <b>0</b>, the width will be automatically adjusted, depending
                                     on the value of the <b>height</b> argument so that the image preserves
                                     its aspect ratio.

                                     If {@link preserve_aspect_ratio} is set to TRUE and both this and the
                                     <b>height</b> arguments are values greater than <b>0</b>, the image will
                                     be resized to the exact required width and height and the aspect ratio
                                     will be preserved - (also see the description for the <b>method</b>
                                     argument below on how can this be done).

                                     If {@link preserve_aspect_ratio} is set to FALSE, the image will be
                                     resized to the required width and the aspect ratio will be ignored.

                                     If both <b>width</b> and <b>height</b> are set to <b>0</b>, a copy of
                                     the source image will be created ({@link jpeg_quality} and
                                     {@link png_compression} will still apply).

                                     If either <b>width</b> or <b>height</b> are set to <b>0</b>, the script
                                     will consider the value of the {@link preserve_aspect_ratio} to bet set
                                     to TRUE regardless of its actual value!

@param int $height The height to resize the image to.

                                     If set to <b>0</b>, the height will be automatically adjusted, depending
                                     on the value of the <b>width</b> argument so that the image preserves
                                     its aspect ratio.

                                     If {@link preserve_aspect_ratio} is set to TRUE and both this and the
                                     <b>width</b> arguments are values greater than <b>0</b>, the image will
                                     be resized to the exact required width and height and the aspect ratio
                                     will be preserved - (also see the description for the <b>method</b>
                                     argument below on how can this be done).

                                     If {@link preserve_aspect_ratio} is set to FALSE, the image will be
                                     resized to the required height and the aspect ratio will be ignored.

                                     If both <b>width</b> and <b>height</b> are set to <b>0</b>, a copy of
                                     the source image will be created ({@link jpeg_quality} and
                                     {@link png_compression} will still apply).

                                     If either <b>height</b> or <b>width</b> are set to <b>0</b>, the script
                                     will consider the value of the {@link preserve_aspect_ratio} to bet set
                                     to TRUE regardless of its actual value!

@param int $method (Optional) Method to use when resizing images to exact width and height while preserving aspect ratio.

                                     If the {@link preserve_aspect_ratio} property is set to TRUE and both the
                                     <b>width</b> and <b>height</b> arguments are values greater than <b>0</b>,
                                     the image will be resized to the exact given width and height and the
                                     aspect ratio will be preserved by using on of the following methods:

                                     -   <b>ZEBRA_IMAGE_BOXED</b> - the image will be scaled so that it will
                                         fit in a box with the given width and height (both width/height will
                                         be smaller or equal to the required width/height) and then it will
                                         be centered both horizontally and vertically. The blank area will be
                                         filled with the color specified by the <b>bgcolor</b> argument. (the
                                         blank area will be filled only if the image is not transparent!)

                                     -   <b>ZEBRA_IMAGE_NOT_BOXED</b> - the image will be scaled so that it
                                         <i>could</i> fit in a box with the given width and height but will
                                         not be enclosed in a box with given width and height. The new width/
                                         height will be both smaller or equal to the required width/height

                                     -   <b>ZEBRA_IMAGE_CROP_TOPLEFT</b>
                                     -   <b>ZEBRA_IMAGE_CROP_TOPCENTER</b>
                                     -   <b>ZEBRA_IMAGE_CROP_TOPRIGHT</b>
                                     -   <b>ZEBRA_IMAGE_CROP_MIDDLELEFT</b>
                                     -   <b>ZEBRA_IMAGE_CROP_CENTER</b>
                                     -   <b>ZEBRA_IMAGE_CROP_MIDDLERIGHT</b>
                                     -   <b>ZEBRA_IMAGE_CROP_BOTTOMLEFT</b>
                                     -   <b>ZEBRA_IMAGE_CROP_BOTTOMCENTER</b>
                                     -   <b>ZEBRA_IMAGE_CROP_BOTTOMRIGHT</b>

                                     For the methods involving crop, first the image is scaled so that both
                                     its sides are equal or greater than the respective sizes of the bounding
                                     box; next, a region of required width and height will be cropped from
                                     indicated region of the resulted image.

                                     Default is ZEBRA_IMAGE_CROP_CENTER

@param hexadecimal $background_color (Optional) The hexadecimal color (like "#FFFFFF" or "#FFF") of the blank area. See the method argument.

                                     When set to -1 the script will preserve transparency for transparent GIF
                                     and PNG images. For non-transparent images the background will be white
                                     (#FFFFFF) in this case.

                                     Default is -1

@return bool Returns TRUE on success or FALSE on error.

                                     If FALSE is returned, check the {@link error} property to see what went
                                     wrong
$height
$method
$background_color

rotate()

rotate(float  $angle,   $background_color = -1) 

Rotates the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

// include the Zebra_Image library require 'path/to/Zebra_Image.php';

// instantiate the class $img = new Zebra_Image();

// a source image $img->source_path = 'path/to/source.ext';

// path to where should the resulting image be saved // note that by simply setting a different extension to the file will // instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext';

// rotate the image 45 degrees, clockwise $img->rotate(45);

Parameters

float $angle

Angle by which to rotate the image clockwise.

                                     Between 0 and 360.

@param mixed $background_color (Optional) The hexadecimal color (like "#FFFFFF" or "#FFF") of the uncovered zone after the rotation.

                                     When set to -1 the script will preserve transparency for transparent GIF
                                     and PNG images. For non-transparent images the background will be white
                                     (#FFFFFF) in this case.

                                     Default is -1.

@return bool Returns TRUE on success or FALSE on error.

                                     If FALSE is returned, check the {@link error} property to see the error
                                     code.
$background_color

_create_from_source()

_create_from_source() 

Returns an array containing the image identifier representing the image obtained from {@link $source_path}, the image's width and height and the image's type.

_flip()

_flip(  $orientation) : boolean

Flips horizontally or vertically or both ways the image given as {@link source_path}.

@since 2.1

Parameters

$orientation

Returns

boolean —

Returns TRUE on success or FALSE on error.

                 If FALSE is returned, check the {@link error} property to see the error code.

_hex2rgb()

_hex2rgb(string  $color,   $default_on_error = '#FFFFFF') 

Converts a hexadecimal representation of a color (i.e. #123456 or #AAA) to a RGB representation.

The RGB values will be a value between 0 and 255 each.

Parameters

string $color

Hexadecimal representation of a color (i.e. #123456 or #AAA). @param string $default_on_error (Optional) Hexadecimal representation of a color to be used in case $color is not recognized as a hexadecimal color.

                                 Default is #FFFFFF

@return array Returns an associative array with the values of (R)ed, (G)reen and (B)lue

$default_on_error

_prepare_image()

_prepare_image(  $width, integer  $height,   $background_color = '#FFFFFF') 

Creates a blank image of given width, height and background color.

@param int $width width of the new image

Parameters

$width
integer $height

height of the new image @param string $background_color (Optional) The hexadecimal color of the background.

                                     Can also be -1 case in which the script will try to create a transparent
                                     image, if possible.

                                     Default is #FFFFFF.

@return Returns the identifier of the newly created image

$background_color

_sharpen_image()

_sharpen_image(  $image) 

Sharpens images. Useful when creating thumbnails.

Code taken from the comments at http://docs.php.net/imageconvolution.

This function will yield a result only for PHP version 5.1.0+ and will leave the image unaltered for older versions!

Parameters

$image

_write_image()

_write_image(  $identifier) : boolean

Creates a new image from given image identifier having the extension as specified by {@link target_path}.

@param $identifier identifier An image identifier

Parameters

$identifier

Returns

boolean —

Returns TRUE on success or FALSE on error.

                             If FALSE is returned, check the {@link error} property to see the error code.