summaryrefslogtreecommitdiff
path: root/src/web/lib/image.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/web/lib/image.php')
-rw-r--r--src/web/lib/image.php77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/web/lib/image.php b/src/web/lib/image.php
new file mode 100644
index 0000000..6d42678
--- /dev/null
+++ b/src/web/lib/image.php
@@ -0,0 +1,77 @@
+<?php /* Copyright (c) 2024 Freya Murphy */
+
+function image(
+ $src,
+ $class = NULL,
+ $link = NULL,
+ $click = NULL,
+ $height = NULL,
+ $width = NULL,
+ $mime = NULL,
+): string {
+ if ($class) {
+ $class = 'image loading ' . $class;
+ } else {
+ $class = 'image loading';
+ }
+
+ $content = '';
+
+ // dont need mime for images
+ if ($mime && strpos($mime, 'image') !== FALSE) {
+ $mime = NULL;
+ }
+
+ if ($link) {
+ $content .= '<a class="' . $class . '" href="' . $link . '">';
+ } else if ($click) {
+ $content .= '<button class="' . $class . '" onclick="' . $click . '">';
+ } else {
+ $content .= '<span class="' . $class . '">';
+ }
+ if ($mime) {
+ $content .= '<object class="inner" type="' . $mime . '" data="' . $src . '" ';
+ } else {
+ $content .= '<img class="inner" src="' . $src . '" ';
+ }
+ if ($height) {
+ $content .= "height=\"{$height}\" ";
+ }
+ if ($width) {
+ $content .= "width=\"{$width}\" ";
+ }
+ if ($mime) {
+ $content .= '></object>';
+ } else {
+ $content .= 'onerror="onImgError(this)" onload="onImgLoad(this)"/>';
+ }
+ if ($link) {
+ $content .= '</a>';
+ } else if ($click) {
+ $content .= '</button>';
+ } else {
+ $content .= '</span>';
+ }
+
+ return $content;
+}
+
+function pfp(
+ $user,
+ $link = TRUE,
+ $click = NULL
+): string {
+ if ($link === TRUE) {
+ $link = '/profile?id=' . $user['id'];
+ }
+ $mime = NULL;
+ if (isset($user['avatar_mime'])) {
+ $mime = $user['avatar_mime'];
+ }
+ return image('/api/rpc/profile_avatar?user_id=' . $user['id'],
+ 'pfp',
+ link: $link,
+ click: $click,
+ mime: $mime
+ );
+}