PHP Class Zebra_Image, Zebra_Image

The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E_ALL. With this library you can rescale, flip, rotate and crop images. It supports loading and saving images in the GIF, JPEG and PNG formats and preserves transparency for GIF, PNG and PNG24. The cool thing about it is that it can resize images to exact given width and height and still maintain aspect ratio. Visit {@link http://stefangabos.ro/php-libraries/zebra-image/} for more information. For more resources visit {@link http://stefangabos.ro/}
Show file Open project: stefangabos/zebra_image Class Usage Examples

Public Properties

Property Type Description
$auto_handle_exif_orientation boolean 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 {@link http://php.net/manual/en/exif.installation.php the PHP manual} Default is FALSE
$chmod_value integer 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 ÿ 撜˼
$enlarge_smaller_images boolean Available only for the {@link resize()} method Default is TRUE
$error integer 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).
$jpeg_quality integer Used only if the file at {@link target_path} is a JPG/JPEG image. Range is 0 - 100 Default is 85
$png_compression integer Available only if PHP version is 5.1.2+, and only if the file at {@link target_path} is a PNG image. It will be ignored otherwise. Range is 0 - 9 Default is 9
$preserve_aspect_ratio boolean Available only for the {@link resize()} method Default is TRUE
$preserve_time boolean Default is TRUE
$sharpen_images boolean 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
$source_path string Supported file types are GIF, PNG and JPEG.
$target_path string Can be a different than {@link 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).

Public Methods

Method Description
__construct ( ) : void Constructor of the class.
apply_filter ( string $filter, mixed $arg1 = '', mixed $arg2 = '', mixed $arg3 = '', mixed $arg4 = '' ) : boolean Applies one or more filters to the image given as {@link source_path} and outputs it as the file specified as {@link target_path}.
crop ( integer $start_x, integer $start_y, integer $end_x, integer $end_y ) : boolean Crops a portion of the image given as {@link source_path} and outputs it as the file specified as {@link target_path}.
flip_both ( ) : boolean Flips both horizontally and vertically the image given as {@link source_path} and outputs the resulted image as {@link target_path}
flip_horizontal ( ) : boolean Flips horizontally the image given as {@link source_path} and outputs the resulted image as {@link target_path}
flip_vertical ( ) : boolean Flips vertically the image given as {@link source_path} and outputs the resulted image as {@link target_path}
resize ( integer $width, integer $height, integer $method = ZEBRA_IMAGE_CROP_CENTER, hexadecimal $background_color = '#FFFFFF' ) : boolean Resizes the image given as {@link source_path} and outputs the resulted image as {@link target_path}.
rotate ( double $angle, mixed $background_color ) : boolean Rotates the image given as {@link source_path} and outputs the resulted image as {@link target_path}.

Private Methods

Method Description
_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 ( $orientation ) : boolean Flips horizontally or vertically or both ways the image given as {@link source_path}.
_hex2rgb ( string $color, string $default_on_error = '#FFFFFF' ) : array Converts a hexadecimal representation of a color (i.e. #123456 or #AAA) to a RGB representation.
_prepare_image ( integer $width, integer $height, string $background_color = '#FFFFFF' ) : Returns Creates a blank image of given width, height and background color.
_sharpen_image ( $image ) Sharpens images. Useful when creating thumbnails.
_write_image ( $identifier ) : boolean Creates a new image from given image identifier having the extension as specified by {@link target_path}.

Method Details

__construct() public method

Initializes the class and the default properties
public __construct ( ) : void
return void

apply_filter() public method

This method is available only if the {@link http://php.net/manual/en/function.imagefilter.php imagefilter} 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), ));
public apply_filter ( string $filter, mixed $arg1 = '', mixed $arg2 = '', mixed $arg3 = '', mixed $arg4 = '' ) : boolean
$filter string The (case-insensitive) name of the filter to apply. Can be one of the following: - brightness - changes the brightness of the image; use arg1 to set the level of brightness; the range of brightness is -255 to 255; - colorize - adds (subtracts) specified RGB values to each pixel; use arg1, arg2 and arg3 in the form of red, green, blue and arg4 for the alpha channel. the range for each color is -255 to 255 and 0 to 127 for alpha; alpha support is available only for PHP 5.2.5+; - contrast - changes the contrast of the image; use arg1 to set the level of contrast; the range of contrast is -100 to 100; - gausian_blur - blurs the image using the Gaussian method; - grayscale - converts the image into grayscale; - edgedetect - uses edge detection to highlight the edges in the image; - emboss - embosses the image; - mean_removal - uses mean removal to achieve a "sketchy" effect; - negate - reverses all the colors of the image; - pixelate - applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode; this filter is available only for PHP 5.3.0+; - selective_blur - blurs the image; - smooth - makes the image smoother. Use arg1 to set the level of smoothness. applies a 9-cell convolution matrix where center pixel has the weight of arg1 and others weight of 1.0. the result is normalized by dividing the sum with arg1 + 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 boolean 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 mixed
$arg2 mixed
$arg3 mixed
$arg4 mixed
return boolean

crop() public method

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);
public crop ( integer $start_x, integer $start_y, integer $end_x, integer $end_y ) : boolean
$start_x integer x coordinate to start cropping from @param integer $start_y y coordinate to start cropping from @param integer $end_x x coordinate where to end the cropping @param integer $end_y y coordinate where to end the cropping @since 1.0.4 @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code.
$start_y integer
$end_x integer
$end_y integer
return boolean

flip_both() public method

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();
Since: 2.1 @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code.
public flip_both ( ) : boolean
return boolean

flip_horizontal() public method

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();
public flip_horizontal ( ) : boolean
return 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() public method

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();
public flip_vertical ( ) : boolean
return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code.

resize() public method

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);
public resize ( integer $width, integer $height, integer $method = ZEBRA_IMAGE_CROP_CENTER, hexadecimal $background_color = '#FFFFFF' ) : boolean
$width integer The width to resize the image to. If set to 0, the width will be automatically adjusted, depending on the value of the height argument so that the image preserves its aspect ratio. If {@link preserve_aspect_ratio} is set to TRUE and both this and the height arguments are values greater than 0, 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 method 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 width and height are set to 0, a copy of the source image will be created ({@link jpeg_quality} and {@link png_compression} will still apply). If either width or height are set to 0, the script will consider the value of the {@link preserve_aspect_ratio} to bet set to TRUE regardless of its actual value! @param integer $height The height to resize the image to. If set to 0, the height will be automatically adjusted, depending on the value of the width argument so that the image preserves its aspect ratio. If {@link preserve_aspect_ratio} is set to TRUE and both this and the width arguments are values greater than 0, 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 method 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 width and height are set to 0, a copy of the source image will be created ({@link jpeg_quality} and {@link png_compression} will still apply). If either height or width are set to 0, 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 width and height arguments are values greater than 0, 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: - ZEBRA_IMAGE_BOXED - the image will be scalled 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 bgcolor argument. (the blank area will be filled only if the image is not transparent!) - ZEBRA_IMAGE_NOT_BOXED - the image will be scalled so that it could 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 - ZEBRA_IMAGE_CROP_TOPLEFT - ZEBRA_IMAGE_CROP_TOPCENTER - ZEBRA_IMAGE_CROP_TOPRIGHT - ZEBRA_IMAGE_CROP_MIDDLELEFT - ZEBRA_IMAGE_CROP_CENTER - ZEBRA_IMAGE_CROP_MIDDLERIGHT - ZEBRA_IMAGE_CROP_BOTTOMLEFT - ZEBRA_IMAGE_CROP_BOTTOMCENTER - ZEBRA_IMAGE_CROP_BOTTOMRIGHT 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 in this case. Default is #FFFFFF. @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see what went wrong
$height integer
$method integer
$background_color hexadecimal
return boolean

rotate() public method

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);
public rotate ( double $angle, mixed $background_color ) : boolean
$angle double 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 in this case. Default is -1. @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see the error code.
$background_color mixed
return boolean

Property Details

$auto_handle_exif_orientation public property

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 {@link http://php.net/manual/en/exif.installation.php the PHP manual} Default is FALSE
Since: 2.2.4 @var boolean
public bool $auto_handle_exif_orientation
return boolean

$chmod_value public property

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 ÿ 撜˼
public int $chmod_value
return integer

$enlarge_smaller_images public property

Available only for the {@link resize()} method Default is TRUE
public bool $enlarge_smaller_images
return boolean

$error public property

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).
public int $error
return integer

$jpeg_quality public property

Used only if the file at {@link target_path} is a JPG/JPEG image. Range is 0 - 100 Default is 85
public int $jpeg_quality
return integer

$png_compression public property

Available only if PHP version is 5.1.2+, and only if the file at {@link target_path} is a PNG image. It will be ignored otherwise. Range is 0 - 9 Default is 9
Since: 2.2 @var integer
public int $png_compression
return integer

$preserve_aspect_ratio public property

Available only for the {@link resize()} method Default is TRUE
public bool $preserve_aspect_ratio
return boolean

$preserve_time public property

Default is TRUE
Since: 1.0.4 @var boolean
public bool $preserve_time
return boolean

$sharpen_images public property

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
Since: 2.2 @var boolean
public bool $sharpen_images
return boolean

$source_path public property

Supported file types are GIF, PNG and JPEG.
public string $source_path
return string

$target_path public property

Can be a different than {@link 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).
public string $target_path
return string