151 lines
3.8 KiB
PHP
151 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ImageUploader extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
<<<<<<< HEAD
|
|
public $image;
|
|
public $imagePath;
|
|
public $fieldName; // Nombre del campo para el formulario
|
|
public $label = 'Subir imagen'; // Etiqueta personalizable
|
|
public $hover = false;
|
|
|
|
// Recibir parámetros si es necesario
|
|
public function mount($fieldName = 'image_path', $label = 'Subir imagen')
|
|
{
|
|
$this->fieldName = $fieldName;
|
|
$this->label = $label;
|
|
}
|
|
|
|
public function updatedImage()
|
|
{
|
|
$this->validate([
|
|
'image' => 'image|max:2048', // 2MB Max
|
|
]);
|
|
|
|
// Subir automáticamente al seleccionar
|
|
$this->uploadImage();
|
|
}
|
|
|
|
public function uploadImage()
|
|
{
|
|
$this->validate([
|
|
'image' => 'required|image|max:2048',
|
|
]);
|
|
|
|
// Guardar la imagen
|
|
$this->imagePath = $this->image->store('uploads', 'public');
|
|
// Emitir evento con el nombre del campo y la ruta
|
|
$this->dispatch('imageUploaded',
|
|
field: $this->fieldName,
|
|
path: $this->imagePath
|
|
);
|
|
=======
|
|
public $photo;
|
|
public $currentImage;
|
|
public $fieldName;
|
|
public $placeholder;
|
|
public $storagePath = 'tmp/uploads';
|
|
|
|
protected $rules = [
|
|
'photo' => 'nullable|image|max:2048', // 2MB Max
|
|
];
|
|
|
|
public function mount($fieldName = 'photo', $currentImage = null, $placeholder = null)
|
|
{
|
|
$this->fieldName = $fieldName;
|
|
$this->currentImage = $currentImage;
|
|
$this->placeholder = $placeholder ?? asset('images/default-avatar.png');
|
|
}
|
|
|
|
public function updatedPhoto()
|
|
{
|
|
$this->validate([
|
|
'photo' => 'image|max:2048', // 2MB Max
|
|
]);
|
|
}
|
|
|
|
public function removePhoto()
|
|
{
|
|
$this->photo = null;
|
|
$this->currentImage = null;
|
|
>>>>>>> f97a7a84985ea300216ba3ea2ab4c31306e1659a
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
<<<<<<< HEAD
|
|
$this->validate([
|
|
'image' => 'required|image|max:2048',
|
|
]);
|
|
|
|
// Guardar la imagen
|
|
$this->imagePath = $this->image->store('images', 'public');
|
|
|
|
// Emitir evento con el nombre del campo y la ruta
|
|
$this->emit('imageUploaded', [
|
|
'field' => $this->fieldName,
|
|
'path' => $this->imagePath
|
|
]);
|
|
}
|
|
|
|
public function removeImage()
|
|
{
|
|
$this->reset(['image', 'imagePath']);
|
|
|
|
// Cambiar emit por dispatch en Livewire v3+
|
|
$this->dispatch('imageRemoved',
|
|
field: $this->fieldName
|
|
);
|
|
}
|
|
|
|
public function toggleHover($status)
|
|
{
|
|
$this->hover = $status;
|
|
}
|
|
|
|
=======
|
|
$this->validate();
|
|
|
|
if ($this->photo) {
|
|
$path = $this->photo->store($this->storagePath);
|
|
|
|
if ($this->model) {
|
|
// Eliminar imagen anterior si existe
|
|
if ($this->model->{$this->fieldName}) {
|
|
Storage::delete($this->model->{$this->fieldName});
|
|
}
|
|
|
|
$this->model->{$this->fieldName} = $path;
|
|
$this->model->save();
|
|
}
|
|
|
|
$this->currentUrl = Storage::url($path);
|
|
$this->showSavedMessage = true;
|
|
$this->photo = null; // Limpiar el input de subida
|
|
}
|
|
}
|
|
|
|
protected function getCurrentImageUrl()
|
|
{
|
|
if ($this->model && $this->model->{$this->fieldName}) {
|
|
return Storage::url($this->model->{$this->fieldName});
|
|
}
|
|
|
|
return $this->placeholder;
|
|
}
|
|
|
|
>>>>>>> f97a7a84985ea300216ba3ea2ab4c31306e1659a
|
|
public function render()
|
|
{
|
|
return view('livewire.image-uploader');
|
|
}
|
|
}
|