summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/message_picture.php132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/message_picture.php b/src/message_picture.php
new file mode 100644
index 0000000..26ce60e
--- /dev/null
+++ b/src/message_picture.php
@@ -0,0 +1,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;
+} \ No newline at end of file