]> BookStack Code Mirror - bookstack/blob - app/Repos/ImageRepo.php
Added custom user avatars
[bookstack] / app / Repos / ImageRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Image;
5 use BookStack\Services\ImageService;
6 use Setting;
7 use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9 class ImageRepo
10 {
11
12     protected $image;
13     protected $imageService;
14
15     /**
16      * ImageRepo constructor.
17      * @param Image        $image
18      * @param ImageService $imageService
19      */
20     public function __construct(Image $image, ImageService $imageService)
21     {
22         $this->image = $image;
23         $this->imageService = $imageService;
24     }
25
26
27     /**
28      * Get an image with the given id.
29      * @param $id
30      * @return mixed
31      */
32     public function getById($id)
33     {
34         return $this->image->findOrFail($id);
35     }
36
37
38     /**
39      * Gets a load images paginated, filtered by image type.
40      * @param string $type
41      * @param int    $page
42      * @param int    $pageSize
43      * @param bool|int   $userFilter
44      * @return array
45      */
46     public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
47     {
48         $images = $this->image->where('type', '=', strtolower($type));
49
50         if ($userFilter !== false) {
51             $images = $images->where('created_by', '=', $userFilter);
52         }
53
54         $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
55         $hasMore = count($images) > $pageSize;
56
57         $returnImages = $images->take(24);
58         $returnImages->each(function ($image) {
59             $this->loadThumbs($image);
60         });
61
62         return [
63             'images'  => $returnImages,
64             'hasMore' => $hasMore
65         ];
66     }
67
68     /**
69      * Save a new image into storage and return the new image.
70      * @param UploadedFile $uploadFile
71      * @param  string      $type
72      * @return Image
73      */
74     public function saveNew(UploadedFile $uploadFile, $type)
75     {
76         $image = $this->imageService->saveNewFromUpload($uploadFile, $type);
77         $this->loadThumbs($image);
78         return $image;
79     }
80
81     /**
82      * Update the details of an image via an array of properties.
83      * @param Image $image
84      * @param array $updateDetails
85      * @return Image
86      */
87     public function updateImageDetails(Image $image, $updateDetails)
88     {
89         $image->fill($updateDetails);
90         $image->save();
91         $this->loadThumbs($image);
92         return $image;
93     }
94
95
96     /**
97      * Destroys an Image object along with its files and thumbnails.
98      * @param Image $image
99      * @return bool
100      */
101     public function destroyImage(Image $image)
102     {
103         $this->imageService->destroyImage($image);
104         return true;
105     }
106
107
108     /**
109      * Load thumbnails onto an image object.
110      * @param Image $image
111      */
112     private function loadThumbs(Image $image)
113     {
114         $image->thumbs = [
115             'gallery' => $this->getThumbnail($image, 150, 150),
116             'display' => $this->getThumbnail($image, 840, 0, true)
117         ];
118     }
119
120     /**
121      * Get the thumbnail for an image.
122      * If $keepRatio is true only the width will be used.
123      * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
124      *
125      * @param Image $image
126      * @param int   $width
127      * @param int   $height
128      * @param bool  $keepRatio
129      * @return string
130      */
131     public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
132     {
133         return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
134     }
135
136
137 }