summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-06-10 23:39:30 +0500
committerilotterytea <iltsu@alright.party>2025-06-10 23:39:30 +0500
commit2536d0db7edf5069bc2aa22493a7594134eac72a (patch)
treeb434cc4df29ea92d93ee3f4383cea6708f9c76bc
parent4043b8cf79d870f025b23103914448e8507b1a04 (diff)
feat: generate message screenshot
-rw-r--r--public/generate.php24
-rw-r--r--src/message_picture.php132
2 files changed, 155 insertions, 1 deletions
diff --git a/public/generate.php b/public/generate.php
index 1238738..ba7883a 100644
--- a/public/generate.php
+++ b/public/generate.php
@@ -1,2 +1,24 @@
<?php
-echo 'xd'; \ No newline at end of file
+include_once '../src/message_picture.php';
+
+$old_pfp = imagecreatefrompng('./pfp.png');
+$oldw = imagesx($old_pfp);
+$oldh = imagesy($old_pfp);
+$pfp = imagecreatetruecolor(72, 72);
+imagecopyresampled($pfp, $old_pfp, 0, 0, 0, 0, 72, 72, $oldw, $oldh);
+
+$screenshot = generate_message_screenshot(
+ $pfp,
+ "ilotterytea",
+ "forsen",
+ "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos sed quae minus libero odit illum atque itaque nulla! Ut quia fuga excepturi saepe nemo magni exercitationem aperiam commodi maxime ab.",
+ time()
+);
+
+imagedestroy($pfp);
+imagedestroy($old_pfp);
+
+header('Content-Type: image/jpeg');
+header("Content-Disposition: inline; filename*=UTF-8''someshit.jpg");
+imagejpeg($screenshot);
+imagedestroy($screenshot); \ No newline at end of file
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