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
136 public function saveNew(UploadedFile $uploadFile, $type, $uploadedTo = 0)
138 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo);
139 $this->loadThumbs($image);
144 * Update the details of an image via an array of properties.
145 * @param Image $image
146 * @param array $updateDetails
149 public function updateImageDetails(Image $image, $updateDetails)
151 $image->fill($updateDetails);
153 $this->loadThumbs($image);
159 * Destroys an Image object along with its files and thumbnails.
160 * @param Image $image
163 public function destroyImage(Image $image)
165 $this->imageService->destroyImage($image);
171 * Load thumbnails onto an image object.
172 * @param Image $image
174 private function loadThumbs(Image $image)
177 'gallery' => $this->getThumbnail($image, 150, 150),
178 'display' => $this->getThumbnail($image, 840, 0, true)
183 * Get the thumbnail for an image.
184 * If $keepRatio is true only the width will be used.
185 * Checks the cache then storage to avoid creating / accessing the filesystem on every check.
187 * @param Image $image
190 * @param bool $keepRatio
193 public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRatio = false)
196 return $this->imageService->getThumbnail($image, $width, $height, $keepRatio);
197 } catch (FileNotFoundException $exception) {