1 <?php namespace BookStack\Repos;
6 use BookStack\Services\ImageService;
7 use BookStack\Services\PermissionService;
8 use Illuminate\Contracts\Filesystem\FileNotFoundException;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 protected $imageService;
17 protected $restrictionService;
21 * ImageRepo constructor.
23 * @param ImageService $imageService
24 * @param PermissionService $permissionService
27 public function __construct(Image $image, ImageService $imageService, PermissionService $permissionService, Page $page)
29 $this->image = $image;
30 $this->imageService = $imageService;
31 $this->restrictionService = $permissionService;
37 * Get an image with the given id.
41 public function getById($id)
43 return $this->image->findOrFail($id);
47 * Execute a paginated query, returning in a standard format.
48 * Also runs the query through the restriction system.
51 * @param int $pageSize
54 private function returnPaginated($query, $page = 0, $pageSize = 24)
56 $images = $this->restrictionService->filterRelatedPages($query, 'images', 'uploaded_to');
57 $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
58 $hasMore = count($images) > $pageSize;
60 $returnImages = $images->take(24);
61 $returnImages->each(function ($image) {
62 $this->loadThumbs($image);
66 'images' => $returnImages,
72 * Gets a load images paginated, filtered by image type.
75 * @param int $pageSize
76 * @param bool|int $userFilter
79 public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
81 $images = $this->image->where('type', '=', strtolower($type));
83 if ($userFilter !== false) {
84 $images = $images->where('created_by', '=', $userFilter);
87 return $this->returnPaginated($images, $page, $pageSize);
91 * Search for images by query, of a particular type.
94 * @param int $pageSize
95 * @param string $searchTerm
98 public function searchPaginatedByType($type, $page = 0, $pageSize = 24, $searchTerm)
100 $images = $this->image->where('type', '=', strtolower($type))->where('name', 'LIKE', '%' . $searchTerm . '%');
101 return $this->returnPaginated($images, $page, $pageSize);
105 * Get gallery images with a particular filter criteria such as
106 * being within the current book or page.
107 * @param int $pagination
108 * @param int $pageSize
113 public function getGalleryFiltered($pagination = 0, $pageSize = 24, $filter, $pageId)
115 $images = $this->image->where('type', '=', 'gallery');
117 $page = $this->page->findOrFail($pageId);
119 if ($filter === 'page') {
120 $images = $images->where('uploaded_to', '=', $page->id);
121 } elseif ($filter === 'book') {
122 $validPageIds = $page->book->pages->pluck('id')->toArray();
123 $images = $images->whereIn('uploaded_to', $validPageIds);
126 return $this->returnPaginated($images, $pagination, $pageSize);
130 * Save a new image into storage and return the new image.
131 * @param UploadedFile $uploadFile
132 * @param string $type
133 * @param int $uploadedTo
135 * @throws \BookStack\Exceptions\ImageUploadException
138 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
140 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
141 $this->loadThumbs($image);
146 * Save a drawing the the database;
147 * @param string $base64Uri
148 * @param int $uploadedTo
150 * @throws \BookStack\Exceptions\ImageUploadException
152 public function saveDrawing(string $base64Uri, int $uploadedTo)
154 $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
155 $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawing', $uploadedTo);
160 * Replace the image content of a drawing.
161 * @param Image $image
162 * @param string $base64Uri
164 * @throws \BookStack\Exceptions\ImageUploadException
166 public function replaceDrawingContent(Image $image, string $base64Uri)
168 return $this->imageService->replaceImageDataFromBase64Uri($image, $base64Uri);
172 * Update the details of an image via an array of properties.
173 * @param Image $image
174 * @param array $updateDetails
176 * @throws \BookStack\Exceptions\ImageUploadException
179 public function updateImageDetails(Image $image, $updateDetails)
181 $image->fill($updateDetails);
183 $this->loadThumbs($image);
189 * Destroys an Image object along with its files and thumbnails.
190 * @param Image $image
193 public function destroyImage(Image $image)
195 $this->imageService->destroyImage($image);
201 * Load thumbnails onto an image object.
202 * @param Image $image
203 * @throws \BookStack\Exceptions\ImageUploadException
206 private function loadThumbs(Image $image)
209 'gallery' => $this->getThumbnail($image, 150, 150),
210 'display' => $this->getThumbnail($image, 840, 0, true)
215 * Get the thumbnail for an image.
216 * If $keepRatio is true only the width will be used.
217 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
219 * @param Image $image
222 * @param bool $keepRatio
224 * @throws \BookStack\Exceptions\ImageUploadException
227 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
230 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
231 } catch (FileNotFoundException $exception) {
238 * Get the raw image data from an Image.
239 * @param Image $image
240 * @return null|string
242 public function getImageData(Image $image)
245 return $this->imageService->getImageData($image);
246 } catch (\Exception $exception) {