public static function load($filename, $allow_optimisations = true, array $preferred = array('imagick', 'gd', 'native'))
{
/* Fail early if file is not readble */
if (!file_exists($filename) || !is_readable($filename)) {
throw new Exception("File '{$filename}' does not exist, or is not readable.");
}
$ext = pathinfo($filename, PATHINFO_EXTENSION);
/* Choose the first implementation which can handle this format */
foreach ($preferred as $implemetnation) {
if ($implemetnation === 'imagick') {
if (!self::isImagickLoaded()) {
// Skip option if Imagick is not loaded
continue;
}
return new \Mike42\Escpos\ImagickEscposImage($filename, $allow_optimisations);
} elseif ($implemetnation === 'gd') {
if (!self::isGdLoaded()) {
// Skip option if GD not loaded
continue;
}
return new \Mike42\Escpos\GdEscposImage($filename, $allow_optimisations);
} elseif ($implemetnation === 'native') {
if (!in_array($ext, array('wbmp', 'pbm', 'bmp'))) {
// Pure PHP is fastest way to generate raster output from wbmp and pbm formats.
continue;
}
return new \Mike42\Escpos\NativeEscposImage($filename, $allow_optimisations);
} else {
// Something else on the 'preferred' list.
throw new InvalidArgumentException("'{$implemetnation}' is not a known EscposImage implementation");
}
}
throw new InvalidArgumentException("No suitable EscposImage implementation found for '{$filename}'.");
}