summaryrefslogtreecommitdiff
path: root/mtg-pdf.php
blob: b5e9fb2c03e4eac9a7855002bf3f3f82ba5893ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

use Gotenberg\Gotenberg;
use Gotenberg\Stream;

$decklist = $_ENV['DECKLIST'];
if (empty($decklist)) {
  die('No decklist provided.');
}

require_once __DIR__ . '/vendor/autoload.php';

/*
 * Match cards from input
 */

preg_match_all("/(\d+) (.+) \(([\w\d]+)\) (\d+\w*) ?([\*\w]*)/", $decklist, $matches);
$cards = [];
for ($idx = 0; $idx < count($matches[0]); $idx++) {
  $cards[] = [
    'quantity' => $matches[1][$idx],
    'name' => $matches[2][$idx],
    'set' => strtoupper($matches[3][$idx]),
    'number' => $matches[4][$idx],
  ];
}

/*
 * Select data from DB
 */

$db = new \PDO('sqlite:AllPrintings.sqlite');

$images = [];
foreach ($cards as $card) {
  $query = 'SELECT scryfallId,layout from cardIdentifiers JOIN cards on cardIdentifiers.uuid=cards.uuid WHERE cards.setCode=:setCode AND cards.number=:number';
  $statement = $db->prepare($query);
  $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]);
  $result = $statement->fetch();
  $id = $result['scryfallId'] ?? '';

  if (empty($id)) {
    $query = 'SELECT scryfallId,layout from tokenIdentifiers JOIN tokens on tokenIdentifiers.uuid=tokens.uuid WHERE tokens.setCode=:setCode AND tokens.number=:number';
    $statement = $db->prepare($query);
    $statement->execute(['setCode' => $card['set'], 'number' => $card['number']]);
    $result = $statement->fetch();
    $id = $result['scryfallId'] ?? '';

    if (empty($id)) {
      echo "$card[name] ($card[set]) $card[number] not found.";
      continue;
    }
  }

  $images[] = [
    'quantity' => $card['quantity'],
    'src' => "https://cards.scryfall.io/png/front/" . substr($id, 0, 1) . "/" . substr($id, 1, 1) . "/$id.png",
    'alt' => $card['name'],
  ];

  if (in_array($result['layout'] ?? '', ['transform', 'double_faced_token'])) {
    $images[] = [
      'quantity' => $card['quantity'],
      'src' => "https://cards.scryfall.io/png/back/" . substr($id, 0, 1) . "/" . substr($id, 1, 1) . "/$id.png",
      'alt' => $card['name'],
    ];
  }
}

/*
 * Build HTML
 */

$template = <<<HTML
<html>
  <head>
    <style>
    html, body {
      padding: 0;
      margin: 0;
    }

    img {
      width: 2.49in;
      height: 3.48in;
      display: block;
      background-color: #16130e;
      background-color: #000;
    }

    #bg {
      display: flex;
      flex-wrap: wrap;
    }
    </style>
  </head>
  <body>
    <div id="bg">{{imgs}}</div>
  </body>
</html>
HTML;

$imgs = array_map(function ($image) {
  return str_repeat("<img src=\"$image[src]\" alt=\"$image[alt]\">", $image['quantity']);
}, $images);

$html = str_replace('{{imgs}}', implode('', $imgs), $template);

/*
 * Build PDF
 */

$request = Gotenberg::chromium('gotenberg:3000')
  ->paperSize(8.27, 11.7) # A4
  ->margins(0.3, 0, 0.3, 0)
  ->outputFilename(date("Ymd_His"))
  ->html(Stream::string('index.html', $html));

if (php_sapi_name() === 'cli') {
  @mkdir(__DIR__ . '/output');
  Gotenberg::save($request, __DIR__ . '/output');
} else {
  $response = Gotenberg::send($request);

  header('Content-Type: application/pdf');
  header('Content-Disposition: attachment; filename="' . date("Ymd_His") . '"');
  echo $response->getBody();
}