emmanuelbarrameda

TakePicture.php

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