Skip to content

Instantly share code, notes, and snippets.

@Zakarialabib
Last active November 15, 2022 23:45
Show Gist options
  • Save Zakarialabib/7985166c5dab381d41b2cb9ee81c6c68 to your computer and use it in GitHub Desktop.
Save Zakarialabib/7985166c5dab381d41b2cb9ee81c6c68 to your computer and use it in GitHub Desktop.
Livewire url and image upload
    public function saveImage()
    {
        if ($this->image_url) {
            $image = file_get_contents($this->image_url);
            $imageName = Str::random(10).'.jpg';
            Storage::disk('local_files')->put('products/'.$imageName, $image, 'public');
            $this->product->image = $imageName;
        }elseif ($this->image) {
            
            $image = $this->image;
            $imageName = Str::slug($this->product->name).'.'.$this->image->extension();
            
            $img = ImageIntervention::make($image->getRealPath())->resize(1500, 1500, function ($constraint) {
                $constraint->aspectRatio();
            });
            $img->stream(); 
            Storage::disk('local_files')->put('products/'.$imageName, $img, 'public');
            $this->product->image = $imageName;
        }
        // gallery image
        if ($this->gallery != null) {
            
            $gallery = [];
            foreach ($this->gallery as $key => $value) {
                $image = $value;
                $imageName = Str::slug($this->product->name).'-'.$key.'.'.$value->extension();
                
                $img = ImageIntervention::make($image->getRealPath())->resize(1500, 1500, function ($constraint) {
                    $constraint->aspectRatio();
                });
                $img->stream(); 
                Storage::disk('local_files')->put('products/'.$imageName, $img, 'public');
                $gallery[] = $imageName;
            }
            $this->product->gallery = json_encode($gallery);
        }
        $this->product->save();
        $this->alert('success', __('Product image updated successfully.'));
        
        $this->emit('refreshIndex');
        
        $this->imageModal = false;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment