font-placeholders.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. require_once(__DIR__ . "/../vendor/autoload.php");
  3. use Mike42\GfxPhp\Image;
  4. // Inputs
  5. $outFile = "out.pbm";
  6. $font = Image::fromFile(dirname(__FILE__). "/resources/5x7hex.pbm");
  7. $codePoint = str_split("0A2F");
  8. $charWidth = 5;
  9. $charHeight = 7;
  10. // Create small image for each character
  11. $chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
  12. $subImages = [];
  13. for ($i = 0; $i < count($codePoint); $i++) {
  14. $id = array_search($codePoint[$i], $chars);
  15. if ($id === false) {
  16. throw new Exception("Don't know how to encode " . $codePoint[$i]);
  17. }
  18. $subImages[] = $font -> subImage($id * $charWidth, 0, $charWidth, $charHeight);
  19. }
  20. // Place four images in a box
  21. $out = Image::create(18, 17, Image::IMAGE_BLACK_WHITE);
  22. $out -> rect(0, 0, 18, 17);
  23. $out -> rect(3, 0, 12, 17, true, 0, 0);
  24. $out -> compose($subImages[0], 0, 0, 4, 1, $charWidth, $charHeight);
  25. $out -> compose($subImages[1], 0, 0, 10, 1, $charWidth, $charHeight);
  26. $out -> compose($subImages[2], 0, 0, 4, 9, $charWidth, $charHeight);
  27. $out -> compose($subImages[3], 0, 0, 10, 9, $charWidth, $charHeight);
  28. # Print output for debugging ;)
  29. echo $out -> toString();
  30. $out -> write($outFile);