IsImage
extends Base
in package
Validates image data, supporting base64 encoded images with optional MIME type and size constraints.
Table of Contents
Properties
- $allowedMimeTypes : array<string|int, mixed>
- $allowSvg : bool
- $checkImageData : bool
- $maxSize : int|null
Methods
- __construct() : mixed
- isValid() : bool
- Returns true if validation is successful
- validate() : bool
- Abstract method to be implemented by all validators.
- detectImageType() : string|null
- Detects image type from binary data signatures.
- detectSvg() : string|null
- Detects if the data is a valid SVG image.
- isMimeTypeAllowed() : bool
- Checks if a MIME type is in the allowed list (case-insensitive per RFC 2045).
- isRecognizableImage() : bool
- Checks if the decoded data is a recognizable image format.
- validateBase64Image() : bool
- Validates base64 encoded image data.
- validateDataUri() : bool
- Validates a data URI formatted image.
Properties
$allowedMimeTypes
private
array<string|int, mixed>
$allowedMimeTypes
$allowSvg
private
bool
$allowSvg
$checkImageData
private
bool
$checkImageData
$maxSize
private
int|null
$maxSize
Methods
__construct()
public
__construct([array<string|int, mixed> $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'] ][, int|null $maxSize = null ][, bool $checkImageData = true ][, bool $allowSvg = false ]) : mixed
Parameters
- $allowedMimeTypes : array<string|int, mixed> = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
-
List of allowed MIME types (e.g., ['image/jpeg', 'image/png'])
- $maxSize : int|null = null
-
Maximum file size in bytes (null for no limit)
- $checkImageData : bool = true
-
Whether to validate the actual image data (requires decoding)
- $allowSvg : bool = false
-
Whether to allow SVG images (default: false for security - SVG can contain scripts)
isValid()
Returns true if validation is successful
public
isValid(mixed $value) : bool
Parameters
- $value : mixed
Return values
boolvalidate()
Abstract method to be implemented by all validators.
protected
validate(mixed $value) : bool
Parameters
- $value : mixed
Return values
booldetectImageType()
Detects image type from binary data signatures.
private
detectImageType(string $imageData) : string|null
Parameters
- $imageData : string
Return values
string|null —Returns MIME type if detected, null otherwise
detectSvg()
Detects if the data is a valid SVG image.
private
detectSvg(string $imageData) : string|null
SVG detection is more permissive but requires explicit opt-in for security.
Parameters
- $imageData : string
Return values
string|null —Returns 'image/svg+xml' if valid SVG, null otherwise
isMimeTypeAllowed()
Checks if a MIME type is in the allowed list (case-insensitive per RFC 2045).
private
isMimeTypeAllowed(string $mimeType) : bool
Parameters
- $mimeType : string
Return values
boolisRecognizableImage()
Checks if the decoded data is a recognizable image format.
private
isRecognizableImage(string $imageData) : bool
This method only validates that the data contains valid image signatures, without checking MIME type restrictions (caller's responsibility).
Parameters
- $imageData : string
Return values
boolvalidateBase64Image()
Validates base64 encoded image data.
private
validateBase64Image(string $base64Data) : bool
Parameters
- $base64Data : string
Return values
boolvalidateDataUri()
Validates a data URI formatted image.
private
validateDataUri(string $dataUri) : bool
Parameters
- $dataUri : string