public static load ( string $filename, string $allow_optimisations = true, array $preferred = ['imagick', 'gd', 'native'] ) : |
||
$filename | string | File to load from |
$allow_optimisations | string | True to allow the fastest rendering shortcuts, false to force the library to read the image into an internal raster format and use PHP to render the image (slower but less fragile). |
$preferred | array | Order to try to load libraries in- escpos-php supports pluggable image libraries. Items can be 'imagick', 'gd', 'native'. |
return |
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}'.");
}
<?php /* Print-outs using the newer graphics print command */ require __DIR__ . '/../autoload.php'; use Mike42\Escpos\Printer; use Mike42\Escpos\EscposImage; use Mike42\Escpos\PrintConnectors\FilePrintConnector; $connector = new FilePrintConnector("php://stdout"); $printer = new Printer($connector); try { $tux = EscposImage::load("resources/tux.png", false); $printer->graphics($tux); $printer->text("Regular Tux.\n"); $printer->feed(); $printer->graphics($tux, Printer::IMG_DOUBLE_WIDTH); $printer->text("Wide Tux.\n"); $printer->feed(); $printer->graphics($tux, Printer::IMG_DOUBLE_HEIGHT); $printer->text("Tall Tux.\n"); $printer->feed(); $printer->graphics($tux, Printer::IMG_DOUBLE_WIDTH | Printer::IMG_DOUBLE_HEIGHT); $printer->text("Large Tux in correct proportion.\n"); $printer->cut(); } catch (Exception $e) { /* Images not supported on your PHP, or image file not found */ $printer->text($e->getMessage() . "\n"); } $printer->close();