Files
dolibarr/htdocs/includes/mike42/escpos-php/example/specific/54-gfx-sidebyside.php
2015-11-11 18:02:47 +01:00

59 lines
1.9 KiB
PHP

<?php
/*
* Example of calling ImageMagick 'convert' to manipulate an image prior to printing.
*
* Written as an example to remind you to do four things-
* - escape your command-line arguments with escapeshellarg
* - close the printer
* - delete any temp files
* - detect and handle external command failure
*
* Note that image operations are slow. You can and should serialise an EscposImage
* object into some sort of cache if you will re-use the output.
*/
require_once (dirname ( __FILE__ ) . "/../../Escpos.php");
// Paths to images to combine
$img1_path = dirname ( __FILE__ ) . "/../resources/tux.png";
$img2_path = dirname ( __FILE__ ) . "/../resources/escpos-php.png";
// Set up temp file with .png extension
$tmpf_path = tempnam ( sys_get_temp_dir (), 'escpos-php' );
$imgCombined_path = $tmpf_path . ".png";
try {
// Convert, load image, remove temp files
$cmd = sprintf ("convert %s %s +append %s",
escapeshellarg ( $img1_path ),
escapeshellarg ( $img2_path ),
escapeshellarg ( $imgCombined_path ));
exec($cmd, $outp, $retval);
if($retval != 0) {
// Detect and handle command failure
throw new Exception("Command \"$cmd\" returned $retval." . implode("\n", $outp));
}
$img = new EscposImage ( $imgCombined_path );
// Setup the printer
$connector = new FilePrintConnector ( "php://stdout" );
$profile = DefaultCapabilityProfile::getInstance ();
// Run the actual print
$printer = new Escpos ( $connector, $profile );
try {
$printer -> setJustification(Escpos::JUSTIFY_CENTER);
$printer -> graphics($img);
$printer -> cut();
} finally {
// Always close the connection
$printer -> close();
}
} catch (Exception $e) {
// Print out any errors: Eg. printer connection, image loading & external image manipulation.
echo $e -> getMessage() . "\n";
echo $e -> getTraceAsString();
} finally {
unlink ( $imgCombined_path );
unlink ( $tmpf_path );
}