Documentation

FileConvert
in package

Converter between various file types

For all features to work, the following programs need to be installed:

  • convert (e.g. from the graphicsmagick-imagemagick-compat Debian package)
  • exiftool (e.g. from the libimage-exiftool-perl Debian package)
  • libreoffice (e.g. from the libreoffice-common Debian package)

Table of Contents

Constants

IMAGICK_OPTIONS  : mixed = ['flatten' => true, 'background' => 'white', 'c...

Methods

getPageCount()  : int
Use 'exiftool' to determine the number of pages in a file
imagemagick()  : string
Convert file using ImageMagick
installed()  : bool
Checks to see that a conversion program is installed
libreoffice()  : string
Convert file using LibreOffice
pdftocairo()  : string
Convert file using PDF to Cairo Needs package 'poppler-utils'
validateExtension()  : mixed
Validates that an output file extension is of a valid format

Constants

IMAGICK_OPTIONS

public mixed IMAGICK_OPTIONS = ['flatten' => true, 'background' => 'white', 'colorspace' => 'sRGB', 'depth' => 8, 'density' => 400, 'quality' => '100%', 'resize' => '50%']

Methods

getPageCount()

Use 'exiftool' to determine the number of pages in a file

public static getPageCount(string $filename) : int
Parameters
$filename : string

Server filename

Tags
throws
RuntimeException

exiftool isn't installed/accessible to PHP

FileConversionException

exiftool was unable to determine the page count

Return values
int —

Number of pages

imagemagick()

Convert file using ImageMagick

public static imagemagick(string $in_file, string $out_ext[, int $page_index = 0 ][, int|array<string|int, mixed> $options = self::IMAGICK_OPTIONS ]) : string
Parameters
$in_file : string

Input filename, with full path

$out_ext : string

Extension to convert file to, e.g. "png", "jpg".

$page_index : int = 0

Page number of document, 0-based (applies to PDFs and other page-based documents)

$options : int|array<string|int, mixed> = self::IMAGICK_OPTIONS

DPI or array of options

Tags
throws
InvalidArgumentException

The $out_ext argument has an invalid format

RuntimeException

ImageMagick isn't installed/accessible to PHP

FileConversionException

ImageMagick failed to convert the file

Return values
string —

Destination file in temp dir

installed()

Checks to see that a conversion program is installed

public static installed(string $program) : bool
Parameters
$program : string

The program name; 'libreoffice', 'convert' (i.e. ImageMagick), 'exiftool'

Return values
bool —

True if the program is installed

libreoffice()

Convert file using LibreOffice

public static libreoffice(string $in_file, string $out_ext) : string
Parameters
$in_file : string

Input filename, with full path

$out_ext : string

Extension to convert file to, e.g. "pdf", "jpg", "txt". Note that spreadsheets cannot be directly converted to images, but can be converted to PDFs which can then be converted using FileConvert::imagemagick

Tags
throws
InvalidArgumentException

The $out_ext argument has an invalid format

RuntimeException

LibreOffice isn't installed/accessible to PHP

FileConversionException

LibreOffice failed to convert the file

Return values
string —

Destination file in temp dir

pdftocairo()

Convert file using PDF to Cairo Needs package 'poppler-utils'

public static pdftocairo(string $in_file, string $out_ext[, int $page_index = 1 ][, int $density = 300 ]) : string
Parameters
$in_file : string

Input filename, with full path

$out_ext : string

Extension to convert file to, e.g. "png", "jpg".

$page_index : int = 1

Page number of document, 0-based (applies to PDFs and other page-based documents)

$density : int = 300

DPI

Tags
throws
InvalidArgumentException

The $out_ext argument has an invalid format

RuntimeException

ImageMagick isn't installed/accessible to PHP

FileConversionException

ImageMagick failed to convert the file

Return values
string —

Destination file in temp dir

validateExtension()

Validates that an output file extension is of a valid format

private static validateExtension(string $ext) : mixed
Parameters
$ext : string

The output extension

Tags
throws
InvalidArgumentException

IF the extension is invalid

On this page

Search results