MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/mpl.imenso.co/app/Models/Gallery.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image as ImageLibrary;

class Gallery extends Model
{
    protected $table = 'galleries';

    public function images()
    {
        return $this->morphMany(\App\Models\Image::class, 'imageable');
    }

    public function deleteImage($type_id)
    {
        if ($this->images()->where('type_id', $type_id)->count() > 0) {
            $this->images()->where('type_id', $type_id)->first()->remove();
        }
    }

    public static function addNew($data)
    {
        $gallery = new self;
        $gallery->name = $data->name;
        $gallery->save();

        $gallery->uploadGalleryImage($data->image);
        $gallery->assignToArtist($data->user_id);
    }

    public function setImage($type_id, $location)
    {
        $image = new Image;
        $image->src = $location;
        $image->imageable_id = $this->id;
        $image->imageable_type = self::class;
        $image->type_id = $type_id;
        $image->save();
    }

    public function assignToArtist($user_id)
    {
        $this->galleryable_id = $user_id;
        $this->galleryable_type = \App\Models\User::class;
        $this->save();
    }

    public function uploadGalleryImage($image)
    {
        $filename = $this->id.'-'.time().'-gallery.'.$image->getClientOriginalExtension();

        $web_location = 'gallery/'.$filename;

        $processed_image = ImageLibrary::make($image->getRealPath());

        $image_type = ImageType::findOrFail(18);

        $processed_image->resize($image_type->default_size_x, $image_type->default_size_y, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });

        $save = $processed_image->save();

        Storage::disk('s3')->put($web_location, $save->__toString());

        $this->deleteImage($image_type->id);
        $this->setImage($image_type->id, $web_location);
    }

    public function galleryImage()
    {
        return $this->image(18);
    }

    public function image($image_type_id = 0)
    {
        if ($this->images()->where('type_id', $image_type_id)->count() > 0) {
            return $this->images()->where('type_id', $image_type_id)->first();
        } else {
            $image = new Image;
            $image->src = ImageType::findOrFail($image_type_id)->default_image;

            return $image;
        }
    }

    public function remove()
    {
        foreach ($this->images as $image) {
            $image->remove();
        }
        $this->delete();
    }
}