3 namespace BookStack\Uploads;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Exceptions\ImageUploadException;
7 use BookStack\Permissions\PermissionApplicator;
9 use Illuminate\Database\Eloquent\Builder;
10 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 public function __construct(
15 protected ImageService $imageService,
16 protected PermissionApplicator $permissions,
17 protected ImageResizer $imageResizer,
22 * Get an image with the given id.
24 public function getById($id): Image
26 return Image::query()->findOrFail($id);
30 * Execute a paginated query, returning in a standard format.
31 * Also runs the query through the restriction system.
33 private function returnPaginated(Builder $query, int $page = 1, int $pageSize = 24): array
35 $images = $query->orderBy('created_at', 'desc')->skip($pageSize * ($page - 1))->take($pageSize + 1)->get();
36 $hasMore = count($images) > $pageSize;
38 $returnImages = $images->take($pageSize);
39 $returnImages->each(function (Image $image) {
40 $this->loadThumbs($image, false);
44 'images' => $returnImages,
45 'has_more' => $hasMore,
50 * Fetch a list of images in a paginated format, filtered by image type.
51 * Can be filtered by uploaded to and also by name.
53 public function getPaginatedByType(
57 int $uploadedTo = null,
58 string $search = null,
59 callable $whereClause = null
61 $imageQuery = Image::query()->where('type', '=', strtolower($type));
63 if ($uploadedTo !== null) {
64 $imageQuery = $imageQuery->where('uploaded_to', '=', $uploadedTo);
67 if ($search !== null) {
68 $imageQuery = $imageQuery->where('name', 'LIKE', '%' . $search . '%');
71 // Filter by page access
72 $imageQuery = $this->permissions->restrictPageRelationQuery($imageQuery, 'images', 'uploaded_to');
74 if ($whereClause !== null) {
75 $imageQuery = $imageQuery->where($whereClause);
78 return $this->returnPaginated($imageQuery, $page, $pageSize);
82 * Get paginated gallery images within a specific page or book.
84 public function getEntityFiltered(
86 string $filterType = null,
89 int $uploadedTo = null,
92 /** @var Page $contextPage */
93 $contextPage = Page::visible()->findOrFail($uploadedTo);
96 if ($filterType === 'book' || $filterType === 'page') {
97 $parentFilter = function (Builder $query) use ($filterType, $contextPage) {
98 if ($filterType === 'page') {
99 $query->where('uploaded_to', '=', $contextPage->id);
100 } elseif ($filterType === 'book') {
101 $validPageIds = $contextPage->book->pages()
105 $query->whereIn('uploaded_to', $validPageIds);
110 return $this->getPaginatedByType($type, $page, $pageSize, null, $search, $parentFilter);
114 * Save a new image into storage and return the new image.
116 * @throws ImageUploadException
118 public function saveNew(UploadedFile $uploadFile, string $type, int $uploadedTo = 0, int $resizeWidth = null, int $resizeHeight = null, bool $keepRatio = true): Image
120 $image = $this->imageService->saveNewFromUpload($uploadFile, $type, $uploadedTo, $resizeWidth, $resizeHeight, $keepRatio);
122 if ($type !== 'system') {
123 $this->loadThumbs($image, true);
130 * Save a new image from an existing image data string.
132 * @throws ImageUploadException
134 public function saveNewFromData(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
136 $image = $this->imageService->saveNew($imageName, $imageData, $type, $uploadedTo);
137 $this->loadThumbs($image, true);
143 * Save a drawing in the database.
145 * @throws ImageUploadException
147 public function saveDrawing(string $base64Uri, int $uploadedTo): Image
149 $name = 'Drawing-' . user()->id . '-' . time() . '.png';
151 return $this->imageService->saveNewFromBase64Uri($base64Uri, $name, 'drawio', $uploadedTo);
155 * Update the details of an image via an array of properties.
159 public function updateImageDetails(Image $image, $updateDetails): Image
161 $image->fill($updateDetails);
162 $image->updated_by = user()->id;
164 $this->loadThumbs($image, false);
170 * Update the image file of an existing image in the system.
171 * @throws ImageUploadException
173 public function updateImageFile(Image $image, UploadedFile $file): void
175 if ($file->getClientOriginalExtension() !== pathinfo($image->path, PATHINFO_EXTENSION)) {
176 throw new ImageUploadException(trans('errors.image_upload_replace_type'));
180 $image->updated_by = user()->id;
184 $this->imageService->replaceExistingFromUpload($image->path, $image->type, $file);
185 $this->loadThumbs($image, true);
189 * Destroys an Image object along with its revisions, files and thumbnails.
193 public function destroyImage(Image $image = null): void
196 $this->imageService->destroy($image);
201 * Destroy images that have a specific URL and type combination.
205 public function destroyByUrlAndType(string $url, string $imageType): void
207 $images = Image::query()
208 ->where('url', '=', $url)
209 ->where('type', '=', $imageType)
212 foreach ($images as $image) {
213 $this->destroyImage($image);
218 * Load thumbnails onto an image object.
220 public function loadThumbs(Image $image, bool $shouldCreate): void
222 $image->setAttribute('thumbs', [
223 'gallery' => $this->getThumbnail($image, 150, 150, false, $shouldCreate),
224 'display' => $this->getThumbnail($image, 1680, null, true, $shouldCreate),
229 * Get a thumbnail URL for the given image.
231 protected function getThumbnail(Image $image, ?int $width, ?int $height, bool $keepRatio, bool $shouldCreate): ?string
234 return $this->imageResizer->resizeToThumbnailUrl($image, $width, $height, $keepRatio, $shouldCreate);
235 } catch (Exception $exception) {
241 * Get the raw image data from an Image.
243 public function getImageData(Image $image): ?string
246 return $this->imageService->getImageData($image);
247 } catch (Exception $exception) {
253 * Get the user visible pages using the given image.
255 public function getPagesUsingImage(Image $image): array
257 $pages = Page::visible()
258 ->where('html', 'like', '%' . $image->url . '%')
259 ->get(['id', 'name', 'slug', 'book_id']);
261 foreach ($pages as $page) {
262 $page->setAttribute('url', $page->getUrl());
265 return $pages->all();