summaryrefslogtreecommitdiff
path: root/index.php
blob: 313238100ed66ae04ee9bf65ccb3c720aef20214 (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
<?php

use Gotenberg\Gotenberg;
use Gotenberg\Stream;

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

$decklist = $_ENV['DECKLIST'];

/*
 * (\d+) ([\w\s]+) \(([\w\d]+)\) (\d+)
 * 2 Hall of Storm Giants (AFR) 257
 */

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

/*
 * SELECT scryfallId from cardIdentifiers
 * JOIN cards ON cardIdentifiers.uuid=cards.uuid
 * WHERE cards.setCode='$SETCODE' AND cards.number='$NUMBER';
 */

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

$images = [];
foreach ($cards as $card) {
  $query = 'SELECT scryfallId 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']]);

  $id = $statement->fetchColumn()?: '';

  if (empty($id)) {
    continue;
  }

  // TODO: check double sided

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

/*
 curl \
--request POST 'http://localhost:3000/forms/chromium/convert/html' \
--form 'files=@"./index.html"' \
-o my.pdf -F marginTop=0.0 -F marginBottom=0.0 -F marginRight=0.0 -F marginLeft=0.0
 */

$template = <<<HTML
<html>
  <head>
    <style>* { margin: 0; padding: 0; } html {  } #bg { background-color: #fff; display: flex; flex-wrap: wrap; } img {display:block;}</style>
    <style>
    @media print {
      @page {
      }
    }
    html, body {
      padding: 0;
      margin: 0;
    }
    img {
      width: 2.49in;
      height: 3.48in;
      background-color: #16130e;
      background-color: #000;
    }
    </style>
  </head>
  <body>
    <div id="bg">{{imgs}}</div>
  </body>
</html>
HTML;

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

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

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

Gotenberg::save($request, __DIR__ . '/output');