]> BookStack Code Mirror - bookstack/blob - app/Uploads/Image.php
Updated php deps
[bookstack] / app / Uploads / Image.php
1 <?php
2
3 namespace BookStack\Uploads;
4
5 use BookStack\Auth\Permissions\JointPermission;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Model;
8 use BookStack\Traits\HasCreatorAndUpdater;
9 use Illuminate\Database\Eloquent\Factories\HasFactory;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11
12 /**
13  * @property int    $id
14  * @property string $name
15  * @property string $url
16  * @property string $path
17  * @property string $type
18  * @property int    $uploaded_to
19  * @property int    $created_by
20  * @property int    $updated_by
21  */
22 class Image extends Model
23 {
24     use HasFactory;
25     use HasCreatorAndUpdater;
26
27     protected $fillable = ['name'];
28     protected $hidden = [];
29
30     public function jointPermissions(): HasMany
31     {
32         return $this->hasMany(JointPermission::class, 'entity_id', 'uploaded_to')
33             ->where('joint_permissions.entity_type', '=', 'page');
34     }
35
36     /**
37      * Get a thumbnail for this image.
38      *
39      * @throws \Exception
40      */
41     public function getThumb(int $width, int $height, bool $keepRatio = false): string
42     {
43         return app()->make(ImageService::class)->getThumbnail($this, $width, $height, $keepRatio);
44     }
45
46     /**
47      * Get the page this image has been uploaded to.
48      * Only applicable to gallery or drawio image types.
49      */
50     public function getPage(): ?Page
51     {
52         return $this->belongsTo(Page::class, 'uploaded_to')->first();
53     }
54 }