Generic image class.
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.
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.
$filenameFilename of the image to load.
A new AnewtImage instance
static from_png($filename) [static] ¶Load a PNG image from a file.
static from_jpeg($filename) [static] ¶Load a JPEG image from a file.
static from_gif($filename) [static] ¶Load a GIF image from a file.
static from_string($str) [static] ¶Load an image from a string.
The image type is automatically detected.
$strThe str holding the image data.
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.
$colorThe color to convert
String representation of the color
static color_to_rgb($color) [static] ¶Convert a color to a (red, green, blue) tuple.
$colorThe color to convert
Array with three integer values for the red, green and blue component.
__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!
$widthThe width of the image (in pixels)
$heightThe height of the image (in pixels)
$imgInternal 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.
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.
$widthThe width of the image (in pixels)
$heightThe height of the image (in pixels)
crop($x, $y, $width, $height) ¶Crop the image to the specified dimensions.
$xThe x coordinate of the rectangle to crop to (in pixels)
$yThe y coordinate of the rectangle to crop to (in pixels)
$widthThe width of the rectangle to crop to (in pixels)
$heightThe height of the rectangle to crop to (in pixels)
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.
$new_widthNew width 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.
$new_heightNew height of the image.
resize($width, $height, $resample=null) ¶Resize image to the specified width and height.
$widthThe new width
$heightThe new height
$resampleWhether to use image resampling (default is true)
resize_relative($scale_factor, $resample=null) ¶Resize image relative to its current size.
$scale_factorScale factor. Values less than 0 shrink the image, values above 1 enlarge the image.
$resampleWhether to use image resampling (default is true)
resize_percentage($scale_percentage, $resample=null) ¶Resize image relative to its current size by providing a percentage.
$scale_percentageScale factor as percentage. Values less than 100 shrink the image, values above 100 enlarge the image.
$resampleWhether 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.
$widthThe new width
$resampleWhether 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.
$heightThe new height
$resampleWhether 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.
$widthThe maximum width to fit in.
$heightThe maximum height to fit in.
$scale_upWhether to scale up if both existing dimensions are smaller than the specified dimensions (default is false).
$resampleWhether 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.
$strThe string to parse.
Allocated color value
color_from_rgb($red, $green, $blue) ¶Allocates as color based on red, green and blue values.
All values should be between 0 and 255.
$redRed component value
$greenGreen component value
$blueBlue component value
Allocated color value
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.
$redRed component value
$greenGreen component value
$blueBlue component value
$alphaAlpha component value
Allocated color value
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.
$colorThe 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.
A new AnewtImageDrawingContext instance.
flush() ¶Flush the output to the browser.
The 'filename' property is inspected to see what file type to use, e.g. PNG or JPEG.
flush_jpeg($quality=null) ¶Flush this image as a JPEG image.
$qualityThe JPEG quality to use for saving (optional).
save_jpeg($filename, $quality=null) ¶Save this image as a JPEG image.
$filenameThe filename to save to.
$qualityThe 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.
get_quality_() [protected] ¶Default image quality.
This is mainly used for JPEG images and returns the value 85.
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.
static _filename_looks_like_png($filename) [private] [static] ¶Check if filename looks like a PNG filename.
$filenameFilename to test.
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.
$filenameFilename to test.
True if the filename looks like a JPEG filename, false otherwise.
_allocate_color($red, $green, $blue, $alpha=null) [private] ¶Allocates a color in the current image.
$redRed component value
$greenGreen component value
$blueBlue component value
$alphaAlpha component value
Allocated color value
_content_type($content_type) [private] ¶Send a Content-type header.
$content_typeThe content-type to send.
$__img ¶Image instance.
Though not marked as such, this is a private variable that should only be accessed from within this module.
$__allocated_colors [private] ¶List of allocated colors.
AnewtContainer::_to_array()AnewtContainer::_add()AnewtContainer::delete()AnewtContainer::get()AnewtContainer::_isset()AnewtContainer::_seed()AnewtContainer::setdefault()AnewtContainer::getdefault()AnewtContainer::set()AnewtContainer::__isset()AnewtContainer::__set()AnewtContainer::__construct()AnewtContainer::is_set()AnewtContainer::add()AnewtContainer::_getdefault()AnewtContainer::__unset()AnewtContainer::seed()AnewtContainer::to_array()AnewtContainer::_get()AnewtContainer::__get()AnewtContainer::keys()AnewtContainer::clear()AnewtContainer::_keys()AnewtContainer::_set()