class ImageResource (View source)

An object representation of GD's native image resources.

Note: Only the functionality needed has been implemented in this class.

Properties

protected resource $resource
protected Type $type
protected Info $info
static protected int $quality
static protected bool $normalizeJpegOrientation

Methods

__construct(resource $resource, Type $type = null, Info $info = null)

Constructor.

static ImageResource
createFromFile(string $file)

Creates an ImageResource from a file.

static ImageResource
createFromString(string $data)

Creates an ImageResource from a string of image data.

static ImageResource
createNew(Dimensions $dimensions, Type $type)

Creates a new image given the width and height.

resource
getResource()

Returns the GD resource.

Dimensions
getDimensions()

Returns the image dimensions.

Type
getType()

Returns the image type.

Info
getInfo()

Returns the image's info.

Exif
getExif()

No description

allocateColor(int $red, int $green, int $blue, int|null $alpha = null)

Allocate a color for an image.

allocateTransparentColor()

Allocate a transparent color for an image.

getColorAt(Point $point)

Returns the color at a point.

fill(Color $color, Point $startPoint = null)

Flood fill.

resample(Point $destPoint, Point $srcPoint, Dimensions $destDimensions, Dimensions $srcDimensions, ImageResource $dest = null)

Resize part of an image with resampling.

flip(string $mode)

Flips the image.

rotate(string $angle)

Rotates the image.

toFile(string $file)

Writes the image to a file.

string
toString()

Returns the image as a data string.

__toString()

No description

__clone()

No description

__destroy()

No description

static boolean
isJpegOrientationNormalized()

Returns whether JPEG orientation is normalized or not.

static 
setNormalizeJpegOrientation(boolean $normalizeJpegOrientation)

Enable or disable JPEG orientation normalization.

static 
setQuality(int $quality)

Sets the quality setting.

static int
getQuality()

Returns the quality setting.

static int
convertJpegQualityToPngCompression(int $quality)

Convert JPEG quality scale to PNG compression scale.

static int
convertPngCompressionToJpegQuality(int $compression)

Convert PNG compression scale to JPEG quality scale.

normalizeJpegOrientation()

If orientation in EXIF data is not normal, flip and/or rotate image until it is correct

verifyColor(Color $color)

Verifies that a color is allocated.

resetInfo()

If image changes, info needs to be recreated

Details

__construct(resource $resource, Type $type = null, Info $info = null)

Constructor.

Either type or info need to be provided.

Parameters

resource $resource A GD resource
Type $type Image type
Info $info Image info

static ImageResource createFromFile(string $file)

Creates an ImageResource from a file.

Parameters

string $file A filepath

Return Value

ImageResource

static ImageResource createFromString(string $data)

Creates an ImageResource from a string of image data.

Parameters

string $data A string containing the image data

Return Value

ImageResource

static ImageResource createNew(Dimensions $dimensions, Type $type)

Creates a new image given the width and height.

Parameters

Dimensions $dimensions Image dimensions
Type $type Type of image

Return Value

ImageResource

resource getResource()

Returns the GD resource.

Return Value

resource

Dimensions getDimensions()

Returns the image dimensions.

Return Value

Dimensions

Type getType()

Returns the image type.

Return Value

Type

Info getInfo()

Returns the image's info.

Return Value

Info

Exif getExif()

Return Value

Exif

Color allocateColor(int $red, int $green, int $blue, int|null $alpha = null)

Allocate a color for an image.

Parameters

int $red Value of red component (between 0 and 255)
int $green Value of green component (between 0 and 255)
int $blue Value of blue component (between 0 and 255)
int|null $alpha Optional value of alpha component (between 0 and 127). 0 = opaque, 127 = transparent.

Return Value

Color

Color allocateTransparentColor()

Allocate a transparent color for an image.

Return Value

Color

Color getColorAt(Point $point)

Returns the color at a point.

Parameters

Point $point

Return Value

Color

ImageResource fill(Color $color, Point $startPoint = null)

Flood fill.

Parameters

Color $color The fill color
Point $startPoint The point to start at

Return Value

ImageResource This image

ImageResource resample(Point $destPoint, Point $srcPoint, Dimensions $destDimensions, Dimensions $srcDimensions, ImageResource $dest = null)

Resize part of an image with resampling.

Parameters

Point $destPoint The destination point
Point $srcPoint The source point
Dimensions $destDimensions The destination dimensions
Dimensions $srcDimensions The source dimensions
ImageResource $dest Optional destination image. Default is current image.

Return Value

ImageResource This image

ImageResource flip(string $mode)

Flips the image.

Based on http://stackoverflow.com/a/10001884/1136593 Thanks Jon Grant

Parameters

string $mode ('V' = vertical, 'H' = horizontal, 'HV' = both)

Return Value

ImageResource This image

ImageResource rotate(string $angle)

Rotates the image.

Parameters

string $angle ('L' = -90°, 'R' = +90°, 'T' = 180°)

Return Value

ImageResource This image

toFile(string $file)

Writes the image to a file.

Parameters

string $file

string toString()

Returns the image as a data string.

Return Value

string

__toString()

__clone()

__destroy()

static boolean isJpegOrientationNormalized()

Returns whether JPEG orientation is normalized or not.

Return Value

boolean

static setNormalizeJpegOrientation(boolean $normalizeJpegOrientation)

Enable or disable JPEG orientation normalization.

Parameters

boolean $normalizeJpegOrientation

static setQuality(int $quality)

Sets the quality setting.

Note: A quality < 10 is assumed to be PNG compression scale.

Parameters

int $quality Between 0 and 100

static int getQuality()

Returns the quality setting.

Return Value

int

static protected int convertJpegQualityToPngCompression(int $quality)

Convert JPEG quality scale to PNG compression scale.

JPEG: 0 (worst) - 100 (best). PNG: 0 (best) - 10 (worst)

Parameters

int $quality

Return Value

int

static protected int convertPngCompressionToJpegQuality(int $compression)

Convert PNG compression scale to JPEG quality scale.

Parameters

int $compression

Return Value

int

protected normalizeJpegOrientation()

If orientation in EXIF data is not normal, flip and/or rotate image until it is correct

protected Color verifyColor(Color $color)

Verifies that a color is allocated.

Parameters

Color $color

Return Value

Color

protected resetInfo()

If image changes, info needs to be recreated