- revisit later; avatar, WYSIWYG
This commit is contained in:
@@ -312,63 +312,11 @@ function storeMultipleUploads(string $field, string $subdir, array $allowedExts,
|
||||
}
|
||||
return ['ok'=>true, 'files'=>$saved];
|
||||
}
|
||||
function storeAvatarUpload(string $field, int $maxBytes): array {
|
||||
$files = normalizeUploadFiles($field);
|
||||
if (!$files || ($files[0]['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) return ['ok'=>true, 'path'=>''];
|
||||
$file = $files[0];
|
||||
if (($file['error'] ?? UPLOAD_ERR_OK) !== UPLOAD_ERR_OK) return ['ok'=>false, 'error'=>uploadErrorText((int)$file['error'])];
|
||||
if ((int)($file['size'] ?? 0) > $maxBytes) return ['ok'=>false, 'error'=>'The avatar exceeds the configured size limit.'];
|
||||
$ext = strtolower(pathinfo((string)$file['name'], PATHINFO_EXTENSION));
|
||||
if (!isImageExt($ext)) return ['ok'=>false, 'error'=>'Please choose a JPG, PNG, GIF, or WebP avatar.'];
|
||||
|
||||
if (!function_exists('getimagesize') || !function_exists('imagecreatetruecolor')) {
|
||||
return storeUploadedFile($file, 'avatars', imageExts(), $maxBytes, ['image_only'=>true, 'resize_over_bytes'=>mbToBytes(1), 'image_max_width'=>512, 'image_quality'=>86]);
|
||||
}
|
||||
$info = @getimagesize((string)$file['tmp_name']);
|
||||
if (!$info || empty($info[0]) || empty($info[1]) || empty($info['mime'])) return ['ok'=>false, 'error'=>'The selected avatar is not a valid image.'];
|
||||
$loader = match($info['mime']) {
|
||||
'image/jpeg' => 'imagecreatefromjpeg',
|
||||
'image/png' => 'imagecreatefrompng',
|
||||
'image/gif' => 'imagecreatefromgif',
|
||||
'image/webp' => 'imagecreatefromwebp',
|
||||
default => '',
|
||||
};
|
||||
if ($loader === '' || !function_exists($loader)) return ['ok'=>false, 'error'=>'That avatar image type is not supported by this server.'];
|
||||
$src = @$loader((string)$file['tmp_name']);
|
||||
if (!$src) return ['ok'=>false, 'error'=>'The avatar could not be opened.'];
|
||||
|
||||
$w = (int)$info[0]; $h = (int)$info[1];
|
||||
$size = (int)p('avatar_crop_size', min($w, $h));
|
||||
$x = (int)p('avatar_crop_x', max(0, (int)(($w - $size) / 2)));
|
||||
$y = (int)p('avatar_crop_y', max(0, (int)(($h - $size) / 2)));
|
||||
$size = max(1, min($size, $w, $h));
|
||||
$x = max(0, min($x, $w - $size));
|
||||
$y = max(0, min($y, $h - $size));
|
||||
|
||||
$dst = imagecreatetruecolor(512, 512);
|
||||
imagecopyresampled($dst, $src, 0, 0, $x, $y, 512, 512, $size, $size);
|
||||
$dir = ensureUploadDir('avatars');
|
||||
$filename = date('YmdHis').'-'.bin2hex(random_bytes(5)).'.jpg';
|
||||
$dest = $dir.'/'.$filename;
|
||||
$ok = imagejpeg($dst, $dest, 86);
|
||||
imagedestroy($src);
|
||||
imagedestroy($dst);
|
||||
if (!$ok) return ['ok'=>false, 'error'=>'The cropped avatar could not be saved.'];
|
||||
@chmod($dest, 0664);
|
||||
return ['ok'=>true, 'path'=>uploadPublicPath('avatars', $filename)];
|
||||
}
|
||||
function formatBytes(int $bytes): string {
|
||||
if ($bytes >= 1048576) return number_format($bytes / 1048576, 1).' MB';
|
||||
if ($bytes >= 1024) return number_format($bytes / 1024, 1).' KB';
|
||||
return $bytes.' B';
|
||||
}
|
||||
function userAvatarHtml(?array $user, string $class = 'avatar-sm'): string {
|
||||
$name = (string)($user['username'] ?? 'User');
|
||||
$path = (string)($user['avatar_path'] ?? '');
|
||||
if ($path !== '') return '<img src="'.e($path).'" alt="'.e($name).'" class="avatar '.$class.'">';
|
||||
$initial = strtoupper(substr($name, 0, 1) ?: 'U');
|
||||
return '<span class="avatar avatar-initial '.$class.'" aria-hidden="true">'.e($initial).'</span>';
|
||||
}
|
||||
function uniqueForumSlug(string $title, int $id): string {
|
||||
$base = makeSlug($title) ?: 'topic';
|
||||
return $base.'-'.$id;
|
||||
|
||||
Reference in New Issue
Block a user