GD
in package
implements
Canvas
Image rendering interface
Renders to an image format supported by GD (jpeg, gif, png, xpm). Not super-useful day-to-day but handy nonetheless
Interfaces, Classes and Traits
- Canvas
- Main rendering interface
Table of Contents
- FONT_SCALE = 0.75
- Amount to scale font sizes
- $_aa_factor : float
- Image antialias factor
- $_actual_height : int
- Actual image height in pixels
- $_actual_width : int
- Actual image width in pixels
- $_bg_color : int
- Background color
- $_bg_color_array : int
- Background color array
- $_colors : array<string|int, mixed>
- Allocated colors
- $_dompdf : Dompdf
- $_height : int
- Apparent canvas height in pixels
- $_img : resource
- Resource handle for the image
- $_imgs : array<string|int, resource>
- Resource handle for the image
- $_page_count : int
- Total number of pages
- $_page_number : int
- Current page number
- $_width : int
- Apparent canvas width in pixels
- $dpi : int
- Actual DPI
- __construct() : mixed
- Class constructor
- add_info() : mixed
- Add meta information to the PDF
- add_link() : mixed
- Add a link to the pdf
- add_named_dest() : mixed
- Add a named destination (similar to <a name="foo">...</a> in html)
- add_object() : mixed
- arc() : mixed
- Add an arc to the PDF See {@link Style::munge_color()} for the format of the color array.
- circle() : mixed
- Draws a circle at $x,$y with radius $r
- clipping_end() : mixed
- Ends the last clipping shape
- clipping_rectangle() : mixed
- Starts a clipping rectangle at x1,y1 with width w and height h
- clipping_roundrectangle() : mixed
- Starts a rounded clipping rectangle at x1,y1 with width w and height h
- close_object() : mixed
- filled_rectangle() : mixed
- Draws a filled rectangle at x1,y1 with width w and height h
- get_dompdf() : Dompdf
- get_font_baseline() : float
- Calculates font baseline, in points
- get_font_height() : float
- Calculates font height, in points
- get_height() : float
- Return the image's height in pixels
- get_image() : resource
- Return the GF image resource
- get_page_count() : int
- Returns the total number of pages in the document
- get_page_number() : int
- Returns the current page number
- get_text_width() : float
- Calculates text size, in points
- get_ttf_file() : string
- get_width() : float
- Return the image's width in pixels
- javascript() : void
- line() : mixed
- Draws a line from x1,y1 to x2,y2
- new_page() : mixed
- Starts a new page
- open_object() : mixed
- output() : string
- Returns the image as a string.
- page_line() : mixed
- page_text() : mixed
- Writes text at the specified x and y coordinates on every page
- polygon() : mixed
- Draws a polygon
- rectangle() : mixed
- Draws a rectangle at x1,y1 with width w and height h
- restore() : mixed
- Restore last state
- rotate() : mixed
- Rotate
- save() : mixed
- Save current state
- scale() : mixed
- Scale
- set_default_view() : void
- Sets the default view
- set_opacity() : mixed
- Sets the opacity
- set_page_count() : mixed
- Sets the page count
- set_page_number() : mixed
- Sets the current page number
- skew() : mixed
- Skew
- stream() : mixed
- Streams the image to the client.
- text() : void
- Writes text at the specified x and y coordinates See {@link Style::munge_color()} for the format of the color array.
- transform() : mixed
- Transform
- translate() : mixed
- Translate
- _allocate_color() : int
- Allocate a new color. Allocate with GD as needed and store previously allocated colors in $this->_colors.
- _downscale() : float
- Scales value down from the current canvas DPI to 72 DPI
- _output() : mixed
- Outputs the image stream directly.
- _upscale() : float
- Scales value up to the current canvas DPI from 72 DPI
- get_font_height_actual() : mixed
Constants
FONT_SCALE
Amount to scale font sizes
public
float
FONT_SCALE
= 0.75
Font sizes are 72 DPI, GD internally uses 96. Scale them proportionally. 72 / 96 = 0.75.
Properties
$_aa_factor
Image antialias factor
protected
float
$_aa_factor
$_actual_height
Actual image height in pixels
protected
int
$_actual_height
$_actual_width
Actual image width in pixels
protected
int
$_actual_width
$_bg_color
Background color
protected
int
$_bg_color
$_bg_color_array
Background color array
protected
int
$_bg_color_array
$_colors
Allocated colors
protected
array<string|int, mixed>
$_colors
$_dompdf
protected
Dompdf
$_dompdf
$_height
Apparent canvas height in pixels
protected
int
$_height
$_img
Resource handle for the image
protected
resource
$_img
$_imgs
Resource handle for the image
protected
array<string|int, resource>
$_imgs
$_page_count
Total number of pages
protected
int
$_page_count
$_page_number
Current page number
protected
int
$_page_number
$_width
Apparent canvas width in pixels
protected
int
$_width
$dpi
Actual DPI
protected
int
$dpi
Methods
__construct()
Class constructor
public
__construct([mixed $size = 'letter' ][, string $orientation = "portrait" ], Dompdf $dompdf[, float $aa_factor = 1.0 ][, array<string|int, mixed> $bg_color = [1, 1, 1, 0] ]) : mixed
Parameters
- $size : mixed = 'letter'
-
The size of image to create: array(x1,y1,x2,y2) or "letter", "legal", etc.
- $orientation : string = "portrait"
-
The orientation of the document (either 'landscape' or 'portrait')
- $dompdf : Dompdf
- $aa_factor : float = 1.0
-
Anti-aliasing factor, 1 for no AA
- $bg_color : array<string|int, mixed> = [1, 1, 1, 0]
-
Image background color: array(r,g,b,a), 0 <= r,g,b,a <= 1
Return values
mixed —add_info()
Add meta information to the PDF
public
add_info(string $label, string $value) : mixed
Parameters
- $label : string
-
label of the value (Creator, Producer, etc.)
- $value : string
-
the text to set
Return values
mixed —add_link()
Add a link to the pdf
public
add_link(string $url, float $x, float $y, float $width, float $height) : mixed
Parameters
- $url : string
-
The url to link to
- $x : float
-
The x position of the link
- $y : float
-
The y position of the link
- $width : float
-
The width of the link
- $height : float
-
The height of the link
Return values
mixed —add_named_dest()
Add a named destination (similar to <a name="foo">...</a> in html)
public
add_named_dest(string $anchorname) : mixed
Parameters
- $anchorname : string
-
The name of the named destination
Return values
mixed —add_object()
public
add_object() : mixed
Return values
mixed —arc()
Add an arc to the PDF See {@link Style::munge_color()} for the format of the color array.
public
arc(float $x1, float $y1, float $r1, float $r2, float $astart, float $aend, array<string|int, mixed> $color, float $width[, array<string|int, mixed> $style = [] ]) : mixed
Parameters
- $x1 : float
- $y1 : float
- $r1 : float
- $r2 : float
- $astart : float
- $aend : float
- $color : array<string|int, mixed>
- $width : float
- $style : array<string|int, mixed> = []
Return values
mixed —circle()
Draws a circle at $x,$y with radius $r
public
circle(float $x, float $y, float $r, array<string|int, mixed> $color[, float $width = null ][, array<string|int, mixed> $style = null ][, bool $fill = false ]) : mixed
See for the format of the color array. See for a description of the $style parameter (aka dash)
Parameters
- $x : float
- $y : float
- $r : float
- $color : array<string|int, mixed>
- $width : float = null
- $style : array<string|int, mixed> = null
- $fill : bool = false
-
Fills the circle if true
Return values
mixed —clipping_end()
Ends the last clipping shape
public
clipping_end() : mixed
Return values
mixed —clipping_rectangle()
Starts a clipping rectangle at x1,y1 with width w and height h
public
clipping_rectangle(float $x1, float $y1, float $w, float $h) : mixed
Parameters
- $x1 : float
- $y1 : float
- $w : float
- $h : float
Return values
mixed —clipping_roundrectangle()
Starts a rounded clipping rectangle at x1,y1 with width w and height h
public
clipping_roundrectangle(mixed $x1, mixed $y1, mixed $w, mixed $h, mixed $rTL, mixed $rTR, mixed $rBR, mixed $rBL) : mixed
Parameters
- $x1 : mixed
- $y1 : mixed
- $w : mixed
- $h : mixed
- $rTL : mixed
- $rTR : mixed
- $rBR : mixed
- $rBL : mixed
Return values
mixed —close_object()
public
close_object() : mixed
Return values
mixed —filled_rectangle()
Draws a filled rectangle at x1,y1 with width w and height h
public
filled_rectangle(float $x1, float $y1, float $w, float $h, array<string|int, mixed> $color) : mixed
Parameters
- $x1 : float
- $y1 : float
- $w : float
- $h : float
- $color : array<string|int, mixed>
Return values
mixed —get_dompdf()
public
get_dompdf() : Dompdf
Return values
Dompdf —get_font_baseline()
Calculates font baseline, in points
public
get_font_baseline(string $font, float $size) : float
Parameters
- $font : string
- $size : float
Return values
float —get_font_height()
Calculates font height, in points
public
get_font_height(string $font, float $size) : float
Parameters
- $font : string
- $size : float
Return values
float —get_height()
Return the image's height in pixels
public
get_height() : float
Return values
float —get_image()
Return the GF image resource
public
get_image() : resource
Return values
resource —get_page_count()
Returns the total number of pages in the document
public
get_page_count() : int
Return values
int —get_page_number()
Returns the current page number
public
get_page_number() : int
Return values
int —get_text_width()
Calculates text size, in points
public
get_text_width(string $text, string $font, float $size[, float $word_spacing = 0.0 ][, float $char_spacing = 0.0 ]) : float
Parameters
- $text : string
-
the text to be sized
- $font : string
-
the desired font
- $size : float
-
the desired font size
- $word_spacing : float = 0.0
-
word spacing, if any
- $char_spacing : float = 0.0
-
char spacing, if any
Return values
float —get_ttf_file()
public
get_ttf_file( $font) : string
Parameters
Return values
string —get_width()
Return the image's width in pixels
public
get_width() : float
Return values
float —javascript()
public
javascript(mixed $code) : void
Parameters
- $code : mixed
Return values
void —line()
Draws a line from x1,y1 to x2,y2
public
line(float $x1, float $y1, float $x2, float $y2, array<string|int, mixed> $color, float $width[, array<string|int, mixed> $style = null ]) : mixed
See for the format of the color array. See for a description of the format of the $style parameter (aka dash).
Parameters
- $x1 : float
- $y1 : float
- $x2 : float
- $y2 : float
- $color : array<string|int, mixed>
- $width : float
- $style : array<string|int, mixed> = null
Return values
mixed —new_page()
Starts a new page
public
new_page() : mixed
Subsequent drawing operations will appear on the new page.
Return values
mixed —open_object()
public
open_object() : mixed
Return values
mixed —output()
Returns the image as a string.
public
output([array<string|int, mixed> $options = [] ]) : string
Parameters
- $options : array<string|int, mixed> = []
-
Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); 'page' => Number of the page to output (defaults to the first).
Return values
string —page_line()
public
page_line() : mixed
Return values
mixed —page_text()
Writes text at the specified x and y coordinates on every page
public
page_text(float $x, float $y, string $text, string $font, float $size[, array<string|int, mixed> $color = [0, 0, 0] ][, float $word_space = 0.0 ][, float $char_space = 0.0 ][, float $angle = 0.0 ]) : mixed
The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced with their current values.
Parameters
- $x : float
- $y : float
- $text : string
-
the text to write
- $font : string
-
the font file to use
- $size : float
-
the font size, in points
- $color : array<string|int, mixed> = [0, 0, 0]
- $word_space : float = 0.0
-
word spacing adjustment
- $char_space : float = 0.0
-
char spacing adjustment
- $angle : float = 0.0
-
angle to write the text at, measured CW starting from the x-axis
Return values
mixed —polygon()
Draws a polygon
public
polygon(array<string|int, mixed> $points, array<string|int, mixed> $color[, float $width = null ][, array<string|int, mixed> $style = null ][, bool $fill = false ]) : mixed
The polygon is formed by joining all the points stored in the $points
array. $points has the following structure:
array(0 => x1,
1 => y1,
2 => x2,
3 => y2,
...
);
See for the format of the color array. See for a description of the $style parameter (aka dash)
Parameters
- $points : array<string|int, mixed>
- $color : array<string|int, mixed>
- $width : float = null
- $style : array<string|int, mixed> = null
- $fill : bool = false
-
Fills the polygon if true
Return values
mixed —rectangle()
Draws a rectangle at x1,y1 with width w and height h
public
rectangle(float $x1, float $y1, float $w, float $h, array<string|int, mixed> $color, float $width[, array<string|int, mixed> $style = null ]) : mixed
See for the format of the color array. See for a description of the $style parameter (aka dash)
Parameters
- $x1 : float
- $y1 : float
- $w : float
- $h : float
- $color : array<string|int, mixed>
- $width : float
- $style : array<string|int, mixed> = null
Return values
mixed —restore()
Restore last state
public
restore() : mixed
Return values
mixed —rotate()
Rotate
public
rotate(float $angle, float $x, float $y) : mixed
Parameters
- $angle : float
- $x : float
- $y : float
Return values
mixed —save()
Save current state
public
save() : mixed
Return values
mixed —scale()
Scale
public
scale(float $s_x, float $s_y, float $x, float $y) : mixed
Parameters
- $s_x : float
- $s_y : float
- $x : float
- $y : float
Return values
mixed —set_default_view()
Sets the default view
public
set_default_view(string $view[, array<string|int, mixed> $options = [] ]) : void
Parameters
- $view : string
- $options : array<string|int, mixed> = []
Return values
void —set_opacity()
Sets the opacity
public
set_opacity(float $opacity[, string $mode = "Normal" ]) : mixed
Parameters
- $opacity : float
- $mode : string = "Normal"
Return values
mixed —set_page_count()
Sets the page count
public
set_page_count(int $count) : mixed
Parameters
- $count : int
Return values
mixed —set_page_number()
Sets the current page number
public
set_page_number(int $num) : mixed
Parameters
- $num : int
Return values
mixed —skew()
Skew
public
skew(float $angle_x, float $angle_y, float $x, float $y) : mixed
Parameters
- $angle_x : float
- $angle_y : float
- $x : float
- $y : float
Return values
mixed —stream()
Streams the image to the client.
public
stream(string $filename[, array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $filename : string
-
The filename to present to the client.
- $options : array<string|int, mixed> = []
-
Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); 'page' => Number of the page to output (defaults to the first); 'Attachment': 1 or 0 (default 1).
Return values
mixed —text()
Writes text at the specified x and y coordinates See {@link Style::munge_color()} for the format of the color array.
public
text(float $x, float $y, string $text, string $font, float $size[, array<string|int, mixed> $color = [0, 0, 0] ][, float $word_spacing = 0.0 ][, float $char_spacing = 0.0 ][, float $angle = 0.0 ]) : void
Parameters
- $x : float
- $y : float
- $text : string
-
the text to write
- $font : string
-
the font file to use
- $size : float
-
the font size, in points
- $color : array<string|int, mixed> = [0, 0, 0]
- $word_spacing : float = 0.0
-
word spacing adjustment
- $char_spacing : float = 0.0
- $angle : float = 0.0
-
Text angle
Return values
void —transform()
Transform
public
transform( $a, $b, $c, $d, $e, $f) : mixed
Parameters
Return values
mixed —translate()
Translate
public
translate(float $t_x, float $t_y) : mixed
Parameters
- $t_x : float
- $t_y : float
Return values
mixed —_allocate_color()
Allocate a new color. Allocate with GD as needed and store previously allocated colors in $this->_colors.
protected
_allocate_color(array<string|int, mixed> $color) : int
Parameters
- $color : array<string|int, mixed>
-
The new current color
Return values
int —The allocated color
_downscale()
Scales value down from the current canvas DPI to 72 DPI
protected
_downscale(float $length) : float
Parameters
- $length : float
Return values
float —_output()
Outputs the image stream directly.
protected
_output([array<string|int, mixed> $options = [] ]) : mixed
Parameters
- $options : array<string|int, mixed> = []
-
Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); 'page' => Number of the page to output (defaults to the first).
Return values
mixed —_upscale()
Scales value up to the current canvas DPI from 72 DPI
protected
_upscale(float $length) : float
Parameters
- $length : float
Return values
float —get_font_height_actual()
protected
get_font_height_actual(mixed $font, mixed $size) : mixed
Parameters
- $font : mixed
- $size : mixed