waImage

Image management

Contents...

To begin working with an image, create an instance of this class by passing the file to path to its factory method; e.g.:

$image = waImage::factory($path);
$image->rotate(90);

Methods

  • crop

    Crops specified image part.

  • factory

    Returns an instance of class waImage for processing specified image file.

  • filter

    Applies a graphical filter to an image.

  • fixImageOrientation

    Restores image orientation according to the EXIF data.

  • getExt

    Returns name extension of a graphical file corresponding to its type.

  • getPixel

    Returns information about a specified image pixel.

  • isWebpSupported

    Returns the WebP image format support status.

  • resize

    Resizes an image.

  • rotate

    Rotates an image.

  • save

    Save an image with specified quality level to a file.

  • sharpen

    Applies a sharpening filter to an image.

  • watermark

    Adds a watermark to an image.

public function crop ($width, $height, $offset_x = self::CENTER, $offset_y = self::CENTER, $deny_exceed_original_sizes = true)

Crops specified image part.

Parameters

  • $width

    Cropped portion width.

  • $height

    Cropped portion height.

  • $offset_x

    Offset of the cropped image portion to the right of the left edge of the original image, in pixels. If not specified, the central image part will be cropped.

  • $offset_y

    Offset of the cropped image portion to the bottom of the top edge of the original image, in pixels. If not specified, the central image part will be cropped.

  • $deny_exceed_original_sizes

    Flag allowing the dimensions of the cropped image part to exceed those of the original image. If not specified, true is used by default.

Example

$image = waImage::factory($image_path);
$image->crop(100, 100);

public static function factory ($file, $adapter = false)

Returns an instance of class waImage for processing specified image file.

Parameters

  • $file

    Path to image file to be processed.

Example

$image = waImage::factory($image_path);
//here you can call public methods of the <code>$image</code> object to process the specified image

public function filter ($type, $params = array())

Applies a graphical filter to an image.

Parameters

  • $type

    Filter type. Acceptable values:

    • FILTER_GRAYSCALE
    • FILTER_SEPIA
    • FILTER_CONTRAST
    • FILTER_BRIGHTNESS
  • $params

    Associative array of parameters for applying filters CONTRAST and BRIGHTNESS with the following keys:

    • level: integer degree of filter strength from 1 to 100. If not specified, the default value of 3 is used.

Example

$image = waImage::factory($image_path);
$image->filter('CONTRAST', array('level' => 15));

public function fixImageOrientation()

Restores image orientation according to the EXIF data.

Example

$image = new waImage($image_path);
$image->fixImageOrientation();

public function getExt()

Returns name extension of a graphical file corresponding to its type.

Example

$image = new waImage($jpeg_image_path);
echo $image->getExt();

Result

jpg

public function resize ($width = null, $height = null, $master = null, $deny_exceed_original_sizes = true)

Resizes an image.

Parameters

  • $width

    Required image width.

  • $height

    Required image width.

  • $master

    String parameter denoting which of the image dimensions (width or height) should be used as the basic one to calculate the length of the other dimension. If not specified, then AUTO value is used by default. Acceptable values:

    • NONE: If no value for $width parameter is specified, then with NONE, the width of the thumbnail will be equal to that of the original image; if no value for $height parameter is specified, then with NONE, the height of the thumbnail will be equal to that of the original image.
    • AUTO: If the ratio of the original image width to the thumbnail width is greater than the ratio of the original height to the thumbnail height, then WIDTH is used as the value of the $master parameter; otherwise HEIGHT is used.
    • INVERSE: If the ratio of the original image width to the thumbnail width is greater than the ratio of the original height to the thumbnail height, then HEIGHT is used as the value of the $master parameter; otherwise WIDTH is used.
    • WIDTH: The height of the thumbnail is calculated based on the width of the original image with proportions maintained; if any value is specified for the $height parameter, it is ignored.
    • HEIGHT: The width of the thumbnail is calculated based on the height of the original image with proportions maintained; if any value is specified for the $width parameter, it is ignored.
  • $deny_exceed_original_sizes Flag allowing the dimensions of the cropped image part to exceed those of the original image. If not specified, true is used by default.

Example

$image = waImage::factory($image_path);
$image->resize(100, 150, 'HEIGHT');

public function rotate ($degrees)

Rotates an image.

Parameters

  • $degrees

    Integer value of the rotation degree from -360 to 360. Positive values mean clockwise rotation, negative values mean counterclockwise.

Example

$image = waImage::factory($image_path);
$image->rotate(90);

public function save ($file = null, $quality = 100)

Save an image with specified quality level to a file.

Parameters

  • $file

    Path at which the image should be saved. If not specified, the image will be saved to the original file.

  • $quality

    Integer value of image quality percentage from 1 (worst quality) to 100 (best quality, default value).

Example

$image = waImage::factory($image_path);
// here you can call image processing methods
// and then save the modified image to a file
$image->save(null, 85);

public function sharpen ($amount)

Applies a sharpening filter to an image.

Parameters

  • $amount

    Filter strength from 1 to 100. Integer and fractional values are acceptable.

Example

$image = waImage::factory($image_path);
$image->sharpen(15);

public function watermark ($options)

Adds a watermark to an image.

Parameters

  • $options

    Associative array of watermark options with the following keys:

    • watermark: an instance of waImage class containing information about an image, or a text string.
    • opacity: fractional value of watermark opacity from 0 to 1. Maximum transparency corresponds to 0. If not specified, default value of 0.3 is used.
    • align: string identifier of text alignment relative to the original image (default value is ALIGN_TOP_LEFT). Acceptable values:
      • ALIGN_TOP_LEFT
      • ALIGN_TOP_RIGHT
      • ALIGN_BOTTOM_LEFT
      • ALIGN_BOTTOM_RIGHT
    • font_file: path to font file. If not specified, or if the specified font file is missing, default font built in PHP is used.
    • font_size: font size. If not specified, default value of 14 is used.
    • font_color: 6-digit color value. If not specified, 000000 is used by default.
    • text_orientation: string identifier of text orientation. Acceptable values:
      • VERTICAL
      • HORIZONTAL: this value is used if text orientation is not specified.

Example

$image = waImage::factory($image_path);
$options = array(
    'watermark'        => 'watermark text',
    'opacity'          => 0.5,
    'align'            => 'ALIGN_BOTTOM_RIGHT',
    'font_size'        => '10',
    'font_color'       => 'ffffff',
    'text_orientation' => 'VERTICAL',
);
// below is an example of watermark parameters if an image file is used instead of simple text
/*
$options = array(
    'watermark' => new waImage($watermark_image_path),
    'opacity'   => 0.3,
    'align'     => 'ALIGN_BOTTOM_RIGHT',
);
*/
$image->watermark($options);

public static function isWebpSupported()

Returns the WebP image format support status.

Example

if (waImage::isWebpSupported()) {
    echo 'WebP is supported!';
}

public function getPixel ($x, $y)

Returns information about a specified image pixel as an array:

  • red color value,
  • green color value,
  • blue color value,
  • transparency value.

Parameters

  • $x

    Horizontal coordinate relative to the left-hand image edge.

  • $y

    Vertical coordinate relative to the top image edge.

Example

$image = waImage::factory($path);
wa_dump($image->getPixel(10, 10));

Result

[
    0.3215686274509804,
    0.32941176470588235,
    0.35294117647058826,
    1,
]