AnewtImage class

Generic image class.

Class Overview

Generic image class.

This class supports loading (PNG, JPEG, GIF) and saving of (PNG, JPEG) images. This class heavily uses the image functions from the GD2 library bundled with PHP, but provides a decent API and many convenience methods for loading, saving, resampling, resizing and drawing.

Public Static Methods

static from_file($filename) [static]

Load an image from a file.

This method tries to detect the filetype automatically and will abort if the automatic detection failed.

Parameters

$filename

Filename of the image to load.

Return value

A new AnewtImage instance

static from_png($filename) [static]

Load a PNG image from a file.

Parameters

$filename

Filename of the image to load.

Return value

A new AnewtImage instance

static from_jpeg($filename) [static]

Load a JPEG image from a file.

Parameters

$filename

Filename of the image to load.

Return value

A new AnewtImage instance

static from_gif($filename) [static]

Load a GIF image from a file.

Parameters

$filename

Filename of the image to load.

Return value

A new AnewtImage instance

static from_string($str) [static]

Load an image from a string.

The image type is automatically detected.

Parameters

$str

The str holding the image data.

Return value

A new AnewtImage instance

static color_to_string($color) [static]

Convert a color to hexadecimal representation suitable for usage in HTML and CSS.

The resulting string includes a leading # character.

Parameters

$color

The color to convert

Return value

String representation of the color

static color_to_rgb($color) [static]

Convert a color to a (red, green, blue) tuple.

Parameters

$color

The color to convert

Return value

Array with three integer values for the red, green and blue component.

Public Methods

__construct($width, $height, $img=null)

Create a new image instance.

This method seems to accept three parameters, but you should really only provide the width and height. The third parameter is for internal usage only!

Parameters

$width

The width of the image (in pixels)

$height

The height of the image (in pixels)

$img

Internal parameter for private internal use. Do not provide this.

destroy()

Destroy this image and all allocated resources.

Do not invoke any instance methods after this function has been called.

get_dimensions()

Return dimensions of this image.

Return value

Array with (width, height) integer values.

set_dimensions($width, $height)

Set the image dimension to the specified width and height.

Note that this crops the image from the top left corner.

Parameters

$width

The width of the image (in pixels)

$height

The height of the image (in pixels)

See also

crop($x, $y, $width, $height)

Crop the image to the specified dimensions.

Parameters

$x

The x coordinate of the rectangle to crop to (in pixels)

$y

The y coordinate of the rectangle to crop to (in pixels)

$width

The width of the rectangle to crop to (in pixels)

$height

The height of the rectangle to crop to (in pixels)

See also

get_width()

Get the width of the current image.

Return value

Width of the image

set_width($new_width)

Set the width of the current image.

This crops the image if the new size is smaller than the current value.

Parameters

$new_width

New width of the image.

get_height()

Get the height of the current image.

Return value

Height of the image

set_height($new_height)

Set the height of the current image.

This crops the image if the new size is smaller than the current value.

Parameters

$new_height

New height of the image.

resize($width, $height, $resample=null)

Resize image to the specified width and height.

Parameters

$width

The new width

$height

The new height

$resample

Whether to use image resampling (default is true)

resize_relative($scale_factor, $resample=null)

Resize image relative to its current size.

Parameters

$scale_factor

Scale factor. Values less than 0 shrink the image, values above 1 enlarge the image.

$resample

Whether to use image resampling (default is true)

resize_percentage($scale_percentage, $resample=null)

Resize image relative to its current size by providing a percentage.

Parameters

$scale_percentage

Scale factor as percentage. Values less than 100 shrink the image, values above 100 enlarge the image.

$resample

Whether to use image resampling (default is true)

resize_width($width, $resample=null)

Resize image to the specified width.

The height is adjusted automatically, keeping the same aspect ratio.

Parameters

$width

The new width

$resample

Whether to use image resampling (default is true)

resize_height($height, $resample=null)

Resize image to the specified height.

The width is adjusted automatically, keeping the same aspect ratio.

Parameters

$height

The new height

$resample

Whether to use image resampling (default is true)

resize_within($width, $height, $scale_up=null, $resample=null)

Resize image so that it fits the specified dimensions.

Aspect ratio is preserved.

Parameters

$width

The maximum width to fit in.

$height

The maximum height to fit in.

$scale_up

Whether to scale up if both existing dimensions are smaller than the specified dimensions (default is false).

$resample

Whether to use image resampling (default is true)

color_from_string($str)

Parses a color from a string.

Hexadecimal values (HTML/CSS format) are converted to a color value, e.g. #ff0000 for red. The alpha channel (transparency) can be provided as well by adding two more letters, e.g. #ff000012. Values consisting of only 3 letters are accepted as well and interpreted according to the same semantics as HTML/CSS uses.

The leading # character is optional.

Parameters

$str

The string to parse.

Return value

Allocated color value

See also

color_from_rgb($red, $green, $blue)

Allocates as color based on red, green and blue values.

All values should be between 0 and 255.

Parameters

$red

Red component value

$green

Green component value

$blue

Blue component value

Return value

Allocated color value

See also

color_from_rgba($red, $green, $blue, $alpha)

Allocates as color based on red, green, blue and alpha values.

All values should be between 0 and 255.

Parameters

$red

Red component value

$green

Green component value

$blue

Blue component value

$alpha

Alpha component value

Return value

Allocated color value

See also

fill($color)

Fills the complete image with the specified color.

This sets the background of the image if called early, and overwrites the current image contents if called after any loading or drawing operations. Note that, when using a color with alpha blending, the current contents of the image are effectively colorized with the specified color.

Parameters

$color

The color to use. This color may include an alpha component.

create_drawing_context()

Create a new drawing context for this image.

You should set some properties and call drawing methods on the returned object instance to create dynamic images.

Return value

A new AnewtImageDrawingContext instance.

save()

Save a previously loaded image back to disk.

flush()

Flush the output to the browser.

The 'filename' property is inspected to see what file type to use, e.g. PNG or JPEG.

See also

flush_png()

Flush this image as a PNG image.

save_png($filename)

Save this image as a PNG image.

Parameters

$filename

The filename to save to.

See also

flush_jpeg($quality=null)

Flush this image as a JPEG image.

Parameters

$quality

The JPEG quality to use for saving (optional).

See also

save_jpeg($filename, $quality=null)

Save this image as a JPEG image.

Parameters

$filename

The filename to save to.

$quality

The JPEG quality to use for saving (optional). This does not affect the image contents itself; that happens only after opening the file from disk after writing.

See also

Protected Methods

get_quality_() [protected]

Default image quality.

This is mainly used for JPEG images and returns the value 85.

Return value

The number 85.

pre_output() [protected]

Callback prior to image output.

Override this method if you want to change the image before it is output, e.g. draw an overlay or even draw a sparkline...

post_output() [protected]

Callback after image output.

Override this method if you want to do postprocessing, e.g. some cleaning up routines could go here. Note that this will not have any effect on the image; it is already saved to a file or sent to the browser.

Private Static Methods

static _filename_looks_like_png($filename) [private] [static]

Check if filename looks like a PNG filename.

Parameters

$filename

Filename to test.

Return value

True if the filename looks like a PNG filename, false otherwise.

static _filename_looks_like_jpeg($filename) [private] [static]

Check if filename looks like a JPEG filename.

Parameters

$filename

Filename to test.

Return value

True if the filename looks like a JPEG filename, false otherwise.

Private Methods

_allocate_color($red, $green, $blue, $alpha=null) [private]

Allocates a color in the current image.

Parameters

$red

Red component value

$green

Green component value

$blue

Blue component value

$alpha

Alpha component value

Return value

Allocated color value

_content_type($content_type) [private]

Send a Content-type header.

Parameters

$content_type

The content-type to send.

Public Attributes

$__img

Image instance.

Though not marked as such, this is a private variable that should only be accessed from within this module.

Private Attributes

$__allocated_colors [private]

List of allocated colors.

Inheritance

Base Classes

Inherited members