resto-dhepot/packages/escpos-php/example/specific/148-data-uri.php

37 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/*
* Example of one way you could load a PNG data URI into an EscposImage object
* without using a file.
*/
require __DIR__ . '/../../vendor/autoload.php';
use Zelda\EscposPhp\Printer;
use Zelda\EscposPhp\PrintConnectors\FilePrintConnector;
use Zelda\EscposPhp\ImagickEscposImage;
// Data URI for a PNG image (red dot from https://en.wikipedia.org/wiki/Data_URI_scheme )
$uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==";
// Convert data URI to binary data
$imageBlob = base64_decode(explode(",", $uri)[1]);
// Give Imagick a filename with the correct extension to stop it from attempting
// to identify the format itself (this avoids CVE-20163714)
$imagick = new Imagick();
$imagick->setResourceLimit(6, 1); // Prevent libgomp1 segfaults, grumble grumble.
$imagick->readImageBlob($imageBlob, "input.png");
// Load Imagick straight into an EscposImage object
$im = new ImagickEscposImage();
$im->readImageFromImagick($imagick);
// Do a test print to make sure that this EscposImage object has the right data
// (should see a tiny bullet point)
$connector = new FilePrintConnector("php://output");
$printer = new Printer($connector);
$printer->bitImage($im);
$printer->cut();
$printer->close();