nyaabooru/app/Livewire/Pages/Upload.php
2025-05-24 23:16:40 -04:00

64 lines
1.5 KiB
PHP

<?php
namespace App\Livewire\Pages;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image;
use Livewire\Attributes\Title;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;
class Upload extends Component
{
use WithFileUploads;
#[Validate('image|max:10240')]
public $file;
#[Validate('required|in:safe,suggestive,explicit')]
public $rating = 'safe';
#[Title('Upload')]
public function render()
{
return view('livewire.pages.upload');
}
public function createPost()
{
$this->validate();
$author = Auth::user();
if ($this->file)
{
$post = Post::create([
'extension' => $this->file->getClientOriginalExtension(),
'rating' => $this->rating,
]);
if ($post)
{
$author->posts()->save($post);
// Save the full image
$this->file->storeAs("posts/$post->id", 'full');
$fullImg = Storage::get("posts/$post->id/full");
// Create thumbnail preview
$thumb = Image::read($fullImg)->scaleDown(width: 512, height: 512);
Storage::put("posts/$post->id/thumb", $thumb->encodeByExtension($post->extension, quality: 70));
// Create smaller preview image
$preview = Image::read($fullImg)->scaleDown(width: 1280, height: 720);
Storage::put("posts/$post->id/preview", $preview->encodeByExtension($post->extension, quality: 70));
return $this->redirect("/posts/$post->id");
}
}
return $this->redirect('/upload');
}
}