1 <?php namespace BookStack\Repos;
5 use BookStack\Services\ImageService;
6 use BookStack\Services\PermissionService;
7 use Symfony\Component\HttpFoundation\File\UploadedFile;
13 protected $imageService;
14 protected $restrictionService;
18 * ImageRepo constructor.
20 * @param ImageService $imageService
21 * @param PermissionService $permissionService
24 public function __construct(Image $image, ImageService $imageService, PermissionService $permissionService, Page $page)
26 $this->image = $image;
27 $this->imageService = $imageService;
28 $this->restrictionService = $permissionService;
34 * Get an image with the given id.
38 public function getById($id)
40 return $this->image->findOrFail($id);
44 * Execute a paginated query, returning in a standard format.
45 * Also runs the query through the restriction system.
48 * @param int $pageSize
51 private function returnPaginated($query, $page = 0, $pageSize = 24)
53 $images = $this->restrictionService->filterRelatedPages($query, 'images', 'uploaded_to');
54 $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
55 $hasMore = count($images) > $pageSize;
57 $returnImages = $images->take(24);
58 $returnImages->each(function ($image) {
59 $this->loadThumbs($image);
63 'images' => $returnImages,
69 * Gets a load images paginated, filtered by image type.
72 * @param int $pageSize
73 * @param bool|int $userFilter
76 public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
78 $images = $this->image->where('type', '=', strtolower($type));
80 if ($userFilter !== false) {
81 $images = $images->where('created_by', '=', $userFilter);
84 return $this->returnPaginated($images, $page, $pageSize);
88 * Search for images by query, of a particular type.
91 * @param int $pageSize
92 * @param string $searchTerm
95 public function searchPaginatedByType($type, $searchTerm, $page = 0, $pageSize = 24)
97 $images = $this->image->where('type', '=', strtolower($type))->where('name', 'LIKE', '%' . $searchTerm . '%');
98 return $this->returnPaginated($images, $page, $pageSize);
102 * Get gallery images with a particular filter criteria such as
103 * being within the current book or page.
106 * @param int $pageNum
107 * @param int $pageSize
110 public function getGalleryFiltered($filter, $pageId, $pageNum = 0, $pageSize = 24)
112 $images = $this->image->where('type', '=', 'gallery');
114 $page = $this->page->findOrFail($pageId);
116 if ($filter === 'page') {
117 $images = $images->where('uploaded_to', '=', $page->id);
118 } elseif ($filter === 'book') {
119 $validPageIds = $page->book->pages->pluck('id')->toArray();
120 $images = $images->whereIn('uploaded_to', $validPageIds);
123 return $this->returnPaginated($images, $pageNum, $pageSize);
127 * Save a new image into storage and return the new image.
128 * @param UploadedFile $uploadFile
129 * @param string $type
130 * @param int $uploadedTo
132 * @throws \BookStack\Exceptions\ImageUploadException
135 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
137 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
138 $this->loadThumbs($image);
143 * Save a drawing the the database;
144 * @param string $base64Uri
145 * @param int $uploadedTo
147 * @throws \BookStack\Exceptions\ImageUploadException
149 public function saveDrawing(string $base64Uri, int $uploadedTo)
151 $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
152 $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
157 * Replace the image content of a drawing.
158 * @param Image $image
159 * @param string $base64Uri
161 * @throws \BookStack\Exceptions\ImageUploadException
163 public function updateDrawing(Image $image, string $base64Uri)
165 return $this->imageService->updateImageFromBase64Uri($image, $base64Uri);
169 * Update the details of an image via an array of properties.
170 * @param Image $image
171 * @param array $updateDetails
173 * @throws \BookStack\Exceptions\ImageUploadException
176 public function updateImageDetails(Image $image, $updateDetails)
178 $image->fill($updateDetails);
180 $this->loadThumbs($image);
186 * Destroys an Image object along with its revisions, files and thumbnails.
187 * @param Image $image
191 public function destroyImage(Image $image)
193 $this->imageService->destroy($image);
199 * Load thumbnails onto an image object.
200 * @param Image $image
201 * @throws \BookStack\Exceptions\ImageUploadException
204 protected function loadThumbs(Image $image)
207 'gallery' => $this->getThumbnail($image, 150, 150),
208 'display' => $this->getThumbnail($image, 840, 0, true)
213 * Get the thumbnail for an image.
214 * If $keepRatio is true only the width will be used.
215 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
216 * @param Image $image
219 * @param bool $keepRatio
221 * @throws \BookStack\Exceptions\ImageUploadException
224 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
227 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
228 } catch (\Exception $exception) {
234 * Get the raw image data from an Image.
235 * @param Image $image
236 * @return null|string
238 public function getImageData(Image $image)
241 return $this->imageService->getImageData($image);
242 } catch (\Exception $exception) {
248 * Check if the provided image type is valid.
252 public function isValidType($type)
254 $validTypes = ['drawing', 'gallery', 'cover', 'system', 'user'];
255 return in_array($type, $validTypes);