Skip to content

Instantly share code, notes, and snippets.

@Zakarialabib
Last active November 15, 2022 23:45
Show Gist options
  • Select an option

  • Save Zakarialabib/7985166c5dab381d41b2cb9ee81c6c68 to your computer and use it in GitHub Desktop.

Select an option

Save Zakarialabib/7985166c5dab381d41b2cb9ee81c6c68 to your computer and use it in GitHub Desktop.

Revisions

  1. Zakarialabib renamed this gist Nov 15, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Zakarialabib created this gist Nov 15, 2022.
    54 changes: 54 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
        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;

        }