summaryrefslogtreecommitdiff
path: root/src/web/helper/image.php
blob: 1b6650fd2e69af20b253abc92bd77445d605ef62 (plain)
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
<?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
	);
}