emmanuelbarrameda

TakePicture.php

Apr 14th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.06 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Filament\Forms\Components;
  4.  
  5. use Filament\Forms\Components\Field;
  6. use Filament\Forms\Components\Concerns\HasFileAttachments;
  7. use Filament\Forms\Get;
  8. use Filament\Forms\Set;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11.  
  12. class TakePicture extends Field
  13. {
  14.     use HasFileAttachments;
  15.  
  16.     protected string $view = 'forms.components.take-picture';
  17.    
  18.     protected string $disk = 'user_photo';
  19.     protected ?string $directory = null;
  20.     protected string $visibility = 'public';
  21.     protected ?string $targetField = null;
  22.     protected bool $shouldDeleteTemporaryFile = true;
  23.     protected bool $showCameraSelector = false;
  24.     protected int $imageQuality = 90;
  25.     protected string $aspect = '16:9';
  26.     protected bool $useModal = true;
  27.    
  28.     public function disk(string $disk): static
  29.     {
  30.         $this->disk = $disk;
  31.        
  32.         return $this;
  33.     }
  34.    
  35.     public function getImageUrlPrefix(): string
  36.     {
  37.         return $this->disk === 'public' ? Storage::disk($this->disk)->url('') : '';
  38.     }
  39.  
  40.     public function directory(?string $directory): static
  41.     {
  42.         $this->directory = $directory;
  43.        
  44.         return $this;
  45.     }
  46.    
  47.     public function visibility(string $visibility): static
  48.     {
  49.         $this->visibility = $visibility;
  50.        
  51.         return $this;
  52.     }
  53.    
  54.     public function targetField(string $fieldName): static
  55.     {
  56.         $this->targetField = $fieldName;
  57.        
  58.         return $this;
  59.     }
  60.    
  61.     public function shouldDeleteTemporaryFile(bool $condition = true): static
  62.     {
  63.         $this->shouldDeleteTemporaryFile = $condition;
  64.        
  65.         return $this;
  66.     }
  67.    
  68.     public function showCameraSelector(bool $show = true): static
  69.     {
  70.         $this->showCameraSelector = $show;
  71.        
  72.         return $this;
  73.     }
  74.    
  75.     public function imageQuality(int $quality): static
  76.     {
  77.         $this->imageQuality = max(1, min(100, $quality));
  78.        
  79.         return $this;
  80.     }
  81.    
  82.     public function aspect(string $ratio): static
  83.     {
  84.         $this->aspect = $ratio;
  85.        
  86.         return $this;
  87.     }
  88.    
  89.     public function useModal(bool $useModal = true): static
  90.     {
  91.         $this->useModal = $useModal;
  92.        
  93.         return $this;
  94.     }
  95.    
  96.     public function getDisk(): string
  97.     {
  98.         return $this->disk;
  99.     }
  100.    
  101.     public function getDirectory(): ?string
  102.     {
  103.         return $this->directory;
  104.     }
  105.    
  106.     public function getVisibility(): string
  107.     {
  108.         return $this->visibility;
  109.     }
  110.    
  111.     public function getTargetField(): ?string
  112.     {
  113.         return $this->targetField ?? $this->getName();
  114.     }
  115.    
  116.     public function getShouldDeleteTemporaryFile(): bool
  117.     {
  118.         return $this->shouldDeleteTemporaryFile;
  119.     }
  120.    
  121.     public function getShowCameraSelector(): bool
  122.     {
  123.         return $this->showCameraSelector;
  124.     }
  125.    
  126.     public function getImageQuality(): int
  127.     {
  128.         return $this->imageQuality;
  129.     }
  130.    
  131.     public function getAspect(): string
  132.     {
  133.         return $this->aspect;
  134.     }
  135.    
  136.     public function getUseModal(): bool
  137.     {
  138.         return $this->useModal;
  139.     }
  140.  
  141.     public function saveBase64Image(string $base64Data): ?string
  142.     {
  143.         if (empty($base64Data)) {
  144.             return null;
  145.         }
  146.         $base64Data = preg_replace('#^data:image/\w+;base64,#i', '', $base64Data);
  147.        
  148.         //unique filename
  149.         $filename = Str::uuid() . '.png';
  150.         $path = $this->directory ? $this->directory . '/' . $filename : $filename;
  151.        
  152.         // store image
  153.         $imageData = base64_decode($base64Data);
  154.        
  155.         if (!$imageData) {
  156.             return null;
  157.         }
  158.    
  159.         Storage::disk($this->disk)->put($path, $imageData, $this->visibility);
  160.         return $path;
  161.     }
  162.    
  163.     protected function setUp(): void
  164.     {
  165.         parent::setUp();
  166.        
  167.         $this->afterStateHydrated(function (Get $get, Set $set, ?string $state): void {
  168.             //displaying an existing record
  169.         });
  170.        
  171.         $this->dehydrateStateUsing(function (?string $state, Get $get, Set $set): mixed {
  172.             //no base64 data, return the original state or null
  173.             if (!$state || !Str::startsWith($state, 'data:image/')) {
  174.                 return $state;
  175.             }
  176.            
  177.             //process the base64 image
  178.             $path = $this->saveBase64Image($state);
  179.            
  180.             if ($this->targetField && $this->targetField !== $this->getName()) {
  181.                 $set($this->targetField, $path);
  182.                 return null;
  183.             }
  184.            
  185.             return $path;
  186.         });
  187.        
  188.         //clean temp files
  189.         $this->afterStateUpdated(function (Get $get, ?string $old, ?string $state) {
  190.             if ($this->shouldDeleteTemporaryFile && $old && $old !== $state && Storage::disk($this->disk)->exists($old)) {
  191.                 Storage::disk($this->disk)->delete($old);
  192.             }
  193.         });
  194.     }
  195. }
Add Comment
Please, Sign In to add comment