Files
project-minnesota/app/Models/Image.php

66 lines
1.8 KiB
PHP
Raw Normal View History

2024-05-23 16:54:06 +02:00
<?php
namespace App\Models;
use App\HasThumbnail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Image extends Model implements HasThumbnail
{
use HasFactory;
2024-06-01 03:10:30 +02:00
/**
* The model's default values for attributes.
*
* @var array
*/
protected $attributes = [
'isCover' => false,
2024-06-12 19:51:41 +02:00
'isProcessing' => true,
2024-06-07 16:26:15 +02:00
'lightboxWidth' => 0,
'lightboxHeight' => 0,
2024-06-01 03:10:30 +02:00
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
2024-06-12 19:51:41 +02:00
protected $fillable = ['album_id', 'lightboxWidth', 'lightboxHeight', 'isCover', 'isProcessing'];
2024-06-01 03:10:30 +02:00
2024-05-23 16:54:06 +02:00
public function album(): BelongsTo
{
return $this->belongsTo(Album::class);
}
public function getThumbnail() : string {
2024-06-12 19:51:41 +02:00
return route('image.thumbnail', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
2024-06-07 16:26:15 +02:00
}
public function getDownload() : string {
2024-06-12 19:51:41 +02:00
return route('image.download', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
2024-06-07 16:26:15 +02:00
}
public function setLightboxSize(int $width, int $height) : void {
$this->lightboxWidth = $width;
$this->lightboxHeight = $height;
$this->save();
}
2024-06-21 19:27:40 +02:00
public function makeCover() : void {
Image::where('isCover', 1)->where('album_id', $this->album_id)->update(['isCover' => 0]);
$this->isCover = true;
$this->save();
}
2024-06-07 16:26:15 +02:00
public function getLightboxAttribute() : array {
return [
2024-06-12 19:51:41 +02:00
'location' => route('image.lightbox', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp,
2024-06-07 16:26:15 +02:00
'width' => $this->lightboxWidth,
'height' => $this->lightboxHeight,
];
2024-05-23 16:54:06 +02:00
}
}