From 2536d0db7edf5069bc2aa22493a7594134eac72a Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Tue, 10 Jun 2025 23:39:30 +0500 Subject: feat: generate message screenshot --- src/message_picture.php | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/message_picture.php (limited to 'src') 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 @@ += $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 -- cgit v1.2.3