148-data-uri.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /*
  3. * Example of one way you could load a PNG data URI into an EscposImage object
  4. * without using a file.
  5. */
  6. require __DIR__ . '/../../vendor/autoload.php';
  7. use Mike42\Escpos\Printer;
  8. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  9. use Mike42\Escpos\ImagickEscposImage;
  10. // Data URI for a PNG image (red dot from https://en.wikipedia.org/wiki/Data_URI_scheme )
  11. $uri = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
  12. AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
  13. 9TXL0Y4OHwAAAABJRU5ErkJggg==";
  14. // Convert data URI to binary data
  15. $imageBlob = base64_decode(explode(",", $uri)[1]);
  16. // Give Imagick a filename with the correct extension to stop it from attempting
  17. // to identify the format itself (this avoids CVE-2016–3714)
  18. $imagick = new Imagick();
  19. $imagick -> setResourceLimit(6, 1); // Prevent libgomp1 segfaults, grumble grumble.
  20. $imagick -> readImageBlob($imageBlob, "input.png");
  21. // Load Imagick straight into an EscposImage object
  22. $im = new ImagickEscposImage();
  23. $im -> readImageFromImagick($imagick);
  24. // Do a test print to make sure that this EscposImage object has the right data
  25. // (should see a tiny bullet point)
  26. $connector = new FilePrintConnector("php://output");
  27. $printer = new Printer($connector);
  28. $printer -> bitImage($im);
  29. $printer -> cut();
  30. $printer -> close();