1 <?php namespace BookStack\Uploads;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Page;
5 use Symfony\Component\HttpFoundation\File\UploadedFile;
11 protected $imageService;
12 protected $restrictionService;
16 * ImageRepo constructor.
18 * @param ImageService $imageService
19 * @param \BookStack\Auth\Permissions\PermissionService $permissionService
20 * @param \BookStack\Entities\Page $page
22 public function __construct(Image $image, ImageService $imageService, PermissionService $permissionService, Page $page)
24 $this->image = $image;
25 $this->imageService = $imageService;
26 $this->restrictionService = $permissionService;
32 * Get an image with the given id.
36 public function getById($id)
38 return $this->image->findOrFail($id);
42 * Execute a paginated query, returning in a standard format.
43 * Also runs the query through the restriction system.
46 * @param int $pageSize
49 private function returnPaginated($query, $page = 0, $pageSize = 24)
51 $images = $this->restrictionService->filterRelatedPages($query, 'images', 'uploaded_to');
52 $images = $images->orderBy('created_at', 'desc')->skip($pageSize * $page)->take($pageSize + 1)->get();
53 $hasMore = count($images) > $pageSize;
55 $returnImages = $images->take(24);
56 $returnImages->each(function ($image) {
57 $this->loadThumbs($image);
61 'images' => $returnImages,
67 * Gets a load images paginated, filtered by image type.
70 * @param int $pageSize
71 * @param bool|int $userFilter
74 public function getPaginatedByType($type, $page = 0, $pageSize = 24, $userFilter = false)
76 $images = $this->image->where('type', '=', strtolower($type));
78 if ($userFilter !== false) {
79 $images = $images->where('created_by', '=', $userFilter);
82 return $this->returnPaginated($images, $page, $pageSize);
86 * Search for images by query, of a particular type.
89 * @param int $pageSize
90 * @param string $searchTerm
93 public function searchPaginatedByType($type, $searchTerm, $page = 0, $pageSize = 24)
95 $images = $this->image->where('type', '=', strtolower($type))->where('name', 'LIKE', '%' . $searchTerm . '%');
96 return $this->returnPaginated($images, $page, $pageSize);
100 * Get gallery images with a particular filter criteria such as
101 * being within the current book or page.
104 * @param int $pageNum
105 * @param int $pageSize
108 public function getGalleryFiltered($filter, $pageId, $pageNum = 0, $pageSize = 24)
110 $images = $this->image->where('type', '=', 'gallery');
112 $page = $this->page->findOrFail($pageId);
114 if ($filter === 'page') {
115 $images = $images->where('uploaded_to', '=', $page->id);
116 } elseif ($filter === 'book') {
117 $validPageIds = $page->book->pages->pluck('id')->toArray();
118 $images = $images->whereIn('uploaded_to', $validPageIds);
121 return $this->returnPaginated($images, $pageNum, $pageSize);
125 * Save a new image into storage and return the new image.
126 * @param UploadedFile $uploadFile
127 * @param string $type
128 * @param int $uploadedTo
130 * @throws \BookStack\Exceptions\ImageUploadException
133 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
135 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
136 $this->loadThumbs($image);
141 * Save a drawing the the database;
142 * @param string $base64Uri
143 * @param int $uploadedTo
145 * @throws \BookStack\Exceptions\ImageUploadException
147 public function saveDrawing(string $base64Uri, int $uploadedTo)
149 $name = 'Drawing-' . user()->getShortName(40) . '-' . strval(time()) . '.png';
150 $image = $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
156 * Update the details of an image via an array of properties.
157 * @param Image $image
158 * @param array $updateDetails
160 * @throws \BookStack\Exceptions\ImageUploadException
163 public function updateImageDetails(Image $image, $updateDetails)
165 $image->fill($updateDetails);
167 $this->loadThumbs($image);
173 * Destroys an Image object along with its revisions, files and thumbnails.
174 * @param Image $image
178 public function destroyImage(Image $image)
180 $this->imageService->destroy($image);
186 * Load thumbnails onto an image object.
187 * @param Image $image
188 * @throws \BookStack\Exceptions\ImageUploadException
191 protected function loadThumbs(Image $image)
194 'gallery' => $this->getThumbnail($image, 150, 150),
195 'display' => $this->getThumbnail($image, 840, 0, true)
200 * Get the thumbnail for an image.
201 * If $keepRatio is true only the width will be used.
202 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
203 * @param Image $image
206 * @param bool $keepRatio
208 * @throws \BookStack\Exceptions\ImageUploadException
211 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
214 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
215 } catch (\Exception $exception) {
221 * Get the raw image data from an Image.
222 * @param Image $image
223 * @return null|string
225 public function getImageData(Image $image)
228 return $this->imageService->getImageData($image);
229 } catch (\Exception $exception) {
235 * Check if the provided image type is valid.
239 public function isValidType($type)
241 $validTypes = ['gallery', 'cover', 'system', 'user'];
242 return in_array($type, $validTypes);