Documentation

Image
in package

Image

Includes access methods and data layer. Do not, I repeat, do not access private fields, or even try to access the database directly!

Tags
version
3.0.0
subpackage

core

copyright

CLOUDREXX CMS - CLOUDREXX AG

author

Reto Kohli reto.kohli@comvation.com

Table of Contents

Constants

ICON_CLEAR_IMAGE_SRC  = 'images/modules/hotelcard/clear_image.gif'
The icon URI for the "remove the current image" link
MAXIMUM_UPLOAD_FILE_SIZE  = 310000
Size limit in bytes for images being uploaded or stored
PATH_NO_IMAGE  = 'images/modules/hotelcard/no_image.gif'
The default "no image" URI
THUMBNAIL_SUFFIX  = '.thumb'
Thumbnail suffix

Properties

$arrAcceptedFiletype  : array<string|int, mixed>
Array of all file extensions accepted as image files
$filetype_key  : int
$height  : int
The image height
$id  : int
$imagetype_key  : int
$ord  : int
The ordinal number
$path  : string
$width  : int
The image width

Methods

__construct()  : mixed
Create an Image
autoSize()  : bool
Determine the Image dimensions
createThumbnail()  : bool
Create a thumbnail of a picture.
crop()  : bool
Create a cropped version of a picture.
delete()  : bool
Delete this object from the database.
deleteById()  : bool
Delete the Image objects selected by their ID and optional ordinal number from the database.
errorHandler()  : bool
Handle any error occurring in this class.
getArrayById()  : array<string|int, mixed>
Returns an array with all Images for the Image ID given.
getById()  : Image
Select an object by ID from the database.
getFileTypeKey()  : int
Get the file type key
getFromSessionByKey()  : Image
Returns the image data stored in the session for the given key
getFromSessionByName()  : Image
Returns the image data stored in the session for the given name
getHeight()  : int
Get the height
getId()  : int
Get the ID
getImageTypeKey()  : int
Get the type ID
getJpegPath()  : string
Takes an image path and returns the corresponding jpeg format file name
getNextOrd()  : int
Returns the greatest ordinal value plus one for the image ID
getOrd()  : int
Get the ordinal number
getPath()  : string
Get the path
getScaledSize()  : array<string|int, mixed>
Returns a scaled image size array
getSizeArray()  : array<string|int, mixed>
Returns an array with the size of the Image
getSizeArrayThumbnail()  : array<string|int, mixed>
Returns an array with the size of the Image thumbnail
getThumbnailPath()  : string
Takes an image path and returns the corresponding thumbnail file name
getWidth()  : int
Get the width
insert()  : int
Insert this object into the database.
load()  : resource
Load the image from the given file path
makeClone()  : mixed
Clone the object
makeThumbnail()  : bool
Create a new thumbnail for the image object
processPostFiles()  : int
Stores any image files from a post request
recordExists()  : bool
Test whether a record with the ID and ordinal number of this object is already present in the database.
replace()  : bool
Replace the image path for the given object ID and ordinal number.
resize()  : bool
Creates both a new version of the Image and a new thumbnail according to its Imagetype
saveJpeg()  : bool
Saves the image to the path given as a jpeg file
scale()  : bool
Create a scaled version of a picture.
setFileTypeKey()  : mixed
Set the file type key
setHeight()  : mixed
Set the height
setImageTypeKey()  : mixed
Set the type ID
setOrd()  : mixed
Set the ordinal number
setPath()  : mixed
Set the path
setWidth()  : mixed
Set the width
store()  : bool
Stores the object in the database.
update()  : int
Update this object in the database.
uploadAndStore()  : int
Uploads an image file and stores its information in the database

Constants

ICON_CLEAR_IMAGE_SRC

The icon URI for the "remove the current image" link

public mixed ICON_CLEAR_IMAGE_SRC = 'images/modules/hotelcard/clear_image.gif'

MAXIMUM_UPLOAD_FILE_SIZE

Size limit in bytes for images being uploaded or stored

public mixed MAXIMUM_UPLOAD_FILE_SIZE = 310000

A little over 300 KB is okay.

PATH_NO_IMAGE

The default "no image" URI

public mixed PATH_NO_IMAGE = 'images/modules/hotelcard/no_image.gif'

THUMBNAIL_SUFFIX

Thumbnail suffix

public mixed THUMBNAIL_SUFFIX = '.thumb'

Properties

$arrAcceptedFiletype

Array of all file extensions accepted as image files

private static array<string|int, mixed> $arrAcceptedFiletype = array('gif', 'jpg', 'png', 'webp')

$filetype_key

private int $filetype_key = \false

The file type key

Tags
access

private

$height

The image height

private int $height = \false

The image height

Tags
access

private

$id

private int $id = \false

The object ID, PRIMARY

Tags
access

private

$imagetype_key

private int $imagetype_key = \false

The image type key

Tags
access

private

$ord

The ordinal number

private int $ord = 0

The ordinal number, PRIMARY

Tags
access

private

$path

private string $path = \false

The image file path

Tags
access

private

$width

The image width

private int $width = \false

The image width

Tags
access

private

Methods

__construct()

Create an Image

public __construct([int $ord = 0 ][, int $image_id = 0 ]) : mixed

Note that the optional $image_id argument SHOULD NOT be used when adding the first Image to another object, but only to ensure that additional Images with different ordinals are added to the same ID.

Parameters
$ord : int = 0

The ordinal number

$image_id : int = 0

The optional Image ID

Tags
access

public

author

Reto Kohli reto.kohli@comvation.com

autoSize()

Determine the Image dimensions

public autoSize() : bool
Return values
bool

True on success, false otherwise

createThumbnail()

Create a thumbnail of a picture.

public static createThumbnail(string $image_path[, int $maxWidth = 160 ][, int $maxHeight = 120 ][, int $quality = 90 ]) : bool

Both the width and height of the thumbnail may be specified; the picture will still be scaled to fit within the given sizes while keeping the original width/height ratio. In addition to that, this method tries to delete an existing thumbnail before attempting to write the new one. Note that thumbnails are always created as jpeg image files!

Parameters
$image_path : string

The image file path

$maxWidth : int = 160

The maximum width of the thumbnail

$maxHeight : int = 120

The maximum height of the thumbnail

$quality : int = 90

The desired jpeg thumbnail quality

Tags
static
Return values
bool

True on success, false otherwise.

crop()

Create a cropped version of a picture.

public static crop(string $source_path, string $target_path, int $x1, int $y1, int $x2, int $y2[, bool $force = false ][, int $quality = 90 ]) : bool

Note that cropped images are always created as jpeg image files!

Parameters
$source_path : string

The source image file path

$target_path : string

The target image file path

$x1 : int

The left border of the cropped image

$y1 : int

The top border of the cropped image

$x2 : int

The right border of the cropped image

$y2 : int

The bottom border of the cropped image

$force : bool = false

If true, the target image is forced to be overwritten

$quality : int = 90

The desired jpeg thumbnail quality

Tags
static
Return values
bool

True on success, false otherwise.

delete()

Delete this object from the database.

public delete([bool $delete_files = false ]) : bool

If the $delete_files parameter is true, the file and thumbnail will be deleted as well

Parameters
$delete_files : bool = false

If true, the files are deleted, too

Tags
global

ADOConnection $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True on success, false otherwise

deleteById()

Delete the Image objects selected by their ID and optional ordinal number from the database.

public static deleteById(int $image_id[, mixed $ord = false ]) : bool

If you don't specify an ordinal number, this method will delete any Image records with that ID. Otherwise, only the selected Image will be removed. Deletes any associated files along with the database records. Returns true if the Image ID specified is empty.

Parameters
$image_id : int

The Image ID

$ord : mixed = false

The optional ordinal number

Tags
todo

Existing thumbnails are deleted along with them.

static
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True on success, false otherwise

errorHandler()

Handle any error occurring in this class.

public errorHandler() : bool

Tries to fix known problems with the database table.

Tags
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
bool

False. Always.

getArrayById()

Returns an array with all Images for the Image ID given.

public static getArrayById(int $image_id[, bool $thumbnail = false ][, int $key = false ]) : array<string|int, mixed>

The array is indexed by the ordinal numbers. If more than one image is found, the array is sorted by those in ascending order. The result may be limited by specifying the $key parameter. False values are ignored. The returned array looks like array( ord => Image, ... more ... )

Parameters
$image_id : int

The Image ID

$thumbnail : bool = false

If true, thumbnail versions are returned. Defaults to false

$key : int = false

The optional key

Tags
static
author

Reto Kohli reto.kohli@comvation.com

Return values
array<string|int, mixed>

The Image array on success, false otherwise

getById()

Select an object by ID from the database.

public static getById(mixed $image_id[, int $ord = 0 ][, bool $thumbnail = false ]) : Image
Parameters
$image_id : mixed
$ord : int = 0

The optional ordinal number, defaults to zero

$thumbnail : bool = false

If true, thumbnail versions are returned. Defaults to false

Tags
static
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
Image

The object on success, false otherwise

getFromSessionByKey()

Returns the image data stored in the session for the given key

public static getFromSessionByKey(string $key) : Image

If no such image is present, returns an image created from the default path given, if any. If the given default image does not exist, returns the Image class default Image. If that fails, too, returns false

Parameters
$key : string

The image key

Return values
Image

The default Image

getFromSessionByName()

Returns the image data stored in the session for the given name

public static getFromSessionByName(string $name) : Image

If no such image is present, returns an image created from the default path given, if any. If the given default image does not exist, returns the Image class default Image. If that fails, too, returns false.

Parameters
$name : string

The image name

Return values
Image

The Image

getJpegPath()

Takes an image path and returns the corresponding jpeg format file name

public static getJpegPath(string $image_path) : string

If the path belongs to a jpeg file already, it is returned unchanged. Note that any rescaled images are created as jpeg image files!

Parameters
$image_path : string

The original image path

Return values
string

The thumbnail image path

getNextOrd()

Returns the greatest ordinal value plus one for the image ID

public static getNextOrd(int $image_id[, int $ord_min = 0 ]) : int

If there is no matching one yet, returns 0. If $ord_min is specified and larger than the highest value found, it is returned instead.

Parameters
$image_id : int

The optional image ID

$ord_min : int = 0

The optional minimum ordinal value

Return values
int

The next ordinal number on success, false otherwise

getPath()

Get the path

public getPath() : string

Note that the path is stored relative to ASCMS_DOCUMENT_ROOT, with ASCMS_PATH_OFFSET, any path separator following it, and everything before that cut off!

Tags
author

Reto Kohli reto.kohli@comvation.com

Return values
string

The path

getScaledSize()

Returns a scaled image size array

public static getScaledSize(array<string|int, mixed> $size, int $maxwidth, int $maxheight) : array<string|int, mixed>

Uses the same indices for width (0), height (1), and the height/width text string (3) as getimagesize(). One of $maxwidth and $maxheight may be zero, in which case it is ignored. The size is then calculated to fit the other while maintaining the ratio. If both are zero, the original $size is returned unchanged.

Parameters
$size : array<string|int, mixed>

The Image size array

$maxwidth : int

The maximum width

$maxheight : int

The maximum height

Return values
array<string|int, mixed>

The scaled size array

getSizeArray()

Returns an array with the size of the Image

public getSizeArray() : array<string|int, mixed>

Uses the same indices for width (0), height (1), and the height/width text string (3) as getimagesize().

Return values
array<string|int, mixed>

The Image size

getSizeArrayThumbnail()

Returns an array with the size of the Image thumbnail

public getSizeArrayThumbnail() : array<string|int, mixed>

Uses the same indices for width (0), height (1), and the height/width text string (3) as getimagesize().

Return values
array<string|int, mixed>

The Image thumbnail size

getThumbnailPath()

Takes an image path and returns the corresponding thumbnail file name

public static getThumbnailPath(string $image_path) : string

If the path belongs to a thumbnail already, it is returned unchanged. Note that any thumbnails are created as jpeg image files!

Parameters
$image_path : string

The original image path

Return values
string

The thumbnail image path

insert()

Insert this object into the database.

public insert() : int

If the ordinal value is false or negative, it is fixed to the result of getNextOrd().

Tags
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
int

The Image ID on success, false otherwise

load()

Load the image from the given file path

public static load(string $file_path) : resource

Based on the ImageManager methods _imageCreateFromFile() and _isImage()

Parameters
$file_path : string

The image file path

Return values
resource

The image resource on success, false otherwise

makeClone()

Clone the object

public makeClone() : mixed

Note that this does NOT create a copy in any way, but simply clears the object ID. Upon storing this object, a new ID is created.

Tags
author

Reto Kohli reto.kohli@comvation.com

makeThumbnail()

Create a new thumbnail for the image object

public makeThumbnail() : bool

This uses this Images' key to determine the Imagetype and its default thumbnail size.

Tags
author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True on success, false otherwise.

processPostFiles()

Stores any image files from a post request

public static processPostFiles(string $target_folder_path) : int

The files may be uploaded or chosen in the file browser. Each file is moved to the given target path with a uniquid() prepended to the file name; the original name is stored temporarily only in the session. If possible, the corresponding Image object is updated. If not, a new one is created and stored. The original file name, type, Image ID and ordinal value are stored in the session under $_SESSION['image'][<field_name>].

Parameters
$target_folder_path : string

The target folder path for uploaded images only

Return values
int

The Image ID if all images have been processed successfully, false if errors occurred, or the empty string if nothing changed

recordExists()

Test whether a record with the ID and ordinal number of this object is already present in the database.

public recordExists() : bool
Tags
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True if the record exists, false otherwise

replace()

Replace the image path for the given object ID and ordinal number.

public replace(int $image_id, int $ord, string $path[, int $imagetype_key = '' ][, int $width = false ][, mixed $height = false ]) : bool

If no object with that ID and ordinal can be found, creates a new one. In that case, the $imagetype_key parameter must be non-empty.

Parameters
$image_id : int

The object ID

$ord : int

The optional height, overrides automatic value

$path : string

The path

$imagetype_key : int = ''

The image type key

$width : int = false

The optional width, overrides automatic value

$height : mixed = false
Return values
bool

True on success, false otherwise

resize()

Creates both a new version of the Image and a new thumbnail according to its Imagetype

public resize() : bool

Note that zero or NULL values are ignored.

Tags
author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True on success, false otherwise.

saveJpeg()

Saves the image to the path given as a jpeg file

public saveJpeg(mixed $image, mixed $path[, mixed $quality = 90 ][, booelan $force = false ]) : bool
Parameters
$image : mixed
$path : mixed
$quality : mixed = 90
$force : booelan = false

Force overwriting existing files if true

Tags
access

public

Return values
bool

True on success, false otherwise

scale()

Create a scaled version of a picture.

public static scale(string $source_path, string $target_path[, bool $force = false ][, int $maxWidth = 160 ][, int $maxHeight = 120 ][, int $quality = 90 ]) : bool

Both the width and height may be specified; the picture will still be scaled to fit within the given sizes while keeping the original width/height ratio. If $force is true, this method tries to delete an existing target image before attempting to write the new one. Note that scaled images are always created as jpeg image files!

Parameters
$source_path : string

The source image file path

$target_path : string

The target image file path

$force : bool = false

If true, the target image is forced to be overwritten

$maxWidth : int = 160

The maximum width of the image

$maxHeight : int = 120

The maximum height of the image

$quality : int = 90

The desired jpeg thumbnail quality

Tags
static
Return values
bool

True on success, false otherwise.

setFileTypeKey()

Set the file type key

public setFileTypeKey(int $filetype_key) : mixed

Any non-positive value or string will be interpreted as NULL

Parameters
$filetype_key : int

The file type key

Tags
author

Reto Kohli reto.kohli@comvation.com

setImageTypeKey()

Set the type ID

public setImageTypeKey(int $imagetype_key) : mixed

Any non-positive value or string will be interpreted as NULL

Parameters
$imagetype_key : int

The type ID

Tags
author

Reto Kohli reto.kohli@comvation.com

setOrd()

Set the ordinal number

public setOrd(int $ord) : mixed

Note that this value is non-negative, negative numbers have their sign stripped here.

Parameters
$ord : int

The ordinal number

Tags
author

Reto Kohli reto.kohli@comvation.com

setPath()

Set the path

public setPath(string $path) : mixed

Note that the path is stored relative to ASCMS_DOCUMENT_ROOT, with ASCMS_PATH_OFFSET, any path separator following it, and everything before that cut off!

Parameters
$path : string

The path

Tags
author

Reto Kohli reto.kohli@comvation.com

store()

Stores the object in the database.

public store() : bool

Either updates or inserts the object, depending on the outcome of the call to .

Tags
author

Reto Kohli reto.kohli@comvation.com

Return values
bool

True on success, false otherwise

update()

Update this object in the database.

public update() : int
Tags
global

mixed $objDatabase Database object

author

Reto Kohli reto.kohli@comvation.com

Return values
int

The Image ID on success, false otherwise

uploadAndStore()

Uploads an image file and stores its information in the database

public static uploadAndStore(string $upload_field_name, string &$target_path[, mixed $image_id = false ][, mixed $imagetype_key = false ][, mixed $ord = false ]) : int
Parameters
$upload_field_name : string

File input field name

$target_path : string

Target path, relative to the document root, including the file name

$image_id : mixed = false
$imagetype_key : mixed = false
$ord : mixed = false
Tags
author

Reto Kohli reto.kohli@comvation.com

Return values
int

The new image ID on success, false otherwise


        
On this page

Search results