Skip to content

Instantly share code, notes, and snippets.

@awcodes
Created October 21, 2023 18:36

Revisions

  1. awcodes created this gist Oct 21, 2023.
    88 changes: 88 additions & 0 deletions CustomMedia.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    <?php

    namespace App\Models;

    use Awcodes\Curator\Models\Media;
    use Cloudinary\Cloudinary;
    use Cloudinary\Transformation\Format;
    use Cloudinary\Transformation\Quality;
    use Cloudinary\Transformation\Resize;
    use Illuminate\Database\Eloquent\Casts\Attribute;
    use Illuminate\Support\Facades\Storage;

    class CustomMedia extends Media
    {
    protected $table = 'media';

    protected function url(): Attribute
    {
    return new Attribute(
    get: function() {
    if ($this->disk === 'cloudinary') {
    return (string) (new Cloudinary())
    ->image($this->path)
    ->resize(Resize::fit(800))
    ->format(Format::auto())
    ->quality(Quality::auto())
    ->toUrl();
    }

    return Storage::disk($this->disk)->url($this->path);
    }
    );
    }

    protected function thumbnailUrl(): Attribute
    {
    return Attribute::make(
    get: function() {
    if ($this->disk === 'cloudinary') {
    return (string) (new Cloudinary())
    ->image($this->path)
    ->resize(Resize::crop(200, 200))
    ->format(Format::auto())
    ->quality(Quality::auto())
    ->toUrl();
    }

    return $this->getSignedUrl(['w' => 200, 'h' => 200, 'fit' => 'crop', 'fm' => 'webp']);
    }
    );
    }

    protected function mediumUrl(): Attribute
    {
    return Attribute::make(
    get: function() {
    if ($this->disk === 'cloudinary') {
    return (string) (new Cloudinary())
    ->image($this->path)
    ->resize(Resize::crop(640, 640))
    ->format(Format::auto())
    ->quality(Quality::auto())
    ->toUrl();
    }

    return $this->getSignedUrl(['w' => 640, 'h' => 640, 'fit' => 'crop', 'fm' => 'webp']);
    }
    );
    }

    protected function largeUrl(): Attribute
    {
    return Attribute::make(
    get: function() {
    if ($this->disk === 'cloudinary') {
    return (string) (new Cloudinary())
    ->image($this->path)
    ->resize(Resize::crop(1024, 1024))
    ->format(Format::auto())
    ->quality(Quality::auto())
    ->toUrl();
    }

    return $this->getSignedUrl(['w' => 1024, 'h' => 1024, 'fit' => 'contain', 'fm' => 'webp']);
    }
    );
    }
    }
    7 changes: 7 additions & 0 deletions curator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?php

    return [
    ...
    'model' => \App\Models\CustomMedia::class,
    ...
    ];