Skip to content

Commit 10f545f

Browse files
authored
Attempt to fix png conversion on Heroku by prepending xml tag (#160)
* refactor: Minor formatting adjustments * fix: prepend xml tag when converting svg
1 parent 49bed5b commit 10f545f

File tree

7 files changed

+37
-18
lines changed

7 files changed

+37
-18
lines changed

src/card.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* Convert date from Y-M-D to more human-readable format
@@ -295,7 +297,8 @@ function generateErrorCard(string $message, array $params = null): string
295297
*
296298
* @param string $svg The SVG for the card to display
297299
*/
298-
function echoAsSvg(string $svg): void {
300+
function echoAsSvg(string $svg): void
301+
{
299302
// set content type to SVG image
300303
header("Content-Type: image/svg+xml");
301304

@@ -310,10 +313,11 @@ function echoAsSvg(string $svg): void {
310313
*
311314
* @throws ImagickException
312315
*/
313-
function echoAsPng(string $svg): void {
316+
function echoAsPng(string $svg): void
317+
{
314318
// trim off all whitespaces to make it a valid SVG string
315319
$svg = trim($svg);
316-
320+
317321
// remove style and animations
318322
$svg = preg_replace('/(<style>\X*<\/style>)/m', '', $svg);
319323
$svg = preg_replace('/(opacity: 0;)/m', 'opacity: 1;', $svg);
@@ -325,7 +329,7 @@ function echoAsPng(string $svg): void {
325329
$imagick->setBackgroundColor(new ImagickPixel('transparent'));
326330

327331
// add svg image
328-
$imagick->readImageBlob($svg);
332+
$imagick->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
329333
$imagick->setImageFormat('png');
330334

331335
// echo PNG data

src/demo/preview.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
require_once "../card.php";
46

src/index.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35

46
// load functions
@@ -15,9 +17,9 @@
1517

1618
// if environment variables are not loaded, display error
1719
if (!$_SERVER["TOKEN"] || !$_SERVER["USERNAME"]) {
18-
$message = file_exists(dirname(__DIR__.'.env',1))
19-
? "Missing token or username in config. Check Contributing.md for details."
20-
: ".env was not found. Check Contributing.md for details.";
20+
$message = file_exists(dirname(__DIR__ . '.env', 1))
21+
? "Missing token or username in config. Check Contributing.md for details."
22+
: ".env was not found. Check Contributing.md for details.";
2123

2224
$card = generateErrorCard($message);
2325
if ($requestedType === "png") {

src/stats.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
/**
46
* Get all HTTP request responses for user's contributions

tests/OptionsTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
4+
25
use PHPUnit\Framework\TestCase;
36

47
// load functions
@@ -56,7 +59,7 @@ public function testThemesHaveValidParameters(): void
5659
foreach ($themes as $theme => $colors) {
5760
// check that there are no extra keys in the theme
5861
$this->assertEquals(
59-
array_diff_key($colors, $this->defaultTheme),
62+
array_diff_key($colors, $this->defaultTheme),
6063
array(),
6164
"The theme '$theme' contains invalid parameters."
6265
);

tests/RenderTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
4+
25
use PHPUnit\Framework\TestCase;
36

47
// load functions

tests/StatsTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
<?php declare (strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
4+
25
use PHPUnit\Framework\TestCase;
36

47
// load functions
5-
require_once dirname(__DIR__, 1).'/vendor/autoload.php';
8+
require_once dirname(__DIR__, 1) . '/vendor/autoload.php';
69
require_once "src/stats.php";
710

811
// load .env
@@ -13,8 +16,8 @@
1316
// if environment variables are not loaded, display error
1417
if (!$_SERVER["TOKEN"] || !$_SERVER["USERNAME"]) {
1518
$message = file_exists(dirname(__DIR__ . '.env', 1))
16-
? "Missing token or username in config. Check Contributing.md for details."
17-
: ".env was not found. Check Contributing.md for details.";
19+
? "Missing token or username in config. Check Contributing.md for details."
20+
: ".env was not found. Check Contributing.md for details.";
1821

1922
die($message);
2023
}

0 commit comments

Comments
 (0)