Skip to content

Commit fa7ea77

Browse files
authored
feat: Add disable_animations parameter (DenverCoder1#452)
1 parent 601aa02 commit fa7ea77

File tree

5 files changed

+70
-27
lines changed

5 files changed

+70
-27
lines changed

README.md

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,27 @@ The `user` field is the only required option. All other fields are optional.
4343

4444
If the `theme` parameter is specified, any color customizations specified will be applied on top of the theme, overriding the theme's values.
4545

46-
| Parameter | Details | Example |
47-
| :---------------: | :---------------------------------------------: | :-----------------------------------------------------------------------: |
48-
| `user` | GitHub username to show stats for | `DenverCoder1` |
49-
| `theme` | The theme to apply (Default: `default`) | `dark`, `radical`, etc. [🎨➜](./docs/themes.md) |
50-
| `hide_border` | Make the border transparent (Default: `false`) | `true` or `false` |
51-
| `border_radius` | Set the roundness of the edges (Default: `4.5`) | Number `0` (sharp corners) to `248` (ellipse) |
52-
| `background` | Background color | **hex code** without `#` or **css color** |
53-
| `border` | Border color | **hex code** without `#` or **css color** |
54-
| `stroke` | Stroke line color between sections | **hex code** without `#` or **css color** |
55-
| `ring` | Color of the ring around the current streak | **hex code** without `#` or **css color** |
56-
| `fire` | Color of the fire in the ring | **hex code** without `#` or **css color** |
57-
| `currStreakNum` | Current streak number | **hex code** without `#` or **css color** |
58-
| `sideNums` | Total and longest streak numbers | **hex code** without `#` or **css color** |
59-
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
60-
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
61-
| `dates` | Date range text color | **hex code** without `#` or **css color** |
62-
| `date_format` | Date format pattern or empty for locale format | See note below on [📅 Date Formats](#-date-formats) |
63-
| `locale` | Locale for labels and numbers (Default: `en`) | ISO 639-1 code - See [🗪 Locales](#-locales) |
64-
| `type` | Output format (Default: `svg`) | Current options: `svg`, `png` or `json` |
65-
| `mode` | Streak mode (Default: `daily`) | `daily` (contribute daily) or `weekly` (contribute once per Sun-Sat week) |
46+
| Parameter | Details | Example |
47+
| :------------------: | :---------------------------------------------: | :-----------------------------------------------------------------------: |
48+
| `user` | GitHub username to show stats for | `DenverCoder1` |
49+
| `theme` | The theme to apply (Default: `default`) | `dark`, `radical`, etc. [🎨➜](./docs/themes.md) |
50+
| `hide_border` | Make the border transparent (Default: `false`) | `true` or `false` |
51+
| `border_radius` | Set the roundness of the edges (Default: `4.5`) | Number `0` (sharp corners) to `248` (ellipse) |
52+
| `background` | Background color | **hex code** without `#` or **css color** |
53+
| `border` | Border color | **hex code** without `#` or **css color** |
54+
| `stroke` | Stroke line color between sections | **hex code** without `#` or **css color** |
55+
| `ring` | Color of the ring around the current streak | **hex code** without `#` or **css color** |
56+
| `fire` | Color of the fire in the ring | **hex code** without `#` or **css color** |
57+
| `currStreakNum` | Current streak number | **hex code** without `#` or **css color** |
58+
| `sideNums` | Total and longest streak numbers | **hex code** without `#` or **css color** |
59+
| `currStreakLabel` | Current streak label | **hex code** without `#` or **css color** |
60+
| `sideLabels` | Total and longest streak labels | **hex code** without `#` or **css color** |
61+
| `dates` | Date range text color | **hex code** without `#` or **css color** |
62+
| `date_format` | Date format pattern or empty for locale format | See note below on [📅 Date Formats](#-date-formats) |
63+
| `locale` | Locale for labels and numbers (Default: `en`) | ISO 639-1 code - See [🗪 Locales](#-locales) |
64+
| `type` | Output format (Default: `svg`) | Current options: `svg`, `png` or `json` |
65+
| `mode` | Streak mode (Default: `daily`) | `daily` (contribute daily) or `weekly` (contribute once per Sun-Sat week) |
66+
| `disable_animations` | Disable SVG animations (Default: `false`) | `true` or `false` |
6667

6768
### 🖌 Themes
6869

src/card.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,22 @@ function generateErrorCard(string $message, array $params = null): string
476476
";
477477
}
478478

479+
/**
480+
* Remove animations from SVG
481+
*
482+
* @param string $svg The SVG for the card as a string
483+
* @return string The SVG without animations
484+
*/
485+
function removeAnimations(string $svg): string
486+
{
487+
$svg = preg_replace("/(<style>\X*?<\/style>)/m", "", $svg);
488+
$svg = preg_replace("/(opacity: 0;)/m", "opacity: 1;", $svg);
489+
$svg = preg_replace("/(animation: fadein[^;'\"]+)/m", "opacity: 1;", $svg);
490+
$svg = preg_replace("/(animation: currstreak[^;'\"]+)/m", "font-size: 28px;", $svg);
491+
$svg = preg_replace("/<a \X*?>(\X*?)<\/a>/m", '\1', $svg);
492+
return $svg;
493+
}
494+
479495
/**
480496
* Converts an SVG card to a PNG image
481497
*
@@ -488,11 +504,7 @@ function convertSvgToPng(string $svg): string
488504
$svg = trim($svg);
489505

490506
// remove style and animations
491-
$svg = preg_replace("/(<style>\X*?<\/style>)/m", "", $svg);
492-
$svg = preg_replace("/(opacity: 0;)/m", "opacity: 1;", $svg);
493-
$svg = preg_replace("/(animation: fadein[^;'\"]+)/m", "opacity: 1;", $svg);
494-
$svg = preg_replace("/(animation: currstreak[^;'\"]+)/m", "font-size: 28px;", $svg);
495-
$svg = preg_replace("/<a \X*?>(\X*?)<\/a>/m", '\1', $svg);
507+
$svg = removeAnimations($svg);
496508

497509
// replace all fully transparent colors in fill or stroke with "none"
498510
// this is a workaround for what seems to be a bug in inkscape where rgba alpha values are ignored
@@ -530,11 +542,14 @@ function convertSvgToPng(string $svg): string
530542
* Return headers and response based on type
531543
*
532544
* @param string|array $output The stats (array) or error message (string) to display
545+
* @param array<string,string>|NULL $params Request parameters
533546
* @return array The Content-Type header and the response body, and status code in case of an error
534547
*/
535-
function generateOutput(string|array $output): array
548+
function generateOutput(string|array $output, array $params = null): array
536549
{
537-
$requestedType = $_REQUEST["type"] ?? "svg";
550+
$params = $params ?? $_REQUEST;
551+
552+
$requestedType = $params["type"] ?? "svg";
538553

539554
// output JSON data
540555
if ($requestedType === "json") {
@@ -563,6 +578,10 @@ function generateOutput(string|array $output): array
563578
];
564579
}
565580
}
581+
// remove animations if disable_animations is set
582+
if (isset($params["disable_animations"]) && $params["disable_animations"] == "true") {
583+
$svg = removeAnimations($svg);
584+
}
566585
// output SVG card
567586
return [
568587
"contentType" => "image/svg+xml",

src/demo/index.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ function gtag() {
137137
<option value="weekly">Weekly</option>
138138
</select>
139139

140+
<label for="disable_animations">Disable Animations</label>
141+
<select class="param" id="hide-border" name="disable_animations">
142+
<option>false</option>
143+
<option>true</option>
144+
</select>
145+
140146
<details class="advanced">
141147
<summary>⚙ Advanced Options</summary>
142148
<div class="content parameters">

src/demo/js/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const preview = {
1212
locale: "en",
1313
border_radius: "4.5",
1414
mode: "daily",
15+
disable_animations: "false",
1516
},
1617

1718
/**

tests/RenderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,20 @@ public function testSplitLines(): void
134134
splitLines("19 de dez. de 2021 - 14 de mar.", 24, 0)
135135
);
136136
}
137+
138+
/**
139+
* Test disable_animations parameter
140+
*/
141+
public function testDisableAnimations(): void
142+
{
143+
$this->testParams["disable_animations"] = "true";
144+
// Check that the card is rendered as expected
145+
$response = generateOutput($this->testStats, $this->testParams);
146+
$render = $response["body"];
147+
$this->assertStringNotContainsString("opacity: 0;", $render);
148+
$this->assertStringContainsString("opacity: 1;", $render);
149+
$this->assertStringContainsString("font-size: 28px;", $render);
150+
$this->assertStringNotContainsString("animation:", $render);
151+
$this->assertStringNotContainsString("<style>", $render);
152+
}
137153
}

0 commit comments

Comments
 (0)