-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.php
More file actions
99 lines (75 loc) · 2.27 KB
/
generator.php
File metadata and controls
99 lines (75 loc) · 2.27 KB
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
<?php
/**
* Social posters generator
*
* @author Anton Lukin
* @version 1.0
*/
use PosterEditor\PosterEditor;
require __DIR__ . '/../vendor/autoload.php';
/**
* Add spaces to number
*/
function spaces($number) {
return number_format($number, 0, ',', ' ');
}
/**
* Add title in proper form
*/
function sanitize($number) {
if ($number > 9 && $number < 20) {
return spaces($number) . " раз";
}
if ($number % 10 < 2 || $number % 10 > 4) {
return spaces($number) . " раз";
}
return spaces($number) . " раза";
}
/**
* Decrypt count from request uri
*/
function decrypt($path, $count = 0) {
$slug = explode('/', trim($path, '/'));
if (isset($slug[1])) {
$number = preg_replace('/[^\d]+/', '', $slug[1]);
// Create salt as 9 x (slug length)
$salt = str_pad('', strlen($number), '9');
// Calculate count
$count = (int) $salt - (int) $number;
}
return $count;
}
{
$count = decrypt(urldecode($_SERVER['REQUEST_URI']));
// Set default image path
$image = "/posters/default.jpg";
// Set default title
$title = "Я календарь перевернул и снова третье сентября...";
// Update image and title if count valid
if ($count < 10000 && $count > 0) {
$image = "/posters/{$count}.jpg";
// Update title with count
$title = "Я календарь перевернул " . sanitize($count) . " и снова третье сентября...";
}
// Generate cover if not exists
if (!file_exists(__DIR__ . $image)) {
try {
$poster = new PosterEditor();
$poster->make(__DIR__ . '/include/poster.png');
$poster->text($title, array(
'x' => 50,
'y' => 440,
'width' => 1100,
'fontsize' => 24,
'fontpath' => __DIR__ . '/include/opensans.ttf',
'horizontal' => 'center',
'color' => '#ffffff',
));
$poster->save(__DIR__ . $image);
} catch(Exception $e) {
$image = "/posters/default.jpg";
}
}
// Show generator template
include_once __DIR__ . '/include/template.php';
}