Job.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace smusatov\bzb2u;
  3. class Job
  4. {
  5. protected Label $label;
  6. protected array $data;
  7. protected int $count = 1;
  8. public function __construct(Label $label)
  9. {
  10. $this->label = $label;
  11. $this->data = array_fill(1, $this->label->getHeightDots(), array_fill(1, $this->label->getWidthDots(), false));
  12. }
  13. public function getLabel(): Label
  14. {
  15. return $this->label;
  16. }
  17. public function getCount(): int
  18. {
  19. return $this->count;
  20. }
  21. public function setCount(int $count): void
  22. {
  23. $this->count = min(99, max(1, $count));
  24. }
  25. public function getPixel(int $x, int $y): bool
  26. {
  27. if(!isset($this->data[$y][$x])) {
  28. throw new \Exception('Out of range');
  29. }
  30. return $this->data[$y][$x];
  31. }
  32. public function setPixel(int $x, int $y, bool $value = true): void
  33. {
  34. if(!isset($this->data[$y][$x])) {
  35. throw new \Exception('Out of range');
  36. }
  37. $this->data[$y][$x] = $value;
  38. }
  39. public function getLines(): \Generator
  40. {
  41. foreach($this->data as $line) {
  42. $line = array_map(fn($bit) => $bit ? 1 : 0, $line);
  43. yield $line;
  44. }
  45. }
  46. public function getPreview()
  47. {
  48. $draw = new \ImagickDraw();
  49. $draw->setFillColor(new \ImagickPixel('#000000'));
  50. foreach($this->data as $y => $row){
  51. foreach($row as $x => $value){
  52. if(!$value) {
  53. continue;
  54. }
  55. $draw->point($x-1, $y-1);
  56. }
  57. }
  58. $im = new \Imagick();
  59. $im->newImage($this->label->getWidthDots(), $this->label->getHeightDots(),new \ImagickPixel('#ffffff'));
  60. $im->setImageFormat('png');
  61. $im->drawImage($draw);
  62. return $im->getImageBlob();
  63. }
  64. }