diff --git a/app/Http/Controllers/Admin/VideoCrudController.php b/app/Http/Controllers/Admin/VideoCrudController.php index 8d74ce0..60d0073 100644 --- a/app/Http/Controllers/Admin/VideoCrudController.php +++ b/app/Http/Controllers/Admin/VideoCrudController.php @@ -44,6 +44,7 @@ class VideoCrudController extends CrudController protected function setupListOperation() { CRUD::column('title'); + CRUD::column('image')->type('image'); CRUD::column('video'); /** @@ -64,6 +65,7 @@ class VideoCrudController extends CrudController CRUD::setValidation(VideoRequest::class); CRUD::field('title'); + CRUD::field('image')->type('image'); CRUD::field('video')->type('upload')->upload(true); /** diff --git a/app/Models/Video.php b/app/Models/Video.php index 019a969..4f8de99 100644 --- a/app/Models/Video.php +++ b/app/Models/Video.php @@ -5,6 +5,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations; +use Illuminate\Support\Str; +use Intervention\Image\ImageManagerStatic as Image; class Video extends Model { @@ -23,6 +25,56 @@ class Video extends Model }); } + public function setImageAttribute($value) + { + $attribute_name = "image"; + // or use your own disk, defined in config/filesystems.php + $disk = config('backpack.base.root_disk_name'); + // destination path relative to the disk above + $destination_path = "public/uploads/videos/images"; + + // if the image was erased + if (empty($value)) { + // delete the image from disk + if (isset($this->{$attribute_name}) && !empty($this->{$attribute_name})) { + \Storage::disk($disk)->delete($this->{$attribute_name}); + } + // set null on database column + $this->attributes[$attribute_name] = null; + } + + // if a base64 was sent, store it in the db + if (Str::startsWith($value, 'data:image')) + { + // 0. Make the image + $image = \Image::make($value)->encode('jpg', 90); + + // 1. Generate a filename. + $filename = md5($value.time()).'.jpg'; + + // 2. Store the image on disk. + \Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream()); + + // 3. Delete the previous image, if there was one. + if (isset($this->{$attribute_name}) && !empty($this->{$attribute_name})) { + \Storage::disk($disk)->delete($this->{$attribute_name}); + } + + // 4. Save the public path to the database + // but first, remove "public/" from the path, since we're pointing to it + // from the root folder; that way, what gets saved in the db + // is the public URL (everything that comes after the domain name) + $public_destination_path = Str::replaceFirst('public/', '', $destination_path); + $this->attributes[$attribute_name] = $public_destination_path.'/'.$filename; + + // TODO + // crop image to 300x300 + } elseif (!empty($value)) { + // if value isn't empty, but it's not an image, assume it's the model value for that attribute. + $this->attributes[$attribute_name] = $this->{$attribute_name}; + } + } + public function setVideoAttribute($value) { $attribute_name = "video"; diff --git a/app/Transformers/VideoTransformer.php b/app/Transformers/VideoTransformer.php index 150cbb5..a6d41c5 100644 --- a/app/Transformers/VideoTransformer.php +++ b/app/Transformers/VideoTransformer.php @@ -22,6 +22,7 @@ class VideoTransformer extends TransformerAbstract return [ 'id' => $video->id, 'title' => $video->title, + 'image' => url($video->image), 'video' => $file, ]; } diff --git a/database/migrations/2023_03_03_171631_add_image_to_videos.php b/database/migrations/2023_03_03_171631_add_image_to_videos.php new file mode 100644 index 0000000..75c73cf --- /dev/null +++ b/database/migrations/2023_03_03_171631_add_image_to_videos.php @@ -0,0 +1,35 @@ +after('title', function ($table) { + $table->text('image'); + }); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('videos', function (Blueprint $table) { + $table->dropColumn('image'); + }); + } +}