3 namespace BookStack\Uploads;
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Permissions\Models\JointPermission;
8 use BookStack\Permissions\PermissionApplicator;
9 use BookStack\Users\Models\HasCreatorAndUpdater;
10 use Illuminate\Database\Eloquent\Builder;
11 use Illuminate\Database\Eloquent\Factories\HasFactory;
12 use Illuminate\Database\Eloquent\Relations\HasMany;
16 * @property string $name
17 * @property string $url
18 * @property string $path
19 * @property string $type
20 * @property int $uploaded_to
21 * @property int $created_by
22 * @property int $updated_by
24 class Image extends Model
27 use HasCreatorAndUpdater;
29 protected $fillable = ['name'];
30 protected $hidden = [];
32 public function jointPermissions(): HasMany
34 return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
35 ->where('joint_permissions.entity_type', '=', 'page');
39 * Scope the query to just the images visible to the user based upon the
40 * user visibility of the uploaded_to page.
42 public function scopeVisible(Builder $query): Builder
44 return app()->make(PermissionApplicator::class)->restrictPageRelationQuery($query, 'images', 'uploaded_to');
48 * Get a thumbnail URL for this image.
49 * Attempts to generate the thumbnail if not already existing.
53 public function getThumb(?int $width, ?int $height, bool $keepRatio = false): ?string
55 return app()->make(ImageResizer::class)->resizeToThumbnailUrl($this, $width, $height, $keepRatio, false);
59 * Get the page this image has been uploaded to.
60 * Only applicable to gallery or drawio image types.
62 public function getPage(): ?Page
64 return $this->belongsTo(Page::class, 'uploaded_to')->first();