-
Notifications
You must be signed in to change notification settings - Fork 6
/
generator.php
49 lines (47 loc) · 2.98 KB
/
generator.php
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
<?php
//Why would we ever need to use an image generation library? All done in less than 50 lines of code...
mb_internal_encoding("UTF-8");
header('Content-type: image/svg+xml');
// Input validation
$background = (isset($_GET["background"]) && ctype_xdigit($_GET["background"]) && strlen($_GET["background"]) == 6) ? "#" . $_GET["background"] : null;
$color = (isset($_GET["color"]) && ctype_xdigit($_GET["color"]) && strlen($_GET["color"]) == 6) ? "#" . $_GET["color"] : "#fff";
$name = isset($_GET["name"]) ? filter_var($_GET["name"], FILTER_SANITIZE_STRING) : "O";
$length = isset($_GET["length"]) ? intval($_GET["length"]) : 2;
$width = isset($_GET["width"]) ? intval($_GET["width"]) : "500";
$height = isset($_GET["height"]) ? intval($_GET["height"]) : "500";
$font_size = isset($_GET["fontSize"]) ? intval($_GET["fontSize"]) : "250";
$rounded = isset($_GET["rounded"]) ? $_GET["rounded"] : ((isset($_GET["isRounded"]) && $_GET["isRounded"] == "true") ? "50" : "0");
$capitalize = (isset($_GET["caps"]) && $_GET["caps"] == "1") ? true : false;
$lowercase = (isset($_GET["caps"]) && $_GET["caps"] == "2") ? true : false;
$bold = (isset($_GET["bold"]) && $_GET["bold"] == "true") ? true : false;
$letters = '';
$words = preg_split('/[\s-]+/', $name);
if ($length == 1) {
$words = [$words[0]];
} else {
array_splice($words, $length - 1, count($words) - $length);
}
foreach ($words as $word) {
if (ctype_digit($word) && strlen($word) == 1) {
$letters .= $word;
} else {
$first = grapheme_substr($word, 0, 1);
$letters .= ctype_digit($first) ? '' : $first;
}
}
if (!$background) {
//If not set or defined, pick a random sexy color.
$colors = $colors = ['#e53935', '#d81b60', '#8e24aa', '#5e35b1', '#3949ab', '#1e88e5', '#039be5', '#00acc1', '#00897b', '#43a047', '#7cb342', '#c0ca33', '#fdd835', '#ffb300', '#fb8c00', '#f4511e', '#6d4c41', '#757575', '#546e7a'];
$random_color_key = hexdec(substr(md5($name), -8)) % count($colors);
$background = $colors[$random_color_key];
}
if ($capitalize) {
$letters = mb_strtoupper($letters);
} else if ($lowercase) {
$letters = mb_strtolower($letters);
}
$style = "";
if ($bold) {
$style = "font-weight:700;";
}
echo '<svg style="' . $style . '" width="' . (string) $width . 'px" height="' . (string) $height . 'px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><style type="text/css">@font-face {font-family: "montserratbold";src: url("https://cdn.oxro.io/fonts/montserrat-bold-webfont.woff2") format("woff2"),url("https://cdn.oxro.io/fonts/montserrat-bold-webfont.woff") format("woff");font-weight: normal;font-style: normal;}</style></defs><rect x="0" y="0" width="500" height="500" rx="' . (string)$rounded .'" style="fill:' . $background . '"/><text x="50%" y="50%" dy=".1em" fill="' . $color . '" text-anchor="middle" dominant-baseline="middle" style="font-family: "Montserrat", sans-serif; font-size: ' . (string) $font_size . 'px; line-height: 1">' . $letters . '</text></svg>';