Add attachment thumbnails and ACP refinements
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\Attachment;
|
||||
use App\Models\AttachmentExtension;
|
||||
use App\Models\Post;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Thread;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -114,6 +115,8 @@ class AttachmentController extends Controller
|
||||
$path = "attachments/{$scopeFolder}/{$filename}";
|
||||
Storage::disk($disk)->putFileAs("attachments/{$scopeFolder}", $file, $filename);
|
||||
|
||||
$thumbnailPayload = $this->maybeCreateThumbnail($file, $scopeFolder);
|
||||
|
||||
$attachment = Attachment::create([
|
||||
'thread_id' => $threadId,
|
||||
'post_id' => $postId,
|
||||
@@ -122,6 +125,9 @@ class AttachmentController extends Controller
|
||||
'user_id' => $user->id,
|
||||
'disk' => $disk,
|
||||
'path' => $path,
|
||||
'thumbnail_path' => $thumbnailPayload['path'] ?? null,
|
||||
'thumbnail_mime_type' => $thumbnailPayload['mime'] ?? null,
|
||||
'thumbnail_size_bytes' => $thumbnailPayload['size'] ?? null,
|
||||
'original_name' => $file->getClientOriginalName(),
|
||||
'extension' => $extension !== '' ? $extension : null,
|
||||
'mime_type' => $mime,
|
||||
@@ -162,6 +168,28 @@ class AttachmentController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function thumbnail(Attachment $attachment): Response
|
||||
{
|
||||
if (!$this->canViewAttachment($attachment)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
if (!$attachment->thumbnail_path) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$disk = Storage::disk($attachment->disk);
|
||||
if (!$disk->exists($attachment->thumbnail_path)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$mime = $attachment->thumbnail_mime_type ?: 'image/jpeg';
|
||||
|
||||
return $disk->response($attachment->thumbnail_path, null, [
|
||||
'Content-Type' => $mime,
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Attachment $attachment): JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
@@ -279,6 +307,8 @@ class AttachmentController extends Controller
|
||||
|
||||
private function serializeAttachment(Attachment $attachment): array
|
||||
{
|
||||
$isImage = str_starts_with((string) $attachment->mime_type, 'image/');
|
||||
|
||||
return [
|
||||
'id' => $attachment->id,
|
||||
'thread_id' => $attachment->thread_id,
|
||||
@@ -294,7 +324,159 @@ class AttachmentController extends Controller
|
||||
'mime_type' => $attachment->mime_type,
|
||||
'size_bytes' => $attachment->size_bytes,
|
||||
'download_url' => "/api/attachments/{$attachment->id}/download",
|
||||
'thumbnail_url' => $attachment->thumbnail_path
|
||||
? "/api/attachments/{$attachment->id}/thumbnail"
|
||||
: null,
|
||||
'is_image' => $isImage,
|
||||
'created_at' => $attachment->created_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
private function maybeCreateThumbnail($file, string $scopeFolder): ?array
|
||||
{
|
||||
$enabled = $this->settingBool('attachments.create_thumbnails', true);
|
||||
if (!$enabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$mime = $file->getMimeType() ?? '';
|
||||
if (!str_starts_with($mime, 'image/')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$maxWidth = $this->settingInt('attachments.thumbnail_max_width', 300);
|
||||
$maxHeight = $this->settingInt('attachments.thumbnail_max_height', 300);
|
||||
if ($maxWidth <= 0 || $maxHeight <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sourcePath = $file->getPathname();
|
||||
$info = @getimagesize($sourcePath);
|
||||
if (!$info) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$width, $height] = $info;
|
||||
if ($width <= 0 || $height <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($width <= $maxWidth && $height <= $maxHeight) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$ratio = min($maxWidth / $width, $maxHeight / $height);
|
||||
$targetWidth = max(1, (int) round($width * $ratio));
|
||||
$targetHeight = max(1, (int) round($height * $ratio));
|
||||
|
||||
$sourceImage = $this->createImageFromFile($sourcePath, $mime);
|
||||
if (!$sourceImage) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$thumbImage = imagecreatetruecolor($targetWidth, $targetHeight);
|
||||
if (!$thumbImage) {
|
||||
imagedestroy($sourceImage);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (in_array($mime, ['image/png', 'image/gif'], true)) {
|
||||
imagecolortransparent($thumbImage, imagecolorallocatealpha($thumbImage, 0, 0, 0, 127));
|
||||
imagealphablending($thumbImage, false);
|
||||
imagesavealpha($thumbImage, true);
|
||||
}
|
||||
|
||||
imagecopyresampled(
|
||||
$thumbImage,
|
||||
$sourceImage,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
$targetWidth,
|
||||
$targetHeight,
|
||||
$width,
|
||||
$height
|
||||
);
|
||||
|
||||
$quality = $this->settingInt('attachments.thumbnail_quality', 85);
|
||||
$thumbBinary = $this->renderImageBinary($thumbImage, $mime, $quality);
|
||||
|
||||
imagedestroy($sourceImage);
|
||||
imagedestroy($thumbImage);
|
||||
|
||||
if ($thumbBinary === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$filename = Str::uuid()->toString();
|
||||
$extension = strtolower((string) $file->getClientOriginalExtension());
|
||||
if ($extension !== '') {
|
||||
$filename .= ".{$extension}";
|
||||
}
|
||||
|
||||
$disk = 'local';
|
||||
$thumbPath = "attachments/{$scopeFolder}/thumbs/{$filename}";
|
||||
Storage::disk($disk)->put($thumbPath, $thumbBinary);
|
||||
|
||||
return [
|
||||
'path' => $thumbPath,
|
||||
'mime' => $mime,
|
||||
'size' => strlen($thumbBinary),
|
||||
];
|
||||
}
|
||||
|
||||
private function createImageFromFile(string $path, string $mime)
|
||||
{
|
||||
return match ($mime) {
|
||||
'image/jpeg', 'image/jpg' => @imagecreatefromjpeg($path),
|
||||
'image/png' => @imagecreatefrompng($path),
|
||||
'image/gif' => @imagecreatefromgif($path),
|
||||
'image/webp' => function_exists('imagecreatefromwebp') ? @imagecreatefromwebp($path) : null,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
private function renderImageBinary($image, string $mime, int $quality): ?string
|
||||
{
|
||||
ob_start();
|
||||
$success = false;
|
||||
|
||||
if (in_array($mime, ['image/jpeg', 'image/jpg'], true)) {
|
||||
$success = imagejpeg($image, null, max(10, min(95, $quality)));
|
||||
} elseif ($mime === 'image/png') {
|
||||
$compression = (int) round(9 - (max(10, min(95, $quality)) / 100) * 9);
|
||||
$success = imagepng($image, null, $compression);
|
||||
} elseif ($mime === 'image/gif') {
|
||||
$success = imagegif($image);
|
||||
} elseif ($mime === 'image/webp' && function_exists('imagewebp')) {
|
||||
$success = imagewebp($image, null, max(10, min(95, $quality)));
|
||||
}
|
||||
|
||||
$data = ob_get_clean();
|
||||
|
||||
if (!$success) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $data !== false ? $data : null;
|
||||
}
|
||||
|
||||
private function settingBool(string $key, bool $default): bool
|
||||
{
|
||||
$value = Setting::query()->where('key', $key)->value('value');
|
||||
if ($value === null) {
|
||||
return $default;
|
||||
}
|
||||
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
private function settingInt(string $key, int $default): int
|
||||
{
|
||||
$value = Setting::query()->where('key', $key)->value('value');
|
||||
if ($value === null) {
|
||||
return $default;
|
||||
}
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Actions\BbcodeFormatter;
|
||||
use App\Models\Post;
|
||||
use App\Models\Thread;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -132,6 +133,10 @@ class PostController extends Controller
|
||||
'mime_type' => $attachment->mime_type,
|
||||
'size_bytes' => $attachment->size_bytes,
|
||||
'download_url' => "/api/attachments/{$attachment->id}/download",
|
||||
'thumbnail_url' => $attachment->thumbnail_path
|
||||
? "/api/attachments/{$attachment->id}/thumbnail"
|
||||
: null,
|
||||
'is_image' => str_starts_with((string) $attachment->mime_type, 'image/'),
|
||||
'created_at' => $attachment->created_at?->toIso8601String(),
|
||||
])
|
||||
->values()
|
||||
@@ -175,13 +180,22 @@ class PostController extends Controller
|
||||
$entry = $map[$key];
|
||||
$url = $entry['url'];
|
||||
$mime = $entry['mime'] ?? '';
|
||||
if (str_starts_with($mime, 'image/')) {
|
||||
if (str_starts_with($mime, 'image/') && $this->displayImagesInline()) {
|
||||
return "[img]{$url}[/img]";
|
||||
}
|
||||
return "[url={$url}]{$rawName}[/url]";
|
||||
}, $body) ?? $body;
|
||||
}
|
||||
|
||||
private function displayImagesInline(): bool
|
||||
{
|
||||
$value = Setting::query()->where('key', 'attachments.display_images_inline')->value('value');
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
private function resolveGroupColor(?\App\Models\User $user): ?string
|
||||
{
|
||||
if (!$user) {
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||
use App\Models\Forum;
|
||||
use App\Models\Thread;
|
||||
use App\Actions\BbcodeFormatter;
|
||||
use App\Models\Setting;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -207,6 +208,10 @@ class ThreadController extends Controller
|
||||
'mime_type' => $attachment->mime_type,
|
||||
'size_bytes' => $attachment->size_bytes,
|
||||
'download_url' => "/api/attachments/{$attachment->id}/download",
|
||||
'thumbnail_url' => $attachment->thumbnail_path
|
||||
? "/api/attachments/{$attachment->id}/thumbnail"
|
||||
: null,
|
||||
'is_image' => str_starts_with((string) $attachment->mime_type, 'image/'),
|
||||
'created_at' => $attachment->created_at?->toIso8601String(),
|
||||
])
|
||||
->values()
|
||||
@@ -250,13 +255,22 @@ class ThreadController extends Controller
|
||||
$entry = $map[$key];
|
||||
$url = $entry['url'];
|
||||
$mime = $entry['mime'] ?? '';
|
||||
if (str_starts_with($mime, 'image/')) {
|
||||
if (str_starts_with($mime, 'image/') && $this->displayImagesInline()) {
|
||||
return "[img]{$url}[/img]";
|
||||
}
|
||||
return "[url={$url}]{$rawName}[/url]";
|
||||
}, $body) ?? $body;
|
||||
}
|
||||
|
||||
private function displayImagesInline(): bool
|
||||
{
|
||||
$value = Setting::query()->where('key', 'attachments.display_images_inline')->value('value');
|
||||
if ($value === null) {
|
||||
return true;
|
||||
}
|
||||
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
|
||||
}
|
||||
|
||||
private function resolveGroupColor(?\App\Models\User $user): ?string
|
||||
{
|
||||
if (!$user) {
|
||||
|
||||
@@ -33,6 +33,9 @@ class Attachment extends Model
|
||||
'user_id',
|
||||
'disk',
|
||||
'path',
|
||||
'thumbnail_path',
|
||||
'thumbnail_mime_type',
|
||||
'thumbnail_size_bytes',
|
||||
'original_name',
|
||||
'extension',
|
||||
'mime_type',
|
||||
@@ -41,6 +44,7 @@ class Attachment extends Model
|
||||
|
||||
protected $casts = [
|
||||
'size_bytes' => 'int',
|
||||
'thumbnail_size_bytes' => 'int',
|
||||
];
|
||||
|
||||
public function thread(): BelongsTo
|
||||
|
||||
Reference in New Issue
Block a user