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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
<?php
function generate_message_screenshot(GdImage $pfp, string $username, string $channel, string $message, int $timestamp): GdImage
{
$font_size = 18;
$font_path = 'Arial.ttf';
$width = 500;
$height = 200;
$pad_x = 24;
$spacing = 4;
// dividing the message lines
$message_lines = [];
$max_line_width = $width - $pad_x * 4;
$buffer_line = "";
$buffer_part = "";
foreach (str_split($message) as $c) {
$buffer_part .= $c;
$bbox_line = imagettfbbox($font_size, 0, $font_path, $buffer_line);
$bbox_part = imagettfbbox($font_size, 0, $font_path, $buffer_part);
$line_width = abs($bbox_line[2] - $bbox_line[0]);
$line_width += abs($bbox_part[2] - $bbox_part[0]);
if ($line_width >= $max_line_width) {
if (empty($buffer_line)) {
$buffer_line .= $buffer_part;
$buffer_part = "";
}
array_push($message_lines, $buffer_line);
$buffer_line = "";
}
if ($c == ' ') {
$buffer_line .= $buffer_part;
$buffer_part = "";
}
}
if (!empty($buffer_part)) {
$buffer_line .= $buffer_part;
}
array_push($message_lines, $buffer_line);
// counting real image height
for ($i = 0; $i < count($message_lines); $i++) {
$h = $pad_x + imagesy($pfp) + $font_size * $i + $spacing * $i;
if ($h + $font_size * 2 >= $height) {
$height += $font_size + $spacing;
}
}
$height += $font_size + $pad_x + $spacing;
$image = imagecreate($width, $height);
$gray_color = imagecolorallocate($image, 50, 50, 50);
$black_color = imagecolorallocate($image, 0, 0, 0);
$white_color = imagecolorallocate($image, 255, 255, 255);
// setting the background
imagefilledrectangle($image, 0, 0, $width, $height, $white_color);
// setting the user's pfp
imagecopy(
$image,
$pfp,
$pad_x,
$pad_x,
0,
0,
imagesx($pfp),
imagesy($pfp)
);
// setting the username and the channel name
imagettftext(
$image,
$font_size,
0,
imagesx($pfp) + 32,
imagesy($pfp) / 2 + $pad_x,
$black_color,
$font_path,
$username
);
imagettftext(
$image,
$font_size - 2,
0,
imagesx($pfp) + 32,
imagesy($pfp) / 2 + $pad_x + $font_size,
$gray_color,
$font_path,
"in #{$channel}"
);
// setting the message
for ($i = 0; $i < count($message_lines); $i++) {
$h = 32 + $pad_x + imagesy($pfp) + $font_size * $i + $spacing * $i;
imagettftext(
$image,
$font_size,
0,
$pad_x,
$h,
$black_color,
$font_path,
$message_lines[$i]
);
}
// drawing the twitch logo
// setting the date
imagettftext(
$image,
$font_size - 4,
0,
$pad_x,
$height - $pad_x,
$gray_color,
$font_path,
date("F j, Y · G:i", $timestamp) . ' (UTC)'
);
return $image;
}
|