Nextcloud PHP API (master)

IPreview

Public interface for discovering preview providers and generating file previews.

ProviderClosure lazily resolves a preview provider and may return false if it cannot be loaded.

Tags
since
6.0.0
psalm-type

ProviderClosure = Closure():(IProviderV2|false)

Attributes
#[Consumable]
$since: '6.0.0'

Table of Contents

Constants

MODE_COVER  : mixed = 'cover'
Preview scaling mode that covers the requested dimensions.
MODE_FILL  : mixed = 'fill'
Preview scaling mode that fills the requested dimensions.

Methods

generatePreviews()  : ISimpleFile
Generates previews of a file
getPreview()  : ISimpleFile
Returns a preview of a file
getProviders()  : array<string, array<int, ProviderClosure>>
Returns the registered preview providers grouped by supported mime-type regex.
hasProviders()  : bool
Does the manager have any providers
isAvailable()  : bool
Returns whether a preview can currently be generated for the given file.
isMimeSupported()  : bool
Returns whether any registered preview provider supports the given mime type.

Constants

MODE_COVER

Preview scaling mode that covers the requested dimensions.

public mixed MODE_COVER = 'cover'
Tags
since
11.0.0

MODE_FILL

Preview scaling mode that fills the requested dimensions.

public mixed MODE_FILL = 'fill'
Tags
since
11.0.0

Methods

generatePreviews()

Generates previews of a file

public generatePreviews(File $file, array<string|int, mixed> $specifications[, string|null $mimeType = null ]) : ISimpleFile
Parameters
$file : File
$specifications : array<string|int, mixed>
$mimeType : string|null = null
Tags
throws
NotFoundException
InvalidArgumentException

if the preview would be invalid (in case the original image is invalid)

since
19.0.0
Return values
ISimpleFile

the last preview that was generated

getPreview()

Returns a preview of a file

public getPreview(File $file[, int $width = -1 ][, int $height = -1 ][, bool $crop = false ][, IPreview::MODE_* $mode = IPreview::MODE_FILL ][, string $mimeType = null ][, bool $cacheResult = true ]) : ISimpleFile

The cache is searched first and if nothing usable was found then a preview is generated by one of the providers

Parameters
$file : File
$width : int = -1
$height : int = -1
$crop : bool = false
$mode : IPreview::MODE_* = IPreview::MODE_FILL
$mimeType : string = null

To force a given mimetype for the file (files_versions needs this)

$cacheResult : bool = true

Whether to cache the preview on the filesystem. Default to true. Can be useful to set to false to limit the amount of stored previews.

Tags
throws
NotFoundException
InvalidArgumentException

if the preview would be invalid (in case the original image is invalid)

since
11.0.0
  • \InvalidArgumentException was added in 12.0.0
32.0.0
  • getPreview($cacheResult) added the $cacheResult argument to the signature
Return values
ISimpleFile

getProviders()

Returns the registered preview providers grouped by supported mime-type regex.

public getProviders() : array<string, array<int, ProviderClosure>>

Each value is a list of closures that create preview providers when invoked, not a list of already instantiated provider objects.

The provider list is built on demand and is empty when previews are disabled.

Tags
since
8.1.0
Return values
array<string, array<int, ProviderClosure>>

Map of mime-type regex to provider-creating closures

hasProviders()

Does the manager have any providers

public hasProviders() : bool
Tags
since
8.1.0
Return values
bool

isAvailable()

Returns whether a preview can currently be generated for the given file.

public isAvailable(FileInfo $file[, string|null $mimeType = null ]) : bool

This checks whether the effective mime type is supported, whether previews are allowed on the file's mount, and whether at least one matching provider is available for the file in the current environment.

Parameters
$file : FileInfo
$mimeType : string|null = null

Optional mime type override to use instead of $file->getMimeType()

Tags
since
8.0.0
32.0.0
  • isAvailable($mimeType) added the $mimeType argument to the signature
Return values
bool

isMimeSupported()

Returns whether any registered preview provider supports the given mime type.

public isMimeSupported([string $mimeType = '*' ]) : bool

This checks the mime type against the providers' registered mime-type regexes. It does not guarantee that preview generation will succeed for a specific file.

Parameters
$mimeType : string = '*'

Mime type string to test, for example "image/png"

Tags
since
6.0.0
Return values
bool
On this page

Search results